Skip to content

Commit a880f1c

Browse files
committed
Add solution #1531
1 parent e38737d commit a880f1c

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,350 LeetCode solutions in JavaScript
1+
# 1,351 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1170,6 +1170,7 @@
11701170
1528|[Shuffle String](./solutions/1528-shuffle-string.js)|Easy|
11711171
1529|[Minimum Suffix Flips](./solutions/1529-minimum-suffix-flips.js)|Medium|
11721172
1530|[Number of Good Leaf Nodes Pairs](./solutions/1530-number-of-good-leaf-nodes-pairs.js)|Medium|
1173+
1531|[String Compression II](./solutions/1531-string-compression-ii.js)|Hard|
11731174
1534|[Count Good Triplets](./solutions/1534-count-good-triplets.js)|Easy|
11741175
1535|[Find the Winner of an Array Game](./solutions/1535-find-the-winner-of-an-array-game.js)|Medium|
11751176
1550|[Three Consecutive Odds](./solutions/1550-three-consecutive-odds.js)|Easy|
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* 1531. String Compression II
3+
* https://leetcode.com/problems/string-compression-ii/
4+
* Difficulty: Hard
5+
*
6+
* Run-length encoding is a string compression method that works by replacing consecutive
7+
* identical characters (repeated 2 or more times) with the concatenation of the character
8+
* and the number marking the count of the characters (length of the run). For example,
9+
* to compress the string "aabccc" we replace "aa" by "a2" and replace "ccc" by "c3". Thus
10+
* the compressed string becomes "a2bc3".
11+
*
12+
* Notice that in this problem, we are not adding '1' after single characters.
13+
*
14+
* Given a string s and an integer k. You need to delete at most k characters from s such that
15+
* the run-length encoded version of s has minimum length.
16+
*
17+
* Find the minimum length of the run-length encoded version of s after deleting at most
18+
* k characters.
19+
*/
20+
21+
/**
22+
* @param {string} s
23+
* @param {number} k
24+
* @return {number}
25+
*/
26+
var getLengthOfOptimalCompression = function(s, k) {
27+
const n = s.length;
28+
const dp = new Array(n + 1).fill().map(() => new Array(k + 1).fill(9999));
29+
dp[0][0] = 0;
30+
31+
for (let i = 1; i <= n; i++) {
32+
for (let j = 0; j <= k; j++) {
33+
let count = 0;
34+
let deletions = 0;
35+
36+
for (let m = i; m >= 1; m--) {
37+
if (s[m - 1] === s[i - 1]) count++;
38+
else deletions++;
39+
40+
if (j - deletions >= 0) {
41+
dp[i][j] = Math.min(
42+
dp[i][j],
43+
dp[m - 1][j - deletions] + calculateEncodedLength(count)
44+
);
45+
}
46+
}
47+
48+
if (j > 0) {
49+
dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1]);
50+
}
51+
}
52+
}
53+
54+
return dp[n][k];
55+
56+
function calculateEncodedLength(count) {
57+
if (count === 0) return 0;
58+
if (count === 1) return 1;
59+
if (count < 10) return 2;
60+
if (count < 100) return 3;
61+
return 4;
62+
}
63+
};

0 commit comments

Comments
 (0)