File tree 1 file changed +36
-0
lines changed
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } strs
3
+ * @param {number } m
4
+ * @param {number } n
5
+ * @return {number }
6
+ */
7
+
8
+ const findMaxForm = function ( strs , m , n ) {
9
+ const memo = Array . from ( new Array ( m + 1 ) , ( ) => new Array ( n + 1 ) . fill ( 0 ) )
10
+ let numZeroes ;
11
+ let numOnes ;
12
+
13
+ for ( let s of strs ) {
14
+ numZeroes = numOnes = 0 ;
15
+ // count number of zeroes and ones in current string
16
+ for ( let c of s ) {
17
+ if ( c == '0' )
18
+ numZeroes ++ ;
19
+ else if ( c == '1' )
20
+ numOnes ++ ;
21
+ }
22
+
23
+ // memo[i][j] = the max number of strings that can be formed with i 0's and j 1's
24
+ // from the first few strings up to the current string s
25
+ // Catch: have to go from bottom right to top left
26
+ // Why? If a cell in the memo is updated(because s is selected),
27
+ // we should be adding 1 to memo[i][j] from the previous iteration (when we were not considering s)
28
+ // If we go from top left to bottom right, we would be using results from this iteration => overcounting
29
+ for ( let i = m ; i >= numZeroes ; i -- ) {
30
+ for ( let j = n ; j >= numOnes ; j -- ) {
31
+ memo [ i ] [ j ] = Math . max ( memo [ i ] [ j ] , memo [ i - numZeroes ] [ j - numOnes ] + 1 ) ;
32
+ }
33
+ }
34
+ }
35
+ return memo [ m ] [ n ] ;
36
+ }
You can’t perform that action at this time.
0 commit comments