Created
January 4, 2018 07:12
-
-
Save clarkdo/23cbc29daf930d2aa05ab7de9deea2c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class TravelCost { | |
private final static Integer[][] participants = {{500, 700}, {200, 600}, {400, 500}, {600, 200}, {500,300}}; | |
private final static int cityAmount = participants[0].length; | |
private final static List<Integer> result = new ArrayList<>(); | |
public static void main(String[] args) { | |
sum(0, 0, 0, 0); | |
System.out.printf(String.valueOf(result)); | |
} | |
private static boolean sum(int fee, int i, int sf, int na) { | |
if (i == participants.length) { | |
return result.add(fee); | |
} | |
for (int p = 0; p < cityAmount; p++) { | |
int capacity = (int) Math.ceil(participants.length / 2.0); | |
if ((sf == capacity && p == 0) || | |
(na == capacity && p == 1)) { | |
continue; | |
} | |
sum(fee + participants[i][p], i + 1, p == 0 ? sf + 1 : sf, p == 0 ? na : na + 1); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment