LeetCode之旅(39)
来源:互联网 发布:手机淘宝装修用dw 编辑:程序博客网 时间:2024/06/07 00:02
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 dayi.
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).
股票允许多次买卖,因此就把每天的利润算出来再相加即可。
class Solution {public: int maxProfit(vector<int> &prices) { if (!prices.size()) return 0; int profit = 0; for (int i=1; i<prices.size(); i++) { if (prices[i] > prices[i-1]) profit += prices[i]-prices[i-1]; } return profit; }};
0 0
- LeetCode之旅(39)
- LeetCode之旅(2)
- LeetCode之旅(3)
- LeetCode之旅(4)
- LeetCode之旅(5)
- LeetCode之旅(6)
- LeetCode之旅(7)
- LeetCode之旅(8)
- LeetCode之旅(9)
- LeetCode之旅(10)
- LeetCode之旅(12)
- LeetCode之旅(13)
- LeetCode之旅(14)
- LeetCode之旅(15)
- LeetCode之旅(16)
- LeetCode之旅(17)
- LeetCode之旅(18)
- LeetCode之旅(19)
- R语言兴起与SAS,SPSS
- memcpy
- do...while(0)在宏定义中的用法
- UVA 1045 - The Great Wall Game(二分图完美匹配)
- ant build.xml 打包可执行jar
- LeetCode之旅(39)
- Dash与Bash的语法区别
- shell脚本中的几个括号总结(小括号/大括号/花括号)
- sizeof 和 strlen
- bloom filter与dawgdic(一种trie树)
- qt事件处理
- iOS中实现打电话的方式
- cocos2d-x项目构建相关
- 有关eclipse