File tree 2 files changed +96
-2
lines changed
solution/1500-1599/1531.String Compression II
2 files changed +96
-2
lines changed Original file line number Diff line number Diff line change 65
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
66
67
67
``` 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
+ }
69
116
```
70
117
71
118
### ** ...**
Original file line number Diff line number Diff line change 58
58
### ** Java**
59
59
60
60
``` 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
+ }
62
109
```
63
110
64
111
### ** ...**
You can’t perform that action at this time.
0 commit comments