为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode474. 一和零 | Ones and Zeroes

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10347688.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.

For now, suppose you are a dominator of m0s and n 1s respectively. On the other hand, there is an array with strings consisting of only 0s and 1s.

Now your task is to find the maximum number of strings that you can form with given m 0sand n 1s. Each 0 and 1 can be used at most once.

Note:

  1. The given numbers of 0s and 1s will both not exceed 100
  2. The size of given string array won't exceed 600

Example 1:

Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
Output: 4

Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0” 

Example 2:

Input: Array = {"10", "0", "1"}, m = 1, n = 1
Output: 2

Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".

在计算机界中,我们总是追求用有限的资源获取最大的收益。

现在,假设你分别支配着 m 个 0 和 n 个 1。另外,还有一个仅包含 0 和 1 字符串的数组。

你的任务是使用给定的 m 个 0 和 n 个 1 ,找到能拼出存在于数组中的字符串的最大数量。每个 0 和 1 至多被使用一次。

注意:

  1. 给定 0 和 1 的数量都不会超过 100
  2. 给定字符串数组的长度不会超过 600

示例 1:

输入: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
输出: 4

解释: 总共 4 个字符串可以通过 5 个 0 和 3 个 1 拼出,即 "10","0001","1","0" 。

示例 2:

输入: Array = {"10", "0", "1"}, m = 1, n = 1
输出: 2

解释: 你可以拼出 "10",但之后就没有剩余数字了。更好的选择是拼出 "0" 和 "1" 。

Runtime: 4936 ms
Memory Usage: 4.1 MB
复制代码
 1 class Solution {
 2     func findMaxForm(_ strs: [String], _ m: Int, _ n: Int) -> Int {
 3         var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:n + 1),count:m + 1)
 4         for str in strs
 5         {
 6             var zeros:Int = 0
 7             var ones:Int = 0
 8             for c in str.characters
 9             {
10                 if c == "0"
11                 {
12                     zeros += 1
13                 }
14                 else
15                 {
16                     ones += 1
17                 }
18             }
19             if zeros <= m && ones <= n
20             {
21                 for i in (zeros...m).reversed()
22                 {
23                     for j in (ones...n).reversed()
24                     {
25                         //递推公式
26                         dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1)
27                     }
28                 }
29             }
30         }
31         return dp[m][n]
32     }
33 }
复制代码

复制代码
 1 class Solution {
 2     func findMaxForm(_ strs: [String], _ m: Int, _ n: Int) -> Int {
 3         let l = strs.count
 4         var dp: [[[Int]]] = Array(repeating: Array(repeating: Array(repeating:0, count: n + 1), count: m + 1), count: l + 1)
 5         for i in 0 ... l {
 6             let counts = i == 0 ? (0, 0) : getCounts(strs[i - 1])
 7             for j in 0 ... m {
 8                 for k in 0 ... n {
 9                     if i == 0 {
10                         dp[i][j][k] = 0
11                     } else if j >= counts.0 && k >= counts.1 {
12                         dp[i][j][k] = max(dp[i - 1][j][k], dp[i - 1][j - counts.0][k - counts.1] + 1)
13                     } else {
14                         dp[i][j][k] = dp[i - 1][j][k]
15                     }
16                 }
17             }
18         }
19         return dp[l][m][n]
20     }
21     
22     private func getCounts(_ str: String) -> (Int, Int) {
23         let s = Array(str)
24         var zeroCount = 0
25         s.forEach {
26             if $0 == "0" {
27                 zeroCount += 1
28             }
29         }
30         return (zeroCount, s.count - zeroCount)
31     }
32 }
复制代码

 

posted @   为敢技术  阅读(258)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°