Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit79deb72

Browse files
add 1094
1 parent0119905 commit79deb72

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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||
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
importjava.util.PriorityQueue;
5+
6+
publicclass_1094 {
7+
publicstaticclassSolution1 {
8+
publicbooleancarPooling(int[][]trips,intcapacity) {
9+
Arrays.sort(trips, (a,b) ->a[1] -b[1]);
10+
PriorityQueue<int[]>heap =newPriorityQueue<>((a,b) ->a[1] -b[1]);
11+
for (int[]trip :trips) {
12+
intstartTime =trip[1];
13+
intendTime =trip[2];
14+
while (!heap.isEmpty() &&heap.peek()[1] <=startTime) {
15+
int[]curr =heap.poll();
16+
capacity +=curr[0];
17+
}
18+
intpeopleCnt =trip[0];
19+
capacity -=peopleCnt;
20+
if (capacity <0) {
21+
returnfalse;
22+
}
23+
heap.offer(newint[]{peopleCnt,endTime});
24+
}
25+
returntrue;
26+
}
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._1094;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_1094Test {
11+
privatestatic_1094.Solution1solution1;
12+
privatestaticint[][]trips;
13+
privatestaticintcapacity;
14+
15+
@BeforeClass
16+
publicstaticvoidsetup() {
17+
solution1 =new_1094.Solution1();
18+
}
19+
20+
@Test
21+
publicvoidtest1() {
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+
publicvoidtest2() {
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+
publicvoidtest3() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp