Skip to content

Commit 79deb72

Browse files
add 1094
1 parent 0119905 commit 79deb72

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ _If you like this project, please leave me a star._ ★
461461
|1103|[Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1103.java) | |Easy|Math|
462462
|1100|[Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1100.java) | |Medium|String, Sliding Window|
463463
|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java) | [:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI)|Easy||
464+
|1094|[Car Pooling](https://leetcode.com/problems/car-pooling/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1094.java) | |Medium|Array, Sorting, Heap, Simulation, Prefix Sum|
464465
|1090|[Largest Values From Labels](https://leetcode.com/problems/largest-values-from-labels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1090.java) | [:tv:](https://youtu.be/E0OkE3G95vU)|Medium|HashTable, Greedy|
465466
|1091|[Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1091.java) | |Medium|BFS|
466467
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | |Easy||
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.PriorityQueue;
5+
6+
public class _1094 {
7+
public static class Solution1 {
8+
public boolean carPooling(int[][] trips, int capacity) {
9+
Arrays.sort(trips, (a, b) -> a[1] - b[1]);
10+
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
11+
for (int[] trip : trips) {
12+
int startTime = trip[1];
13+
int endTime = trip[2];
14+
while (!heap.isEmpty() && heap.peek()[1] <= startTime) {
15+
int[] curr = heap.poll();
16+
capacity += curr[0];
17+
}
18+
int peopleCnt = trip[0];
19+
capacity -= peopleCnt;
20+
if (capacity < 0) {
21+
return false;
22+
}
23+
heap.offer(new int[]{peopleCnt, endTime});
24+
}
25+
return true;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1094;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1094Test {
11+
private static _1094.Solution1 solution1;
12+
private static int[][] trips;
13+
private static int capacity;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1094.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
trips = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2,1,5],[3,3,7]");
23+
capacity = 4;
24+
assertEquals(false, solution1.carPooling(trips, capacity));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
trips = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2,1,5],[3,3,7]");
30+
capacity = 5;
31+
assertEquals(true, solution1.carPooling(trips, capacity));
32+
}
33+
34+
@Test
35+
public void test3() {
36+
trips = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[7,5,6],[6,7,8],[10,1,6]");
37+
capacity = 16;
38+
assertEquals(false, solution1.carPooling(trips, capacity));
39+
}
40+
}

0 commit comments

Comments
 (0)