Skip to content

Commit 40ec720

Browse files
committed
Add solution for 1029
1 parent 662b720 commit 40ec720

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Your ideas/fixes/algorithms are more than welcome!
2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3030
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1030.java) | O(R*C) | O(1) | |Easy|
31+
|1029|[Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1029.java)| O(nlogn) | O(1) | |Easy| Array, Sorting |
3132
|1022|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1022.java) | O(n) | O(n) | |Easy|
3233
|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | O(n) | O(n) | |Easy|
3334
|1020|[Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1020.java) | O(mn) | O(mn) | |Medium|Graph, DFS, BFS, recursion|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
/**
7+
* There are 2N people a company is planning to interview.
8+
* The cost of flying the i-th person to city A is costs[i][0], and
9+
* the cost of flying the i-th person to city B is costs[i][1].
10+
*
11+
* Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.
12+
*/
13+
14+
public class _1029 {
15+
public static class Solution1 {
16+
public int twoCitySchedCost(int[][] costs) {
17+
Arrays.sort(costs, Comparator.comparing((int[] arr) -> arr[0] - arr[1]));
18+
int totalCost = 0;
19+
final int n = costs.length;
20+
21+
for (int i = 0; i < n; ++i) {
22+
// 2i/n = I(i >= n/2), i.e. 1 if true, 0 if false
23+
totalCost += costs[i][2*i/n];
24+
}
25+
return totalCost;
26+
}
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1029;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1029Test {
10+
private static _1029.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1029.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
/*
20+
* The first person goes to city A for a cost of 10.
21+
* The second person goes to city A for a cost of 30.
22+
* The third person goes to city B for a cost of 50.
23+
* The fourth person goes to city B for a cost of 20.
24+
25+
* The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
26+
*/
27+
int[][] costs = {{10, 20}, {30, 200}, {400, 50}, {30, 20}};
28+
assertEquals(solution1.twoCitySchedCost(costs), 110);
29+
}
30+
}

0 commit comments

Comments
 (0)