Skip to content

Commit

Permalink
add 817 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 20, 2024
1 parent f283417 commit 45e85f8
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,8 @@
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)
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)
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)
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)
| _O(n)_ | _O(n)_ | Medium |`dp`,`heap`,`google`| AGAIN (not start)
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)
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)
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)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20241220: 815
20241220: 815,871
20241215: 1109
20241214: 560,523
20241208: 304,853,325
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
2025-02-13 -> ['815']
2025-02-13 -> ['815,871']
2025-02-08 -> ['1109']
2025-02-07 -> ['560,523']
2025-02-01 -> ['304,853,325']
2025-01-26 -> ['370(todo)']
2025-01-24 -> ['34,767']
2025-01-23 -> ['815']
2025-01-23 -> ['815,871']
2025-01-20 -> ['722,380']
2025-01-19 -> ['33,81']
2025-01-18 -> ['1109']
Expand All @@ -13,21 +13,21 @@
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['304,853,325', '394']
2025-01-10 -> ['815', '833,950']
2025-01-10 -> ['815,871', '833,950']
2025-01-05 -> ['1109', '370(todo)']
2025-01-04 -> ['560,523', '53,210,207']
2025-01-03 -> ['34,767', '444']
2025-01-02 -> ['815', '1188,130,855(again)']
2025-01-02 -> ['815,871', '1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-29 -> ['304,853,325', '33,81']
2024-12-28 -> ['815', '1109', '900']
2024-12-28 -> ['815,871', '1109', '900']
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['815', '004(todo),34(todo),162(todo),275(todo)']
2024-12-25 -> ['815,871', '004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-23 -> ['815', '1109', '370(todo)']
2024-12-22 -> ['815', '560,523']
2024-12-21 -> ['815', '304,853,325', '34,767', '394', '855,846']
2024-12-23 -> ['815,871', '1109', '370(todo)']
2024-12-22 -> ['815,871', '560,523']
2024-12-21 -> ['815,871', '304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['1109', '833,950', '932']
2024-12-19 -> ['560,523']
2024-12-18 -> ['1109', '951,792']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package LeetCodeJava.DynamicProgramming;

// https://leetcode.com/problems/minimum-number-of-refueling-stops/description/

import java.util.Collections;
import java.util.PriorityQueue;

/**
* 871. Minimum Number of Refueling Stops Hard Topics Companies A car travels from a starting
* position to a destination which is target miles east of the starting position.
*
* <p>There are gas stations along the way. The gas stations are represented as an array stations
* where stations[i] = [positioni, fueli] indicates that the ith gas station is positioni miles east
* of the starting position and has fueli liters of gas.
*
* <p>The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in
* it. It uses one liter of gas per one mile that it drives. When the car reaches a gas station, it
* may stop and refuel, transferring all the gas from the station into the car.
*
* <p>Return the minimum number of refueling stops the car must make in order to reach its
* destination. If it cannot reach the destination, return -1.
*
* <p>Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.
* If the car reaches the destination with 0 fuel left, it is still considered to have arrived.
*
* <p>Example 1:
*
* <p>Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target
* without refueling. Example 2:
*
* <p>Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can not
* reach the target (or even the first gas station). Example 3:
*
* <p>Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2
* Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of
* fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60
* (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to
* and reach the target. We made 2 refueling stops along the way, so we return 2.
*
* <p>Constraints:
*
* <p>1 <= target, startFuel <= 109 0 <= stations.length <= 500 1 <= positioni < positioni+1 <
* target 1 <= fueli < 109
*/
public class MinimumNumberOfRefuelingStops {

// V0
// TODO : implement
// public int minRefuelStops(int target, int startFuel, int[][] stations) {
//
// }

// V1
// IDEA : DP
// https://leetcode.com/problems/minimum-number-of-refueling-stops/editorial/
public int minRefuelStops_1(int target, int startFuel, int[][] stations) {
int N = stations.length;
long[] dp = new long[N + 1];
dp[0] = startFuel;
for (int i = 0; i < N; ++i)
for (int t = i; t >= 0; --t)
if (dp[t] >= stations[i][0]) dp[t + 1] = Math.max(dp[t + 1], dp[t] + (long) stations[i][1]);

for (int i = 0; i <= N; ++i) if (dp[i] >= target) return i;
return -1;
}

// V2
// IDEA : HEAP
// https://leetcode.com/problems/minimum-number-of-refueling-stops/editorial/
public int minRefuelStops(int target, int tank, int[][] stations) {
// pq is a maxheap of gas station capacities
PriorityQueue<Integer> pq = new PriorityQueue(Collections.reverseOrder());
int ans = 0, prev = 0;
for (int[] station : stations) {
int location = station[0];
int capacity = station[1];
tank -= location - prev;
while (!pq.isEmpty() && tank < 0) { // must refuel in past
tank += pq.poll();
ans++;
}

if (tank < 0) return -1;
pq.offer(capacity);
prev = location;
}

// Repeat body for station = (target, inf)
{
tank -= target - prev;
while (!pq.isEmpty() && tank < 0) {
tank += pq.poll();
ans++;
}
if (tank < 0) return -1;
}

return ans;
}

}

0 comments on commit 45e85f8

Please sign in to comment.