当前位置:网站首页>Algorithms - ones and zeros (Kotlin)
Algorithms - ones and zeros (Kotlin)
2022-08-05 05:05:00 【Millet technology research and development of the Android Cao X】
题目
给你一个二进制字符串数组 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.思路要清晰
Let's break down the problem,Just thinking about whether to add the current to the collection
or don't join,Then it is equal to the previous largest value,要么加入,Then the maximum value will wait for the previous onea个0,b个1的最大值 + 1
七夕快乐
边栏推荐
- [Nine Lectures on Backpacks - 01 Backpack Problems]
- 【cesium】加载并定位 3D Tileset
- [informix] Resolving startup errors and solutions
- write the story about us
- Develop your own node package
- After controlling the export file in MySQL, it becomes \N. Is there any solution?
- Judgment statement _switch and case
- NPDP证书含金量高吗?跟PMP相比?
- 小程序_动态设置tabBar主题皮肤
- entry point injection
猜你喜欢

Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!

AUTOCAD - dimension association

Application status of digital twin technology in power system

About the installation of sklearn library

upload upload pictures to Tencent cloud, how to upload pictures

开发一套高容错分布式系统

Redis哨兵模式配置文件详解

Please write the SparkSQL statement

Detailed explanation of each module of ansible

Flutter real machine running and simulator running
随机推荐
App rapid development and construction experience: the importance of small programs + custom plug-ins
[8.3] Code Source - [meow ~ meow ~ meow~] [tree] [and]
UVA10827
【软考 系统架构设计师】软件架构设计③ 特定领域软件架构(DSSA)
浅析主流跨端技术方案
类的底层机制
dedecms后台生成提示读取频道信息失败的解决方法
使用二维码解决固定资产管理的难题
creo怎么测量点到面的距离
There are a lot of 4T hard drives remaining, prompting "No space left on device" insufficient disk space
"Recursion" recursion concept and typical examples
number_gets the specified number of decimals
Structured light 3D reconstruction (1) Striped structured light 3D reconstruction
upload上传图片到腾讯云,如何上传图片
RL强化学习总结(一)
LAB 信号量实现细节
How to identify false evidence and evidence?
结构光三维重建(二)线结构光三维重建
Redis哨兵模式配置文件详解
狗仔队:表面编辑多视点图像处理