122. Best Time to Buy and Sell Stock II
来源:互联网 发布:cs1.5弹道优化脚本 编辑:程序博客网 时间:2024/05/16 07:58
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).
(转)考虑如果是我们在炒股,那么我们在某天买入股票的时候,如果明天比今天价格高,那么卖给明天是赚的,如果后天更高,那肯定会想等到后天,也就是会一直等到连续的最高的那一天,而反过来想,每天都卖其实跟等到那天卖是一样的,对吧,只要今天比昨天价格高,那就昨天买,今天卖,反正又不要手续费。
https://leetcode.com/discuss/42176/three-lines-in-c-with-explanation
class Solution {public: int maxProfit(vector<int> &prices) { int profit = 0; for(int i = 0;i<prices.size();i++) { int dif = prices[i]- prices[i-1]; if(dif>0) profit+= dif; } return profit; }};
0 0
- 122. Best Time to Buy and Sell Stock II
- LeetCode 122. Best Time to Buy and Sell Stock II
- 122.Best Time to Buy and Sell Stock II
- [Leetcode] 122. Best Time to Buy and Sell Stock II
- [leetcode] 122.Best Time to Buy and Sell Stock II
- 【leetcode】122.Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 【LeetCode】122.Best Time to Buy and Sell Stock II
- [leetcode] 122. Best Time to Buy and Sell Stock II
- LeetCode #122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II LeetCode
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- [LeetCode]122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- Leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- 内联函数
- 线性回归与梯度下降(附Matlab实现)
- Codeforces Beta Round #87 (Div. 2) / 116C Party (DFS&树的最大深度)
- 第二周项目1
- 请不要做一个浮躁的人
- 122. Best Time to Buy and Sell Stock II
- 以为女程序员的奋斗路程
- Sencha touch 开发系列:类与ST组件,事件,自定义组件
- 等待通知中 。。。
- WIKIOI-1014 装箱问题
- OC自学笔记—2
- yii 验证码 CCaptcha的总结
- TCP/IP笔试面试
- 提高数据库并发性能概要