Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef3b04b

Browse files
authored
Create 0474-ones-and-zeroes.kt
1 parentb269084 commitef3b04b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

‎kotlin/0474-ones-and-zeroes.kt‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Recursion with memoization solution
3+
*/
4+
classSolution {
5+
funfindMaxForm(strs:Array<String>,m:Int,n:Int):Int {
6+
val dp=Array(m+1){Array(n+1){IntArray(strs.size){-1 } } }
7+
8+
fundfs(i:Int,m:Int,n:Int):Int {
9+
if(i== strs.size)return0
10+
11+
if(dp[m][n][i]!=-1)return dp[m][n][i]
12+
13+
val zeros= strs[i].count{ it=='0' }
14+
val ones= strs[i].count{ it=='1' }
15+
16+
dp[m][n][i]= dfs(i+1, m, n)
17+
if(zeros<= m&& ones<= n) {
18+
dp[m][n][i]= maxOf(
19+
dp[m][n][i],
20+
1+ dfs(i+1, m- zeros, n- ones)
21+
)
22+
}
23+
24+
return dp[m][n][i]
25+
}
26+
27+
return dfs(0, m, n)
28+
}
29+
}
30+
31+
/*
32+
* DP solution
33+
*/
34+
classSolution {
35+
funfindMaxForm(strs:Array<String>,m:Int,n:Int):Int {
36+
val dp=Array(m+1){IntArray(n+1) }
37+
38+
for(strin strs) {
39+
val zeros= str.count{ it=='0'}
40+
val ones= str.count{ it=='1'}
41+
for(iin m downTo zeros) {
42+
for(jin n downTo ones) {
43+
dp[i][j]= maxOf(
44+
1+ dp[i- zeros][j- ones],
45+
dp[i][j]
46+
)
47+
}
48+
}
49+
}
50+
51+
return dp[m][n]
52+
}
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp