Skip to content

Commit ae4c9c2

Browse files
authored
feat: add java solution to lc problem: No.1531 (#2163)
1 parent 083b4af commit ae4c9c2

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

solution/1500-1599/1531.String Compression II/README.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,54 @@
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```java
68-
68+
class Solution {
69+
public int getLengthOfOptimalCompression(String s, int k) {
70+
// dp[i][k] := the length of the optimal compression of s[i..n) with at most
71+
// k deletion
72+
dp = new int[s.length()][k + 1];
73+
Arrays.stream(dp).forEach(A -> Arrays.fill(A, K_MAX));
74+
return compression(s, 0, k);
75+
}
76+
77+
private static final int K_MAX = 101;
78+
private int[][] dp;
79+
80+
private int compression(final String s, int i, int k) {
81+
if (k < 0) {
82+
return K_MAX;
83+
}
84+
if (i == s.length() || s.length() - i <= k) {
85+
return 0;
86+
}
87+
if (dp[i][k] != K_MAX) {
88+
return dp[i][k];
89+
}
90+
int maxFreq = 0;
91+
int[] count = new int[128];
92+
// Make letters in s[i..j] be the same.
93+
// Keep the letter that has the maximum frequency in this range and remove
94+
// the other letters.
95+
for (int j = i; j < s.length(); ++j) {
96+
maxFreq = Math.max(maxFreq, ++count[s.charAt(j)]);
97+
dp[i][k] = Math.min(dp[i][k], getLength(maxFreq) + compression(s, j + 1, k - (j - i + 1 - maxFreq)));
98+
}
99+
return dp[i][k];
100+
}
101+
102+
// Returns the length to compress `maxFreq`.
103+
private int getLength(int maxFreq) {
104+
if (maxFreq == 1) {
105+
return 1; // c
106+
}
107+
if (maxFreq < 10) {
108+
return 2; // [1-9]c
109+
}
110+
if (maxFreq < 100) {
111+
return 3; // [1-9][0-9]c
112+
}
113+
return 4; // [1-9][0-9][0-9]c
114+
}
115+
}
69116
```
70117

71118
### **...**

solution/1500-1599/1531.String Compression II/README_EN.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,54 @@
5858
### **Java**
5959

6060
```java
61-
61+
class Solution {
62+
public int getLengthOfOptimalCompression(String s, int k) {
63+
// dp[i][k] := the length of the optimal compression of s[i..n) with at most
64+
// k deletion
65+
dp = new int[s.length()][k + 1];
66+
Arrays.stream(dp).forEach(A -> Arrays.fill(A, K_MAX));
67+
return compression(s, 0, k);
68+
}
69+
70+
private static final int K_MAX = 101;
71+
private int[][] dp;
72+
73+
private int compression(final String s, int i, int k) {
74+
if (k < 0) {
75+
return K_MAX;
76+
}
77+
if (i == s.length() || s.length() - i <= k) {
78+
return 0;
79+
}
80+
if (dp[i][k] != K_MAX) {
81+
return dp[i][k];
82+
}
83+
int maxFreq = 0;
84+
int[] count = new int[128];
85+
// Make letters in s[i..j] be the same.
86+
// Keep the letter that has the maximum frequency in this range and remove
87+
// the other letters.
88+
for (int j = i; j < s.length(); ++j) {
89+
maxFreq = Math.max(maxFreq, ++count[s.charAt(j)]);
90+
dp[i][k] = Math.min(dp[i][k], getLength(maxFreq) + compression(s, j + 1, k - (j - i + 1 - maxFreq)));
91+
}
92+
return dp[i][k];
93+
}
94+
95+
// Returns the length to compress `maxFreq`.
96+
private int getLength(int maxFreq) {
97+
if (maxFreq == 1) {
98+
return 1; // c
99+
}
100+
if (maxFreq < 10) {
101+
return 2; // [1-9]c
102+
}
103+
if (maxFreq < 100) {
104+
return 3; // [1-9][0-9]c
105+
}
106+
return 4; // [1-9][0-9][0-9]c
107+
}
108+
}
62109
```
63110

64111
### **...**

0 commit comments

Comments
 (0)