Skip to content

Commit 45e85f8

Browse files
committed
add 817 java
1 parent f283417 commit 45e85f8

File tree

4 files changed

+114
-10
lines changed

4 files changed

+114
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,8 @@
10921092
823 | [Binary Trees With Factors](https://leetcode.com/problems/binary-trees-with-factors/) | [Python](./leetcode_python/Dynamic_Programming/binary-trees-with-factors.py) | _O(n^2)_ | _O(n)_ | Medium |`dp`| AGAIN (not start)
10931093
837 | [New 21 Game](https://leetcode.com/problems/new-21-game/) | [Python](./leetcode_python//Dynamic_Programming/new-21-game.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/New21Game.java) | _O(n)_ | _O(n)_ | Medium |`dp`, trick, math, `google`| AGAIN (2) (not start)
10941094
838 | [Push Dominoes](https://leetcode.com/problems/push-dominoes/) | [Python](./leetcode_python/Dynamic_Programming/push-dominoes.py) | _O(n)_ | _O(n)_ | Medium |`dp`,`two pointers`,`google`| AGAIN (not start)
1095+
871 | [Minimum Number of Refueling Stops](https://leetcode.com/problems/minimum-number-of-refueling-stops/editorial/) | [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MinimumNumberOfRefuelingStops.java)
1096+
| _O(n)_ | _O(n)_ | Medium |`dp`,`heap`,`google`| AGAIN (not start)
10951097
877 | [Stone Game](https://leetcode.com/problems/stone-game/) | [Python](./leetcode_python/Dynamic_Programming/stone-game.py) | _O(n^2)_ | _O(n)_ | Medium | variant of [Predict the Winner](https://leetcode.com/problems/predict-the-winner/) | AGAIN (not start)
10961098
926| [Flip String to Monotone Increasing](https://leetcode.com/problems/flip-string-to-monotone-increasing/) | [Python](./leetcode_python/Dynamic_Programming/flip-string-to-monotone-increasing.py) | _O(n)_ | _O(1)_ | Medium |good trick, dp, prefix sum, `google`, `amazon`| AGAIN********** (4)
10971099
931| [Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/) | [Python](./leetcode_python/Dynamic_Programming/minimum-falling-path-sum.py) | _O(n^2)_ | _O(1)_ | Medium |`google`, `goldman sachs`| AGAIN (not start)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241220: 815
1+
20241220: 815,871
22
20241215: 1109
33
20241214: 560,523
44
20241208: 304,853,325

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
2025-02-13 -> ['815']
1+
2025-02-13 -> ['815,871']
22
2025-02-08 -> ['1109']
33
2025-02-07 -> ['560,523']
44
2025-02-01 -> ['304,853,325']
55
2025-01-26 -> ['370(todo)']
66
2025-01-24 -> ['34,767']
7-
2025-01-23 -> ['815']
7+
2025-01-23 -> ['815,871']
88
2025-01-20 -> ['722,380']
99
2025-01-19 -> ['33,81']
1010
2025-01-18 -> ['1109']
@@ -13,21 +13,21 @@
1313
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
1414
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1515
2025-01-11 -> ['304,853,325', '394']
16-
2025-01-10 -> ['815', '833,950']
16+
2025-01-10 -> ['815,871', '833,950']
1717
2025-01-05 -> ['1109', '370(todo)']
1818
2025-01-04 -> ['560,523', '53,210,207']
1919
2025-01-03 -> ['34,767', '444']
20-
2025-01-02 -> ['815', '1188,130,855(again)']
20+
2025-01-02 -> ['815,871', '1188,130,855(again)']
2121
2024-12-30 -> ['722,380']
2222
2024-12-29 -> ['304,853,325', '33,81']
23-
2024-12-28 -> ['815', '1109', '900']
23+
2024-12-28 -> ['815,871', '1109', '900']
2424
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2525
2024-12-26 -> ['776,31']
26-
2024-12-25 -> ['815', '004(todo),34(todo),162(todo),275(todo)']
26+
2024-12-25 -> ['815,871', '004(todo),34(todo),162(todo),275(todo)']
2727
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
28-
2024-12-23 -> ['815', '1109', '370(todo)']
29-
2024-12-22 -> ['815', '560,523']
30-
2024-12-21 -> ['815', '304,853,325', '34,767', '394', '855,846']
28+
2024-12-23 -> ['815,871', '1109', '370(todo)']
29+
2024-12-22 -> ['815,871', '560,523']
30+
2024-12-21 -> ['815,871', '304,853,325', '34,767', '394', '855,846']
3131
2024-12-20 -> ['1109', '833,950', '932']
3232
2024-12-19 -> ['560,523']
3333
2024-12-18 -> ['1109', '951,792']
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package LeetCodeJava.DynamicProgramming;
2+
3+
// https://leetcode.com/problems/minimum-number-of-refueling-stops/description/
4+
5+
import java.util.Collections;
6+
import java.util.PriorityQueue;
7+
8+
/**
9+
* 871. Minimum Number of Refueling Stops Hard Topics Companies A car travels from a starting
10+
* position to a destination which is target miles east of the starting position.
11+
*
12+
* <p>There are gas stations along the way. The gas stations are represented as an array stations
13+
* where stations[i] = [positioni, fueli] indicates that the ith gas station is positioni miles east
14+
* of the starting position and has fueli liters of gas.
15+
*
16+
* <p>The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in
17+
* it. It uses one liter of gas per one mile that it drives. When the car reaches a gas station, it
18+
* may stop and refuel, transferring all the gas from the station into the car.
19+
*
20+
* <p>Return the minimum number of refueling stops the car must make in order to reach its
21+
* destination. If it cannot reach the destination, return -1.
22+
*
23+
* <p>Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.
24+
* If the car reaches the destination with 0 fuel left, it is still considered to have arrived.
25+
*
26+
* <p>Example 1:
27+
*
28+
* <p>Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target
29+
* without refueling. Example 2:
30+
*
31+
* <p>Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can not
32+
* reach the target (or even the first gas station). Example 3:
33+
*
34+
* <p>Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2
35+
* Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of
36+
* fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60
37+
* (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to
38+
* and reach the target. We made 2 refueling stops along the way, so we return 2.
39+
*
40+
* <p>Constraints:
41+
*
42+
* <p>1 <= target, startFuel <= 109 0 <= stations.length <= 500 1 <= positioni < positioni+1 <
43+
* target 1 <= fueli < 109
44+
*/
45+
public class MinimumNumberOfRefuelingStops {
46+
47+
// V0
48+
// TODO : implement
49+
// public int minRefuelStops(int target, int startFuel, int[][] stations) {
50+
//
51+
// }
52+
53+
// V1
54+
// IDEA : DP
55+
// https://leetcode.com/problems/minimum-number-of-refueling-stops/editorial/
56+
public int minRefuelStops_1(int target, int startFuel, int[][] stations) {
57+
int N = stations.length;
58+
long[] dp = new long[N + 1];
59+
dp[0] = startFuel;
60+
for (int i = 0; i < N; ++i)
61+
for (int t = i; t >= 0; --t)
62+
if (dp[t] >= stations[i][0]) dp[t + 1] = Math.max(dp[t + 1], dp[t] + (long) stations[i][1]);
63+
64+
for (int i = 0; i <= N; ++i) if (dp[i] >= target) return i;
65+
return -1;
66+
}
67+
68+
// V2
69+
// IDEA : HEAP
70+
// https://leetcode.com/problems/minimum-number-of-refueling-stops/editorial/
71+
public int minRefuelStops(int target, int tank, int[][] stations) {
72+
// pq is a maxheap of gas station capacities
73+
PriorityQueue<Integer> pq = new PriorityQueue(Collections.reverseOrder());
74+
int ans = 0, prev = 0;
75+
for (int[] station : stations) {
76+
int location = station[0];
77+
int capacity = station[1];
78+
tank -= location - prev;
79+
while (!pq.isEmpty() && tank < 0) { // must refuel in past
80+
tank += pq.poll();
81+
ans++;
82+
}
83+
84+
if (tank < 0) return -1;
85+
pq.offer(capacity);
86+
prev = location;
87+
}
88+
89+
// Repeat body for station = (target, inf)
90+
{
91+
tank -= target - prev;
92+
while (!pq.isEmpty() && tank < 0) {
93+
tank += pq.poll();
94+
ans++;
95+
}
96+
if (tank < 0) return -1;
97+
}
98+
99+
return ans;
100+
}
101+
102+
}

0 commit comments

Comments
 (0)