From 42a83d45421583e66a777bb358be96c2e5ba8b29 Mon Sep 17 00:00:00 2001 From: pg06pg <44113714+pg06pg@users.noreply.github.com> Date: Thu, 28 Dec 2023 13:46:23 +0530 Subject: [PATCH 1/3] Update README_EN.md --- .../1531.String Compression II/README_EN.md | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/solution/1500-1599/1531.String Compression II/README_EN.md b/solution/1500-1599/1531.String Compression II/README_EN.md index 5bbf33a0f8225..635875addcfec 100644 --- a/solution/1500-1599/1531.String Compression II/README_EN.md +++ b/solution/1500-1599/1531.String Compression II/README_EN.md @@ -57,8 +57,53 @@ ### **Java** -```java - +```class Solution { + public int getLengthOfOptimalCompression(String s, int k) { + // dp[i][k] := the length of the optimal compression of s[i..n) with at most + // k deletion + dp = new int[s.length()][k + 1]; + Arrays.stream(dp).forEach(A -> Arrays.fill(A, kMax)); + return compression(s, 0, k); + } + + private static final int kMax = 101; + private int[][] dp; + + private int compression(final String s, int i, int k) { + if (k < 0) + return kMax; + if (i == s.length() || s.length() - i <= k) + return 0; + if (dp[i][k] != kMax) + return dp[i][k]; + + int maxFreq = 0; + int[] count = new int[128]; + + // Make letters in s[i..j] be the same. + // Keep the letter that has the maximum frequency in this range and remove + // the other letters. + for (int j = i; j < s.length(); ++j) { + maxFreq = Math.max(maxFreq, ++count[s.charAt(j)]); + dp[i][k] = Math.min( // + dp[i][k], // + getLength(maxFreq) + compression(s, j + 1, k - (j - i + 1 - maxFreq))); + } + + return dp[i][k]; + } + + // Returns the length to compress `maxFreq`. + private int getLength(int maxFreq) { + if (maxFreq == 1) + return 1; // c + if (maxFreq < 10) + return 2; // [1-9]c + if (maxFreq < 100) + return 3; // [1-9][0-9]c + return 4; // [1-9][0-9][0-9]c + } +} ``` ### **...** From 25b76cb882f9196933130bf07ab82b496a45ce43 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 28 Dec 2023 16:21:02 +0800 Subject: [PATCH 2/3] Update README_EN.md --- .../1531.String Compression II/README_EN.md | 90 ++++++++++--------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/solution/1500-1599/1531.String Compression II/README_EN.md b/solution/1500-1599/1531.String Compression II/README_EN.md index 635875addcfec..d85e0b04ea683 100644 --- a/solution/1500-1599/1531.String Compression II/README_EN.md +++ b/solution/1500-1599/1531.String Compression II/README_EN.md @@ -57,52 +57,54 @@ ### **Java** -```class Solution { - public int getLengthOfOptimalCompression(String s, int k) { - // dp[i][k] := the length of the optimal compression of s[i..n) with at most - // k deletion - dp = new int[s.length()][k + 1]; - Arrays.stream(dp).forEach(A -> Arrays.fill(A, kMax)); - return compression(s, 0, k); - } - - private static final int kMax = 101; - private int[][] dp; - - private int compression(final String s, int i, int k) { - if (k < 0) - return kMax; - if (i == s.length() || s.length() - i <= k) - return 0; - if (dp[i][k] != kMax) - return dp[i][k]; - - int maxFreq = 0; - int[] count = new int[128]; - - // Make letters in s[i..j] be the same. - // Keep the letter that has the maximum frequency in this range and remove - // the other letters. - for (int j = i; j < s.length(); ++j) { - maxFreq = Math.max(maxFreq, ++count[s.charAt(j)]); - dp[i][k] = Math.min( // - dp[i][k], // - getLength(maxFreq) + compression(s, j + 1, k - (j - i + 1 - maxFreq))); +```java +class Solution { + public int getLengthOfOptimalCompression(String s, int k) { + // dp[i][k] := the length of the optimal compression of s[i..n) with at most + // k deletion + dp = new int[s.length()][k + 1]; + Arrays.stream(dp).forEach(A -> Arrays.fill(A, K_MAX)); + return compression(s, 0, k); } - return dp[i][k]; - } - - // Returns the length to compress `maxFreq`. - private int getLength(int maxFreq) { - if (maxFreq == 1) - return 1; // c - if (maxFreq < 10) - return 2; // [1-9]c - if (maxFreq < 100) - return 3; // [1-9][0-9]c - return 4; // [1-9][0-9][0-9]c - } + private static final int K_MAX = 101; + private int[][] dp; + + private int compression(final String s, int i, int k) { + if (k < 0) { + return K_MAX; + } + if (i == s.length() || s.length() - i <= k) { + return 0; + } + if (dp[i][k] != K_MAX) { + return dp[i][k]; + } + int maxFreq = 0; + int[] count = new int[128]; + // Make letters in s[i..j] be the same. + // Keep the letter that has the maximum frequency in this range and remove + // the other letters. + for (int j = i; j < s.length(); ++j) { + maxFreq = Math.max(maxFreq, ++count[s.charAt(j)]); + dp[i][k] = Math.min(dp[i][k], getLength(maxFreq) + compression(s, j + 1, k - (j - i + 1 - maxFreq))); + } + return dp[i][k]; + } + + // Returns the length to compress `maxFreq`. + private int getLength(int maxFreq) { + if (maxFreq == 1) { + return 1; // c + } + if (maxFreq < 10) { + return 2; // [1-9]c + } + if (maxFreq < 100) { + return 3; // [1-9][0-9]c + } + return 4; // [1-9][0-9][0-9]c + } } ``` From 1bf1ec9e33aa48813bc71f4ff9628a6c04d7bb63 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 28 Dec 2023 16:21:30 +0800 Subject: [PATCH 3/3] Update README.md --- .../1531.String Compression II/README.md | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/solution/1500-1599/1531.String Compression II/README.md b/solution/1500-1599/1531.String Compression II/README.md index af6ba95e7ce17..50d0d2f9724e8 100644 --- a/solution/1500-1599/1531.String Compression II/README.md +++ b/solution/1500-1599/1531.String Compression II/README.md @@ -65,7 +65,54 @@ ```java - +class Solution { + public int getLengthOfOptimalCompression(String s, int k) { + // dp[i][k] := the length of the optimal compression of s[i..n) with at most + // k deletion + dp = new int[s.length()][k + 1]; + Arrays.stream(dp).forEach(A -> Arrays.fill(A, K_MAX)); + return compression(s, 0, k); + } + + private static final int K_MAX = 101; + private int[][] dp; + + private int compression(final String s, int i, int k) { + if (k < 0) { + return K_MAX; + } + if (i == s.length() || s.length() - i <= k) { + return 0; + } + if (dp[i][k] != K_MAX) { + return dp[i][k]; + } + int maxFreq = 0; + int[] count = new int[128]; + // Make letters in s[i..j] be the same. + // Keep the letter that has the maximum frequency in this range and remove + // the other letters. + for (int j = i; j < s.length(); ++j) { + maxFreq = Math.max(maxFreq, ++count[s.charAt(j)]); + dp[i][k] = Math.min(dp[i][k], getLength(maxFreq) + compression(s, j + 1, k - (j - i + 1 - maxFreq))); + } + return dp[i][k]; + } + + // Returns the length to compress `maxFreq`. + private int getLength(int maxFreq) { + if (maxFreq == 1) { + return 1; // c + } + if (maxFreq < 10) { + return 2; // [1-9]c + } + if (maxFreq < 100) { + return 3; // [1-9][0-9]c + } + return 4; // [1-9][0-9][0-9]c + } +} ``` ### **...**