当前位置:网站首页>算法---一和零(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
七夕快乐
边栏推荐
- 4T硬盘剩余很多提示“No space left on device“磁盘空间不足
- 不看后悔,appium自动化环境完美搭建
- 动力小帆船制作方法简单,电动小帆船制作方法
- UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
- [SWPU2019]Web1
- [BJDCTF2020] EasySearch
- Please write the SparkSQL statement
- 【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】
- how to measure distance from point to face in creo
- [Nine Lectures on Backpacks - 01 Backpack Problems]
猜你喜欢

UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)

Mini Program_Dynamic setting of tabBar theme skin

In the hot summer, teach you to use Xiaomi smart home accessories + Raspberry Pi 4 to connect to Apple HomeKit

4T硬盘剩余很多提示“No space left on device“磁盘空间不足

Application status of digital twin technology in power system

No regrets, the appium automation environment is perfectly built
![[MRCTF2020]Ezpop(详解)](/img/19/920877ca36d1eda8d118637388ab05.png)
[MRCTF2020]Ezpop(详解)

UE4 第一人称角色模板 添加蹲伏功能

Day019 Method overriding and introduction of related classes

浅析主流跨端技术方案
随机推荐
Qixi Festival code confession
8.04 Day35-----MVC三层架构
dedecms dream weaving tag tag does not support capital letters fix
UE4 第一人称角色模板 添加冲刺(加速)功能
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
A 35-year-old software testing engineer with a monthly salary of less than 2W, resigns and is afraid of not finding a job, what should he do?
upload上传图片到腾讯云,如何上传图片
dedecms报错The each() function is deprecated
什么是ASEMI光伏二极管,光伏二极管的作用
UE4 第一人称角色模板 添加蹲伏功能
Spark Basics [Introduction, Getting Started with WordCount Cases]
How to identify false evidence and evidence?
Shell(4)条件控制语句
[MRCTF2020]Ezpop(详解)
AUTOCAD——标注关联
How do newcomers get started and learn software testing?
Mysql的undo log详解
The solution to the failure to read channel information when dedecms generates a message in the background
浅析主流跨端技术方案
ansible各个模块详解