1
+ // Runtime: 2419 ms (Top 5.20%) | Memory: 274.6 MB (Top 9.24%)
1
2
class Solution {
2
3
public:
3
4
int minimumFinishTime (vector<vector<int >>& tires, int changeTime, int numLaps) {
4
5
int n = tires.size ();
5
6
// to handle the cases where numLaps is small
6
- // without_change[i][j]: the total time to run j laps consecutively with tire i
7
+ // without_change[i][j]: the total time to run j laps consecutively with tire i
7
8
vector<vector<int >> without_change (n, vector<int >(20 , 2e9 ));
8
9
for (int i = 0 ; i < n; i++) {
9
10
without_change[i][1 ] = tires[i][0 ];
@@ -13,22 +14,22 @@ class Solution {
13
14
without_change[i][j] = without_change[i][j-1 ] * tires[i][1 ];
14
15
}
15
16
// since we define it as the total time, rather than just the time for the j-th lap
16
- // we have to make it prefix sum
17
+ // we have to make it prefix sum
17
18
for (int j = 2 ; j < 20 ; j++) {
18
19
if ((long long )without_change[i][j-1 ] + without_change[i][j] >= 2e9 )
19
20
break ;
20
21
without_change[i][j] += without_change[i][j-1 ];
21
22
}
22
23
}
23
-
24
- // dp[x]: the minimum time to finish x laps
24
+
25
+ // dp[x]: the minimum time to finish x laps
25
26
vector<int > dp (numLaps+1 , 2e9 );
26
27
for (int i = 0 ; i < n; i++) {
27
28
dp[1 ] = min (dp[1 ], tires[i][0 ]);
28
29
}
29
30
for (int x = 1 ; x <= numLaps; x++) {
30
31
if (x < 20 ) {
31
- // x is small enough, so an optimal solution might never changes tires!
32
+ // x is small enough, so an optimal solution might never changes tires!
32
33
for (int i = 0 ; i < n; i++) {
33
34
dp[x] = min (dp[x], without_change[i][x]);
34
35
}
@@ -37,7 +38,7 @@ class Solution {
37
38
dp[x] = min (dp[x], dp[j] + changeTime + dp[x-j]);
38
39
}
39
40
}
40
-
41
+
41
42
return dp[numLaps];
42
43
}
43
- };
44
+ };
0 commit comments