-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution871.cs
39 lines (34 loc) · 937 Bytes
/
Solution871.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution871
{
/// <summary>
/// 871. Minimum Number of Refueling Stops - Hard
/// <a href="https://leetcode.com/problems/minimum-number-of-refueling-stops">See the problem</a>
/// </summary>
public int MinRefuelStops(int target, int startFuel, int[][] stations)
{
var n = stations.Length;
var 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] + stations[i][1]);
}
}
}
for (int i = 0; i <= n; i++)
{
if (dp[i] >= target)
{
return i;
}
}
return -1;
}
}