LeetCode 121. Best Time to Buy and Sell Stock
来源:互联网 发布:常见电信网络诈骗试题 编辑:程序博客网 时间:2024/04/30 15:23
描述
给出连续几天股票的波动,问可以获得的最大收益
解决
首先要知道,股票是要先买入,然后再卖出的。比较暴力的就是O(n^2)遍历啦,但是可以用O(n)的方法,在遍历的过程中记录到当前位置最小的值,然后做减法进行比较统计就可以了。
class Solution {public: int maxProfit(vector<int>& prices) { int lenth = prices.size(); if (lenth == 0){ return 0; } int max_val = 0; int tmp = prices[0]; for (int i = 1; i < lenth; ++i){ tmp = tmp > prices[i] ? prices[i] : tmp; max_val = max_val < prices[i] - tmp ? prices[i] - tmp : max_val; } return max_val; }};
0 0
- LeetCode 121. Best Time to Buy and Sell Stock
- [Leetcode] 121. Best Time to Buy and Sell Stock
- [leetcode] 121. Best Time to Buy and Sell Stock
- 【leetcode】121. Best Time to Buy and Sell Stock
- [leetcode] 121. Best Time to Buy and Sell Stock
- 121. Best Time to Buy and Sell Stock LeetCode
- [LeetCode]121. Best Time to Buy and Sell Stock
- Leetcode 121. Best Time to Buy and Sell Stock
- leetcode 121. Best Time to Buy and Sell Stock
- LeetCode 121. Best Time to Buy and Sell Stock
- LeetCode题解:121. Best Time to Buy and Sell Stock
- LeetCode *** 121. Best Time to Buy and Sell Stock
- 【leetcode】121. Best Time to Buy and Sell Stock
- #leetcode#121. Best Time to Buy and Sell Stock
- LeetCode 121. Best Time to Buy and Sell Stock
- 121.[Leetcode]Best Time to Buy and Sell Stock
- leetcode 121. Best Time to Buy and Sell Stock
- Leetcode 121. Best Time to Buy and Sell Stock
- 安卓获取当前最顶部Task应用名称
- bootstrap学习
- 【Sicily 1937】导游
- struts配置全局异常result与全局异常映射mapping以及自定义返回类型
- Android不同App中Activity的相互启动
- LeetCode 121. Best Time to Buy and Sell Stock
- Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果
- Java模拟Http post、get请求
- 用java语言实现事件委托模式
- 非常可乐
- MySQL一行多列求和sum()函数
- Filter简介
- hadoop 基本操作命令
- 悼念512汶川大地震遇难同胞——老人是真饿了