【LeetCode】474. Ones and Zeroes 解题报告(Python)
【LeetCode】474. Ones and Zeroes 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/ones-and-zeroes/description/
题目描述:
n 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 m 0s 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 0s and n 1s. Each 0 and 1 can be used at most once.
Note:
- The given numbers of 0s and 1s will both not exceed 100
- 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".
题目大意
我们现在从数组中每个字符串都有一些0和1,问给了m个0,n个1,从数组中取出最多的字符串,这些字符串中1和0的出现次数之和不超过m,n.
解题方法
看到这个题第一个感觉是贪心,但是想了想,无论是贪心少的还是贪心多的,都会影响到后面选取的变化,所以不行。
遇到这种求最多或最少的次数的,并且不用求具体的解决方案,一般都是使用DP。
这个DP很明白了,定义一个数组dp[m+1][n+1],代表m个0, n个1能组成的最长字符串。遍历每个字符串统计出现的0和1得到zeros和ones,所以第dp[i][j]的位置等于dp[i][j]和dp[i - zeros][j - ones] + 1。其中dp[i - zeros][j - ones]表示如果取了当前的这个字符串,那么剩下的可以取的最多的数字。
这个题用Counter竟然不通过,使用两个常量去做统计居然就行了。
时间复杂度有点难计算,大致是O(MN * L), L 是数组长度,空间复杂度是O(MN).
代码如下:
class Solution(object):
def findMaxForm(self, strs, m, n):
"""
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
# m个0, n个1能组成的最长字符串
dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
for str in strs:
zeros, ones = 0, 0
for c in str:
if c == "0":
zeros += 1
elif c == "1":
ones += 1
for i in range(m, zeros - 1, -1):
for j in range(n, ones - 1, -1):
dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1)
return dp[m][n]
参考资料:
http://www.cnblogs.com/grandyang/p/6188893.html
https://kingsfish.github.io/2017/07/23/Leetcode-474-Ones-and-Zeros/
日期
2018 年 9 月 23 日 —— 今天是实验室第一个打卡的
【LeetCode】474. Ones and Zeroes 解题报告(Python)的更多相关文章
- 【LeetCode】283. Move Zeroes 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
随机推荐
- nrf 51802 和 nrf51822 的区别于联系
51802QFAA与51822QFAA在FLASH 跟RAM的容量没有差别:区别在于:a,接收灵敏度 51802是-91dBm;51822是-93dBm,这个差异导致接收距离有差异:b,Tx Powe ...
- 基于树莓派部署 code-server
code-server 是 vscode 的服务端程序,通过部署 code-server 在服务器,可以实现 web 端访问 vscode.进而可以达到以下能力: 支持跨设备(Mac/iPad/iPh ...
- day22面向对象编程思想
day22面向对象编程思想 1.面向过程 面向过程: 核心是"过程"二字 过程的终极奥义就是将程序流程化 过程是"流水线",用来分步骤解决问题的 面向对象: 核 ...
- Zookeeper之创建组,加入组,列出组成员和删除组
public class CreateGroup implements Watcher { private static final int SESSION_TIMEOUT=5000; //ZooKe ...
- python 从ubantu环境迁移到windows环境
下载安装Anaconda3 Anaconda3-2021.05-Windows-x86_64.exe 默认安装目录 C:\ProgramData\Anaconda3 可以启动Anaconda查看不同的 ...
- centos 7 重新获取IP地址
1.安装软件包 dhclient # yum install dhclient 2.释放现有IP # dhclient -r 3.重新获取 # dhclient 4.查看获取到到IP # ip a
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...
- NSURLSessionDownloadTask实现大文件下载
- 4.1 涉及知识点(1)使用NSURLSession和NSURLSessionDownload可以很方便的实现文件下载操作 第一个参数:要下载文件的url路径 第二个参数:当接收完服务器返回的数据 ...
- Selenium之Canvas画布操作
现在有一个场景是需要进入到 Canvas画布中 进行单击操作,现在使用过如下方法 canvas = driver.find_element_by_xpath("//canvas[@id='# ...
- TV盒子 android 4.4安装kodi 和 安装jellyfin插件连接到 jellyfin服务器
很多IPTV盒子系统都是android 4.4的,无法安装新版kodi, 最高只能安装到改版的kodi, 版本是17.1,jellyfin插件也只能安装到0.5.8 首先下载kodi和jellyfin ...