-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path474.js
91 lines (79 loc) · 2.17 KB
/
474.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
// Memoization
var findMaxForm = function(strs, m, n) {
let length =[];
for(let i = 0;i<strs.length;i++){
length[i]=[];
for(let j=0;j<=m;j++){
length[i][j]=[];
}
}
return maxForm(strs,0,m,n,length);
};
var maxForm = function(strs,id,m,n,length){
if(m<0||n<0)return 0;
if(length[id][m][n])return length[id][m][n];
let ones = 0;
let zeros = 0;
for(let i=0;i<strs[id].length;i++){
if(strs[id][i]==='0')zeros++;
else ones++;
}
if(id===strs.length-1){
length[id][m][n] = (zeros<=m&&ones<=n)?1:0;
}else{
length[id][m][n] = (zeros<=m&&ones<=n)?
Math.max(maxForm(strs,id+1,m-zeros,n-ones,length)+1,maxForm(strs,id+1,m,n,length)):maxForm(strs,id+1,m,n,length);
}
return length[id][m][n];
};
// Bottom-up
var findMaxForm = function(strs, m, n) {
let length =[];
for(let i = 0;i<=strs.length;i++){
length[i]=[];
for(let j=0;j<=m;j++){
length[i][j]=[];
for(let k=0;k<=n;k++){
length[i][j][k]=0;
}
}
}
for(let i=strs.length-1;i>=0;i--){
let ones = 0;
let zeros = 0;
for(let j=0;j<strs[i].length;j++){
if(strs[i][j]==='0')zeros++;
else ones++;
}
for (let j=0;j<=m;j++){
for (let k=0;k<=n;k++){
length[i][j][k] = (zeros<=j&&ones<=k)?Math.max(length[i+1][j-zeros][k-ones]+1,length[i+1][j][k]):length[i+1][j][k];
}
}
}
return length[0][m][n];
};
// 2D dp solution
var findMaxForm = function(strs, m, n) {
let length =[];
for(let j=0;j<=m;j++){
length[j]=[];
for(let k=0;k<=n;k++){
length[j][k]=0;
}
}
for(let i=strs.length-1;i>=0;i--){
let ones = 0;
let zeros = 0;
for(let j=0;j<strs[i].length;j++){
if(strs[i][j]==='0')zeros++;
else ones++;
}
for (let j=m;j>=zeros;j--){
for (let k=n;k>=ones;k--){
length[j][k] = Math.max(length[j-zeros][k-ones]+1,length[j][k]);
}
}
}
return length[m][n];
};