best-time-to-buy-and-sell-stock-iii
来源:互联网 发布:anywhere远程桌面软件 编辑:程序博客网 时间:2024/06/16 06:54
题目:
Say you have an array for which the i th element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
程序:
class Solution {public: int maxProfit(vector<int> &p) { if(p.size()==0||p.size()==1) return 0; if(p.size()==2) return max(0,p[1]-p[0]); int Max=-9999,k; for(k=0;k<p.size()-1;k++) Max=max(jisuan(p,0,k)+jisuan(p,k+1,p.size()-1),Max); Max=max(Max,jisuan(p,0,p.size()-1)); return Max; } int jisuan(vector<int> &p,int i,int j) { int xl[1000],k,Min=9999,Max=-9999; for(k=i;k<=j;k++) { Min=min(Min,p[k]); xl[k]=Min; } for(k=i;k<=j;k++) Max=max(Max,p[k]-xl[k]); return Max; }};
阅读全文
0 0
- LeetCode Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock III
- leetcode 16: Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- leetcode 62: Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- 【leetcode】Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- LeetCode - Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III
- LeetCode之Best Time to Buy and Sell Stock III
- [LeetCode]Best Time to Buy and Sell Stock III
- SQL 和 NoSQL
- 安装Robo 3T(Robomongo)MongoDB可视化工具
- linux (ubuntu) Qt 5.9.1 发布程序 转载+个人错误记录
- Hibernate与 MyBatis的比较
- 弹出层随鼠标滑轮上下移动,页面遮罩层不动
- best-time-to-buy-and-sell-stock-iii
- centos7 安装和设置mariadb
- hdu(HDU5945)单调队列优化DP
- fseek函数(转)
- form表单提交以及用Jquery实现ajax提交form表单
- ATS中常见的字符串操作2
- 获取真实访问者ip地址
- JavaSE基础01
- 杂志投稿费用