From 14154b96020aae63e2c2351ae7021b9e61a337ac Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Mon, 5 Jul 2021 16:26:30 -0700 Subject: [PATCH] add js solution for findMaxForm --- ...4.\344\270\200\345\222\214\351\233\266.md" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index fd925f7608..6b73c08073 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -244,6 +244,35 @@ func max(a,b int) int { } ``` +Javascript: +```javascript +const findMaxForm = (strs, m, n) => { + const dp = Array.from(Array(m+1), () => Array(n+1).fill(0)); + let numOfZeros, numOfOnes; + + for(let str of strs) { + numOfZeros = 0; + numOfOnes = 0; + + for(let c of str) { + if (c === '0') { + numOfZeros++; + } else { + numOfOnes++; + } + } + + for(let i = m; i >= numOfZeros; i--) { + for(let j = n; j >= numOfOnes; j--) { + dp[i][j] = Math.max(dp[i][j], dp[i - numOfZeros][j - numOfOnes] + 1); + } + } + } + + return dp[m][n]; +}; +``` + -----------------------