Skip to content

Commit

Permalink
Create 0474-ones-and-zeroes.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored Mar 2, 2023
1 parent b269084 commit ef3b04b
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions kotlin/0474-ones-and-zeroes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Recursion with memoization solution
*/
class Solution {
fun findMaxForm(strs: Array<String>, m: Int, n: Int): Int {
val dp = Array(m + 1){ Array(n + 1){ IntArray(strs.size){ -1 } } }

fun dfs(i: Int, m: Int, n: Int): Int {
if(i == strs.size) return 0

if(dp[m][n][i] != -1) return dp[m][n][i]

val zeros = strs[i].count{ it == '0' }
val ones = strs[i].count{ it == '1' }

dp[m][n][i] = dfs(i + 1, m, n)
if(zeros <= m && ones <= n) {
dp[m][n][i] = maxOf(
dp[m][n][i],
1 + dfs(i + 1, m - zeros, n - ones)
)
}

return dp[m][n][i]
}

return dfs(0, m, n)
}
}

/*
* DP solution
*/
class Solution {
fun findMaxForm(strs: Array<String>, m: Int, n: Int): Int {
val dp = Array(m + 1){ IntArray(n + 1) }

for(str in strs) {
val zeros = str.count{ it == '0'}
val ones = str.count{ it == '1'}
for(i in m downTo zeros) {
for(j in n downTo ones) {
dp[i][j] = maxOf(
1 + dp[i - zeros][j - ones],
dp[i][j]
)
}
}
}

return dp[m][n]
}
}

0 comments on commit ef3b04b

Please sign in to comment.