Skip to content

Commit ba80cda

Browse files
add 1029
1 parent 697612e commit ba80cda

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Your ideas/fixes/algorithms are more than welcome!
3232
|1037|[Valid Boomerang](https://leetcode.com/problems/valid-boomerang/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1037.java) | O(1) | O(1) | |Easy|Math|
3333
|1033|[Moving Stones Until Consecutive](https://leetcode.com/problems/moving-stones-until-consecutive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1033.java) | O(1) | O(1) | |Easy|Math|
3434
|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|
35+
|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|
3536
|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|
3637
|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|
3738
|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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1029. Two City Scheduling
7+
*
8+
* There are 2N people a company is planning to interview.
9+
* The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[i][1].
10+
* Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.
11+
*
12+
* Example 1:
13+
*
14+
* Input: [[10,20],[30,200],[400,50],[30,20]]
15+
* Output: 110
16+
* Explanation:
17+
* The first person goes to city A for a cost of 10.
18+
* The second person goes to city A for a cost of 30.
19+
* The third person goes to city B for a cost of 50.
20+
* The fourth person goes to city B for a cost of 20.
21+
*
22+
* The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
23+
*
24+
* Note:
25+
*
26+
* 1 <= costs.length <= 100
27+
* It is guaranteed that costs.length is even.
28+
* 1 <= costs[i][0], costs[i][1] <= 1000
29+
* */
30+
public class _1029 {
31+
public static class Solution1 {
32+
/**credit: https://leetcode.com/problems/two-city-scheduling/discuss/280173/Java-4-lines-intuitive-solution
33+
* and
34+
* https://leetcode.com/problems/two-city-scheduling/discuss/278771/Java-sort-solution*/
35+
public int twoCitySchedCost(int[][] costs) {
36+
Arrays.sort(costs, (a, b) -> (a[0] - a[1] - (b[0] - b[1])));
37+
int cost = 0;
38+
for (int i = 0; i < costs.length; i++) {
39+
if (i < costs.length / 2) {
40+
cost += costs[i][0];
41+
} else {
42+
cost += costs[i][1];
43+
}
44+
}
45+
return cost;
46+
}
47+
}
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
private static int[][] costs;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1029.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
costs = new int[][]{
21+
{10, 20},
22+
{30, 200},
23+
{400, 50},
24+
{30, 20}
25+
};
26+
assertEquals(110, solution1.twoCitySchedCost(costs));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
costs = new int[][]{
32+
{259, 770},
33+
{448, 54},
34+
{926, 667},
35+
{184, 139},
36+
{840, 118},
37+
{577, 469}
38+
};
39+
assertEquals(1859, solution1.twoCitySchedCost(costs));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy