-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy path1531-string-compression-ii.js
92 lines (72 loc) · 2.25 KB
/
1531-string-compression-ii.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* DP | Recursion | Memoization
* Time O(n^4) | Space O(n^4)
* https://leetcode.com/problems/string-compression-ii/
* @param {string} s
* @param {number} k
* @return {number}
*/
function getLengthOfOptimalCompression(s, k) {
const cache = new Map();
const dfs = (i, pre, freqOfSameChar, leftK) => {
const hash = `${i},${pre},${freqOfSameChar},${leftK}`;
if (cache.has(hash)) return cache.get(hash);
if (leftK < 0) return Infinity;
if (i === s.length) return 0;
const deleteChar = dfs(i+1, pre, freqOfSameChar, leftK-1);
let keepChar;
if (s[i] === pre) {
const addOneOrZero = (freqOfSameChar === 1 || freqOfSameChar === 9 || freqOfSameChar === 99) ? 1 : 0;
keepChar = addOneOrZero + dfs(i+1, pre, freqOfSameChar + 1, leftK);
} else {
keepChar = 1 + dfs(i+1, s[i], 1, leftK);
}
cache.set(hash, Math.min(deleteChar, keepChar));
return cache.get(hash);
}
return dfs(0, -1, 0, k);
}
/**
* Brute force.
* Time O(2^n) | Space O(n)
* https://leetcode.com/problems/string-compression-ii
* @param {string} s
* @param {number} k
* @return {number}
*/
var getLengthOfOptimalCompressionBF = function(s, k) {
const runLengthEncoded = (str) => {
const encodedStr = [];
let left = 0;
let right = 1;
while(right < str.length + 1) {
while(str[left] === str[right]) {
right++;
}
const len = right - left;
if(len > 1) {
encodedStr.push(str[left], len);
} else {
encodedStr.push(str[left])
}
left = right;
right++;
}
return encodedStr.join("").length;
}
let min = Infinity;
const dfs = (i, str, deleteLeft) => {
// console.log(str)
const hash = `${i}-${deleteLeft}`;
if(i === s.length) {
min = Math.min(min, runLengthEncoded(str))
return;
}
if(deleteLeft > 0) {
return Math.min(dfs(i+1, str+s[i], deleteLeft), dfs(i+1, str, deleteLeft-1));
}
return dfs(i+1, str+s[i], deleteLeft);
}
dfs(0, "", k);
return min;
};