740. Delete and Earn
来源:互联网 发布:无人机飞控调参软件 编辑:程序博客网 时间:2024/05/18 06:48
这也是之前没有ac的代码,超时了,因此采用这种方式,先统计每种数字删除后的value,然后利用dp【i】算法,计算[i, end],这个范围内的,和的最大值。首先需要明确,如果采用二维dp,很容易超时。
class Solution {public: int deleteAndEarn(vector<int>& nums) { map<int,int> count; for(int i=0;i<nums.size();i++) { if(count.find(nums[i])!=count.end()) count[nums[i]]+=1; else count[nums[i]]=1; } vector<int> numbers; vector<int> values; vector<int> dp; for(map<int,int>::iterator it=count.begin();it!=count.end();++it) { numbers.push_back(it->first); values.push_back((it->first)*(it->second)); dp.push_back(0); } dp.push_back(0); for(int i=numbers.size()-1;i>=0;i--) { int sum=0; sum=values[i]; if(i+1<numbers.size()) { if(numbers[i+1]==numbers[i]+1) sum+=dp[i+2]; else sum+=dp[i+1]; dp[i] = sum>dp[i+1]?sum:dp[i+1]; } else dp[i]=values[i]; //cout<<i<<" "<<dp[i]<<endl; } return dp[0]; }};
阅读全文
0 0
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- 740. Delete and Earn
- Leetcode:740. Delete and Earn
- LeetCode | 740. Delete and Earn
- [leetcode] 740. Delete and Earn
- [LeetCode]740. Delete and Earn
- Leetcode 740. Delete and Earn
- LWC 61:740. Delete and Earn
- 【第十五周】740. Delete and Earn
- Delete and Earn
- 深入理解JQuery中$.get()、$.post()、$.getJSON()、$.ajax()方法
- QT控件大全 三十八 QWidgetKeyboard
- YCSB--HBase性能测试工具的安装和使用
- PostgreSQL HA双机热备份配置
- python 网络 select
- 740. Delete and Earn
- 关于素数的埃氏筛法
- Container:容器,对象生命周期管理的基石——学习总结篇
- springboot 打包部署 共享依赖包(分布式开发集中式部署微服务)
- IntelliJ下gradle location is incorrect问题
- ReactNative如何实时看到更改效果
- C语言实现简单的三子棋小游戏
- Java基础之分支语句
- 使用Zxing玩转二维码白边的各个花样