当前位置:网站首页>算法---一和零(Kotlin)
算法---一和零(Kotlin)
2022-08-05 04:38:00 【小米科技Android 研发曹新雨】
题目
给你一个二进制字符串数组 strs 和两个整数 m 和 n 。
请你找出并返回 strs 的最大子集的长度,该子集中 最多 有 m 个 0 和 n 个 1 。
如果 x 的所有元素也是 y 的元素,集合 x 是集合 y 的 子集 。
示例 1:
输入:strs = [“10”, “0001”, “111001”, “1”, “0”], m = 5, n = 3
输出:4
解释:最多有 5 个 0 和 3 个 1 的最大子集是 {“10”,“0001”,“1”,“0”} ,因此答案是 4 。
其他满足题意但较小的子集包括 {“0001”,“1”} 和 {“10”,“1”,“0”} 。{“111001”} 不满足题意,因为它含 4 个 1 ,大于 n 的值 3 。
示例 2:
输入:strs = [“10”, “0”, “1”], m = 1, n = 1
输出:2
解释:最大的子集是 {“0”, “1”} ,所以答案是 2 。
提示:
1 <= strs.length <= 600
1 <= strs[i].length <= 100
strs[i] 仅由 ‘0’ 和 ‘1’ 组成
1 <= m, n <= 100
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/ones-and-zeroes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决思路

解决方法
fun findMaxForm(strs: Array<String>, m: Int, n: Int): Int {
val size = strs.size
val dp = Array(size + 1) {
Array(m + 1) {
Array(n + 1) {
0 } } }
//统计0 和 1 的个数
val array = Array(size) {
Array(2) {
0 } }
strs.forEachIndexed {
index, s ->
val toCharArray = s.toCharArray()
toCharArray.forEach {
if (it == '0') {
array[index][0]++
} else {
array[index][1]++
}
}
}
//填充dp数组
for (i in 0..size) {
for (j in 0..m) {
for (k in 0..n) {
if (i > 0) {
dp[i][j][k] = dp[i - 1][j][k]
if (j - array[i - 1][0] >= 0 && k - array[i - 1][1] >= 0) {
dp[i][j][k] = dp[i][j][k].coerceAtLeast(dp[i - 1][j - array[i - 1][0]][k - array[i - 1][1]] + 1)
}
}
}
}
}
return dp[size][m][n]
}
总结
1.思路要清晰
我们要把问题分解开来,只思考要不要把当前加到集合里面
要么不加入,那么就等于前一个最大的值,要么加入,则最大值就等前面的a个0,b个1的最大值 + 1
七夕快乐
边栏推荐
- bytebuffer 使用demo
- overloaded operator
- UE4 第一人称角色模板 添加蹲伏功能
- Cron(Crontab)--use/tutorial/example
- Detailed explanation of each module of ansible
- 说说数据治理中常见的20个问题
- DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
- Machine Learning Overview
- [8.2] Code Source - [Currency System] [Coins] [New Year's Questions (Data Enhanced Edition)] [Three Stages]
- Event parse tree Drain3 usage and explanation
猜你喜欢

How do newcomers get started and learn software testing?
![[SWPU2019]Web1](/img/06/36e69a2d7d5475a6749a7d81edf50f.png)
[SWPU2019]Web1

bytebuffer 使用demo

Mysql's redo log detailed explanation

University Physics---Particle Kinematics
![[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]](/img/f3/0d92e22a424206241f4e1640f1bf6b.png)
[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
![[CISCN2019 华东南赛区]Web11](/img/15/843334fec0a5cc8cfaba92aab938db.png)
[CISCN2019 华东南赛区]Web11

No regrets, the appium automation environment is perfectly built
![[极客大挑战 2019]FinalSQL](/img/e4/0c8225ef7c5e7e5bdbaac2ef6fc867.png)
[极客大挑战 2019]FinalSQL

【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
随机推荐
flink读取mongodb数据源
bytebuffer 使用demo
Why did you start preparing for the soft exam just after the PMP exam?
[Nine Lectures on Backpacks - 01 Backpack Problems]
Application status of digital twin technology in power system
使用IDEA连接TDengine服务器
[MRCTF2020]Ezpop(详解)
[MRCTF2020] Ezpop (detailed)
【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
35岁的软件测试工程师,月薪不足2W,辞职又怕找不到工作,该何去何从?
多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
[BSidesCF 2019] Kookie
NPDP证书含金量高吗?跟PMP相比?
8.04 Day35-----MVC three-tier architecture
数字孪生技术在电力系统中的应用现状
开发属于自己的node包
[Geek Challenge 2019]FinalSQL
【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】
1007 Climb Stairs (贪心 | C思维)