LeetCode122—Best Time to Buy and Sell Stock II
来源:互联网 发布:linux vi 运行 编辑:程序博客网 时间:2024/05/19 14:15
LeetCode122—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 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).
不限买卖次数,但是每天只能一次操作。
分析
这题就简单多了,既然不限买卖次数,那么只要第i+1天的价格比第i天的价格要高,这样就可以交易。
代码
class Solution {public: int maxProfit(vector<int>& prices) { int curMax = 0; for (int i = 1; i < prices.size(); i++) { if (prices[i] - prices[i - 1] > 0) curMax = curMax + (prices[i] - prices[i - 1]); } return curMax; }};
1 0
- LeetCode122—Best Time to Buy and Sell Stock II
- leetcode122---Best Time to Buy and Sell Stock II
- LeetCode122:Best Time to Buy and Sell Stock II
- leetcode122 Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- leetcode122 Best Time to Buy and Sell Stock II
- Leetcode122. Best Time to Buy and Sell Stock II
- leetcode122.[DP] Best Time to Buy and Sell Stock II
- leetcode122. Best Time to Buy and Sell Stock II
- leetcode122~Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II题解
- LeetCode122 Best Time to Buy and Sell Stock II
- Leetcode122. Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- LeetCode122——Best Time to Buy and Sell Stock II
- Leetcode122 - Best Time to Buy and Sell Stock II (dp)
- 使用socket判断http请求或http响应的传输结束
- c++第四次实验
- CSS中position的absolute如何相对于父元素的位置进行定位
- 使用反射通过字符串动态调用函数
- Ubuntu kylin 安装记
- LeetCode122—Best Time to Buy and Sell Stock II
- 简单的基于YCrCb颜色空间的人脸检测(肤色)
- 敲响OO时代的丧钟
- java中Executor、ExecutorService、ThreadPoolExecutor介绍
- 剑指offerr面试题 调整数组顺序使奇数位于偶数前面
- 常见面试题及其解答--问题摘自程序员面试笔试宝典(未完待续)
- CDISC SDTM PR domain 学习笔记
- [IOS/翻译]Core OS Layer
- 流程定义查询