Skip to content

Commit 29e9bb9

Browse files
authored
2022-08-20 update: added "Minimum Number of Refueling Stops" (#72)
1 parent a171ddf commit 29e9bb9

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Soon.
3333
| 239. Sliding Window Maximum | [Link](https://leetcode.com/problems/sliding-window-maximum/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/SlidingWindowMaximum.java) |
3434
| 297. Serialize and Deserialize Binary Tree | [Link](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/SerializeAndDeserializeBinaryTree.java) |
3535
| 458. Poor Pigs | [Link](https://leetcode.com/problems/poor-pigs/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/PoorPigs.java) |
36+
| 871. Minimum Number of Refueling Stops | [Link](https://leetcode.com/problems/minimum-number-of-refueling-stops/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/MinimumNumberOfRefuelingStops.java) |
3637
| 1106. Parsing A Boolean Expression | [Link](https://leetcode.com/problems/parsing-a-boolean-expression/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/ParsingABooleanExpression.java) |
3738
| 1220. Count Vowels Permutation | [Link](https://leetcode.com/problems/count-vowels-permutation/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/CountVowelsPermutation.java) |
3839
| 1944. Number of Visible People in a Queue | [Link](https://leetcode.com/problems/number-of-visible-people-in-a-queue/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/NumberOfVisiblePeopleInAQueue.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.smlnskgmail.jaman.leetcodejava.hard;
2+
3+
// https://leetcode.com/problems/minimum-number-of-refueling-stops/
4+
public class MinimumNumberOfRefuelingStops {
5+
6+
private final int target;
7+
private final int startFuel;
8+
private final int[][] stations;
9+
10+
public MinimumNumberOfRefuelingStops(int target, int startFuel, int[][] stations) {
11+
this.target = target;
12+
this.startFuel = startFuel;
13+
this.stations = stations;
14+
}
15+
16+
public int solution() {
17+
int n = stations.length;
18+
long[] dp = new long[n + 1];
19+
dp[0] = startFuel;
20+
for (int i = 0; i < n; i++) {
21+
for (int j = i; j >= 0; j--) {
22+
if (dp[j] >= stations[i][0]) {
23+
dp[j + 1] = Math.max(dp[j + 1], dp[j] + (long) stations[i][1]);
24+
}
25+
}
26+
}
27+
for (int i = 0; i <= n; i++) {
28+
if (dp[i] >= target) {
29+
return i;
30+
}
31+
}
32+
return -1;
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.smlnskgmail.jaman.leetcodejava.hard;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class MinimumNumberOfRefuelingStopsTest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertEquals(
12+
0,
13+
new MinimumNumberOfRefuelingStops(1, 1, new int[][]{}).solution()
14+
);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)