File tree Expand file tree Collapse file tree 2 files changed +11
-11
lines changed
solution/3500-3599/3500.Minimum Cost to Divide Array Into Subarrays Expand file tree Collapse file tree 2 files changed +11
-11
lines changed Original file line number Diff line number Diff line change @@ -98,7 +98,7 @@ class Solution:
98
98
n = len (nums)
99
99
sumN = [0 ] * (n + 1 )
100
100
sumC = [0 ] * (n + 1 )
101
-
101
+
102
102
for i in range (1 , n + 1 ):
103
103
sumN[i] = sumN[i - 1 ] + nums[i - 1 ]
104
104
sumC[i] = sumC[i - 1 ] + cost[i - 1 ]
@@ -183,7 +183,7 @@ class Solution {
183
183
if (start == n) return 0 ;
184
184
return Long . MAX_VALUE ;
185
185
}
186
-
186
+
187
187
if (dp[start][end] != null ) return dp[start][end];
188
188
189
189
long currentNumsSum = prefixNums[end], currentCostSum = prefixCosts[n - 1 ];
@@ -212,14 +212,14 @@ class Solution {
212
212
public:
213
213
long long minimumCost(vector<int >& nums, vector<int >& cost, int K) {
214
214
int n = nums.size();
215
-
215
+
216
216
long long sn[n + 1], sc[n + 1];
217
217
sn[0] = sc[0] = 0;
218
218
for (int i = 1; i <= n; i++) {
219
219
sn[i] = sn[i - 1] + nums[i - 1];
220
220
sc[i] = sc[i - 1] + cost[i - 1];
221
221
}
222
-
222
+
223
223
const long long INF = 1e18 ;
224
224
long long f[n + 1 ];
225
225
for (int i = 0 ; i <= n; i++) f[i] = INF;
@@ -228,11 +228,11 @@ class Solution {
228
228
long long t = sn[i] * (sc[i] - sc[j]) + K * (sc[n] - sc[j]);
229
229
f[i] = min(f[i], f[j] + t);
230
230
}
231
-
231
+
232
232
return f[n];
233
233
}
234
234
};
235
-
235
+
236
236
```
237
237
238
238
#### Go
Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ class Solution:
93
93
n = len (nums)
94
94
sumN = [0 ] * (n + 1 )
95
95
sumC = [0 ] * (n + 1 )
96
-
96
+
97
97
for i in range (1 , n + 1 ):
98
98
sumN[i] = sumN[i - 1 ] + nums[i - 1 ]
99
99
sumC[i] = sumC[i - 1 ] + cost[i - 1 ]
@@ -178,7 +178,7 @@ class Solution {
178
178
if (start == n) return 0 ;
179
179
return Long . MAX_VALUE ;
180
180
}
181
-
181
+
182
182
if (dp[start][end] != null ) return dp[start][end];
183
183
184
184
long currentNumsSum = prefixNums[end], currentCostSum = prefixCosts[n - 1 ];
@@ -207,14 +207,14 @@ class Solution {
207
207
public:
208
208
long long minimumCost(vector<int >& nums, vector<int >& cost, int K) {
209
209
int n = nums.size();
210
-
210
+
211
211
long long sn[n + 1], sc[n + 1];
212
212
sn[0] = sc[0] = 0;
213
213
for (int i = 1; i <= n; i++) {
214
214
sn[i] = sn[i - 1] + nums[i - 1];
215
215
sc[i] = sc[i - 1] + cost[i - 1];
216
216
}
217
-
217
+
218
218
const long long INF = 1e18 ;
219
219
long long f[n + 1 ];
220
220
for (int i = 0 ; i <= n; i++) f[i] = INF;
@@ -223,7 +223,7 @@ class Solution {
223
223
long long t = sn[i] * (sc[i] - sc[j]) + K * (sc[n] - sc[j]);
224
224
f[i] = min(f[i], f[j] + t);
225
225
}
226
-
226
+
227
227
return f[n];
228
228
}
229
229
};
You can’t perform that action at this time.
0 commit comments