-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0474.ones-and-zeroes.scala
More file actions
28 lines (24 loc) · 898 Bytes
/
Copy path0474.ones-and-zeroes.scala
File metadata and controls
28 lines (24 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package leet.`0474`
object Solution:
def findMaxForm(strs: Array[String], M: Int, N: Int): Int =
val dp = Array.ofDim[Int](M + 1, N + 1)
val nums = strs.iterator.map: x =>
val zeros = x.count(_ == '0')
(zeros, x.size - zeros)
for
(z, o) <- nums
i <- M to z by -1
j <- N to o by -1
do dp(i)(j) = dp(i)(j) max (dp(i - z)(j - o) + 1)
dp(M)(N)
import munit.FunSuite
class Suite extends FunSuite:
import Solution.*
import upickle.default.*
List(
(strs = """["10","0001","111001","1","0"]""", m = 5, n = 3, expected = 4),
(strs = """["10","0","1"]""", m = 1, n = 1, expected = 2),
).foreach { case (strs, m, n, expected) =>
test(s"findMaxForm($strs, $m, $n) = $expected"):
assertEquals(findMaxForm(read[Array[String]](strs), m, n), expected)
}