474. Ones and Zeroes
来源:互联网 发布:开源财务系统 php 编辑:程序博客网 时间:2024/06/06 01:45
强行求出了解,主要记录每一个dp空格的值以及有值的dp的位置。但是求解的过程太慢了。于是参考了discuss,我的天,基本都是一样的,但是它的DP方程更加简洁明了。。。。
class Solution {public: int findMaxForm(vector<string>& strs, int m, int n) { int maxForm=0; vector<vector<int>> dp; vector<int> temp(n+1,0);//dp[0][0]~dp[m][n] for(int i=0;i<m+1;i++) dp.push_back(temp); set<pair<int,int>> hasValue; pair<int,int> temp1(m,n);//m---0;n---1 hasValue.insert(temp1); for(int i=0;i<strs.size();i++) { int one=0; int zero=0; for(int j=0;j<strs[i].size();j++) one+=strs[i][j]-'0'; zero=strs[i].size()-one; //cout<<zero<<" "<<one<<endl; set<pair<int,int>> newHasValue; for(set<pair<int,int>>::iterator it=hasValue.begin();it!=hasValue.end();it++) { if(it->first>=zero&&it->second>=one) { dp[it->first-zero][it->second-one]=max(dp[it->first-zero][it->second-one],dp[it->first][it->second]+1); maxForm=max(maxForm,dp[it->first-zero][it->second-one]); pair<int,int> temp(it->first-zero,it->second-one); newHasValue.insert(temp); } } for(set<pair<int,int>>::iterator it=newHasValue.begin();it!=newHasValue.end();it++) hasValue.insert(*it); } /*for(int i=0;i<dp.size();i++) { for(int j=0;j<dp[i].size();j++) cout<<dp[i][j]<<" "; cout<<endl; }*/ return maxForm; }};
0 0
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- Apereo CAS 5.0.X 启用OAuth2服务时的一个坑
- (转载)c++实现反射机制
- 解决js动态改变dom元素属性后页面及时渲染问题
- JAVA学习之this,static,String常用方法
- [编程题]序列和
- 474. Ones and Zeroes
- Android热修复之Tinker
- 购物车项目总结
- java中泛型的使用
- 简单小结一下关于JavaScript中的闭包
- adnroid.appwidget.AppWidgetProvider
- 排序总结(java版本)
- 【AngularJS】使用ng-repeat中$index的注意点
- RabbitMQ官网教程3——发布订阅