Skip to content

Commit 505f764

Browse files
authored
Create 2547-minimum-cost-to-split-an-array.js
1 parent a7e6636 commit 505f764

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const minCost = function (nums, k) {
7+
const n = nums.length,
8+
max = Math.max(...nums),
9+
dp = Array(n + 1).fill(Number.MAX_SAFE_INTEGER)
10+
dp[0] = 0
11+
for (let i = 0; i < n; i++) {
12+
let f = Array(max + 1).fill(0),
13+
cost = 0
14+
for (let j = i; j < n; j++) {
15+
f[nums[j]]++
16+
if (f[nums[j]] == 2) {
17+
cost += 2
18+
} else if (f[nums[j]] > 2) {
19+
cost++
20+
}
21+
dp[j + 1] = Math.min(dp[i] + cost + k, dp[j + 1])
22+
}
23+
}
24+
return dp[n]
25+
}

0 commit comments

Comments
 (0)