@@ -83,48 +83,6 @@ class A {
8383 }
8484}
8585
86- class B {
87- int len (int most) {
88- if (most == 0 ) return 0 ;
89- if (most == 1 ) return 1 ;
90- if (most < 10 ) return 2 ;
91- if (most < 100 ) return 3 ;
92- return 4 ;
93- }
94-
95- int getLengthOfOptimalCompression (String s, int k) {
96- List <String > chars = s.split ("" );
97- List <List <int >> dp = List .filled (chars.length + 1 , 0 )
98- .map (
99- (e) => List .filled (k + 1 , 0 ),
100- )
101- .toList ();
102- int dfs (List <String > chars, int idx, int k, List <List <int >> dp) {
103- if (k < 0 ) return 101 ;
104- if (dp[idx][k] > 0 ) {
105- // if there is no more than k chars left, delete all
106- if (idx + k >= chars.length) {
107- dp[idx][k] = 0 ;
108- } else {
109- List <int > cnt = List .filled (128 , 0 );
110- int most = 0 , best = 101 ;
111- for (int j = idx; j < chars.length; j++ ) {
112- cnt[chars[j].codeUnitAt (0 )]++ ;
113- most = max (most, cnt[chars[j].codeUnitAt (0 )]);
114- // delete [idx,j] but keep chars with most counts
115- best = min (best,
116- len (most) + dfs (chars, j + 1 , k - j + idx - 1 + most, dp));
117- }
118- dp[idx][k] = best;
119- }
120- }
121- return dp[idx][k];
122- }
123-
124- return dfs (chars, 0 , k, dp);
125- }
126- }
127-
12886class C {
12987 int getLengthOfOptimalCompression (String s, int k) {
13088 int n = s.length;
@@ -172,7 +130,9 @@ class C {
172130 }
173131}
174132
175- class D {
133+ class Solution {
134+ // Runtime: 534 ms, faster than 100.00% of Dart online submissions for String Compression II.
135+ // Memory Usage: 161.9 MB, less than 100.00% of Dart online submissions for String Compression II.
176136 int n = 127 ;
177137 late List <List <int >> dp;
178138 int getLen (int x) {
@@ -210,9 +170,8 @@ class D {
210170 }
211171
212172 int getLengthOfOptimalCompression (String s, int k) {
213- //dp = vector<vector<int>>(N,vector<int>(N,-1));
214- // dp = List.filled(n, 0).map((e) => List.filled(n, -1)).toList();
215- dp = List .filled (n, List .filled (n, - 1 ));
173+ dp = List .filled (n, 0 ).map ((e) => List .filled (n, - 1 )).toList ();
174+
216175 return helper (s, 0 , k);
217176 }
218177}
@@ -311,11 +270,11 @@ class E {
311270// int getLengthOfOptimalCompression(String s, int k) {
312271
313272// }
314- class Solution {
273+ class M {
315274 int getLengthOfOptimalCompression (String s, int k) {
316275 // dp[i][k] := length of optimal compression of s[i:] w/ at most k deletion
317276 // dp.resize(s.length, List<int>(k + 1, kMax));
318- dp = List .filled (s.length, List .filled (k + 1 , kMax));
277+ dp = List .filled (s.length, 0 ). map ((e) => List .filled (k + 1 , kMax)). toList ( );
319278 return compression (s, 0 , k);
320279 }
321280
@@ -333,7 +292,7 @@ class Solution {
333292 // Make chars in s[i..j] be same
334293 // Keep the char that has max freq in this range and remove other chars
335294 for (int j = i; j < s.length; ++ j) {
336- maxFreq = max (maxFreq, ++ count[s.codeUnitAt (j)] + 1 );
295+ maxFreq = max (maxFreq, ++ count[s.codeUnitAt (j)]);
337296 dp[i][k] = min (
338297 dp[i][k],
339298 getLength (maxFreq) +
0 commit comments