LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
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). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
分析
参考stock problem相关的精帖
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<=1)
return 0;
vector<int> sell(prices.size(),0),buy(prices.size(),0);
buy[0]=-prices[0];
for(int i=1;i<prices.size();i++){
sell[i]=max(sell[i-1],buy[i-1]+prices[i]);
buy[i]=max(buy[i-1],sell[i-1]-prices[i]);
}
return sell[prices.size()-1];
}
};
LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)的更多相关文章
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (买卖股票的最好时机之二)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 122. Best Time to Buy and Sell Stock II ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java [Leetcode 122]Best Time to Buy and Sell Stock II
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- LeetCode 122 Best Time to Buy and Sell Stock II 解题报告
题目要求 Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 122 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- NOIp2013 货车运输 By cellur925
题目传送门 A 国有 n 座城市,编号从 1 到 n ,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重. 现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下 ...
- Golang bash alias 自动配置GOPATH并运行项目
BASH代码: source ~/.bash_profile; export GOPATH=$GOPATH:`cd ..; pwd`; echo -e "* GOPATH: $GOPATH ...
- python 37条编程技巧-汇总(转载+整理)
1.原地交换两个数字 x, y =10, 20 print x, y y, x = x, y print x, y 10 20 20 10 2.链状比较操作符 n = 10 print 1 < ...
- linux ls touch、umask、 chattr
1.$PATH2.ls -al ~ (~ 显示隐藏的文件) 3$ cd ~ // 回到自己的主文件4.cp /var/log/wtmp . //复制到当前目录,最后的“.”不要忘 ls -l /var ...
- Unix\Linux | 总结笔记 |文件系统
1. ls [选项] [文件] 显示目录中的文件信息 -a 显示全部文件(包括隐藏文件) -l 查看文件的属性.大小等详细信息 (ls -l 详解) -al 查看当前目录中 ...
- BFS(最短路) HDOJ 4308 Saving Princess claire_
题目传送门 题意:一个(r*c<=5000)的迷宫,起点'Y‘,终点'C',陷阱‘#’,可行路‘*’(每走一个,*cost),传送门P,问Y到C的最短路 分析:一道最短路问题,加了传送门的功能, ...
- jdk 1.8下 java ArrayList 添加元素解析
转载请注明http://www.cnblogs.com/majianming/p/8006452.html 有人问我,java ArrayList底层是怎么实现的?我就回答数组,他再问我,那它是怎么实 ...
- html5的表单元素总结
- 一个简单的公式——求小于N且与N互质的数的和
首先看一个简单的东西. 若$gcd(i,n)=1$,则有$gcd(n-i,n)=1$ 于是在小于$n$且与$n$互质的数中,$i$与$n-i$总是成对存在,且相加等于$n$. 考虑$i=n-i$的特殊 ...
- spark集群启动步骤及web ui查看
集群启动步骤:先启动HDFS系统,在启动spark集群,最后提交jar到spark集群执行. 1.hadoop启动cd /home/***/hadoop-2.7.4/sbinstart-all.sh ...