Skip to content

Commit 14154b9

Browse files
authored
add js solution for findMaxForm
1 parent 1da6ff7 commit 14154b9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

problems/0474.一和零.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,35 @@ func max(a,b int) int {
244244
}
245245
```
246246

247+
Javascript:
248+
```javascript
249+
const findMaxForm = (strs, m, n) => {
250+
const dp = Array.from(Array(m+1), () => Array(n+1).fill(0));
251+
let numOfZeros, numOfOnes;
252+
253+
for(let str of strs) {
254+
numOfZeros = 0;
255+
numOfOnes = 0;
256+
257+
for(let c of str) {
258+
if (c === '0') {
259+
numOfZeros++;
260+
} else {
261+
numOfOnes++;
262+
}
263+
}
264+
265+
for(let i = m; i >= numOfZeros; i--) {
266+
for(let j = n; j >= numOfOnes; j--) {
267+
dp[i][j] = Math.max(dp[i][j], dp[i - numOfZeros][j - numOfOnes] + 1);
268+
}
269+
}
270+
}
271+
272+
return dp[m][n];
273+
};
274+
```
275+
247276

248277

249278
-----------------------

0 commit comments

Comments
 (0)