Leetcode 309. Best Time to Buy and Sell Stock with Cooldown
来源:互联网 发布:php的框架有哪些 编辑:程序博客网 时间:2024/06/05 06:24
题目:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
- You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
- After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
class Solution {public:int bestprice(int left,int right ,vector<int>& a){int min=0,max=0,diffenerce=0;for(int i=left;i<right;i++){min=a[i];for(int j=i+1;j<=right;j++){max=a[j];if((max-min)>diffenerce)diffenerce=max-min;}}if((right-left)<=0){return 0;}else{if(right-left==1){return (a[right]-a[left])<0?0:(a[right]-a[left]);}else{ int temp=0; for(int i=left+2;i<right-1;i++) { temp= bestprice(left,i-1,a)+bestprice(i+1,right,a)<temp?temp:bestprice(left,i-1,a)+bestprice(i+1,right,a); } return temp<diffenerce?diffenerce:temp;}}} int maxProfit(vector<int>& prices) { int i=0,j=prices.size(); return bestprice(0,j-1,prices); }};2,参考网上大神,吊打:原地址
http://bookshadow.com/weblog/2015/11/24/leetcode-best-time-to-buy-and-sell-stock-with-cooldown/
阅读全文
0 0
- leetcode Best Time to Buy and Sell Stock with Cooldown
- LeetCode Best Time to Buy and Sell Stock with Cooldown
- Leetcode: Best Time to Buy and Sell Stock with Cooldown
- [LeetCode]Best Time to Buy and Sell Stock with Cooldown
- Leetcode Best Time to Buy and Sell Stock with Cooldown
- leetcode - Best Time to Buy and Sell Stock with Cooldown
- LeetCode:Best Time to Buy and Sell Stock with Cooldown
- leetcode Best Time to Buy and Sell Stock with Cooldown
- Leetcode Best Time to Buy and Sell Stock with Cooldown
- LeetCode:Best Time to Buy and Sell Stock with Cooldown
- LeetCode--Best Time to Buy and Sell Stock with Cooldown
- 309.Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 从零开始的UART(基于S3C2440)
- 点击按钮更改对应段落(多个按钮之间互不影响)
- idea SVN 忽略文件夹classes、lib
- matlab 中如何使用bwboundaries获取连通区域并求取其轮廓
- Vue-高级讲师之笔记06
- Leetcode 309. Best Time to Buy and Sell Stock with Cooldown
- 代码更换SeekBar进度条颜色
- 课堂在线Java程序设计作业 类的使用
- 【总结】数据预处理
- Easyui combobox设置值和文本的几种方式和问题
- jQuery setInterval倒计时精确到毫秒
- 【性能测试】-常见性能测试问题分析(一)
- shiro
- Vue-高级讲师之笔记07(结尾)