Leetcode Best Time to Buy and Sell Stock
来源:互联网 发布:数据分级 英语 编辑:程序博客网 时间:2024/06/05 10:51
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
O(n^2)的就不用说了,一层遍历start_idx 一层遍历end_idx, 所以要在O(n)时间做的话 就是一次遍历记录两个位置,特殊点在于, start_idx总是当前最小, 答案一定的在这样的解中产生,就是 当前值减去当前最小值。
class Solution {public: int maxProfit(vector<int>& prices) { if (prices.size() == 0) return 0; int ret = 0, cur_min = prices[0]; for (int i = 1; i < prices.size(); i++) { ret = max(ret, prices[i] - cur_min); if (prices[i] < cur_min) { cur_min = min(prices[i], cur_min); } } return ret; }};
阅读全文
0 0
- 【LeetCode】Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock
- [LeetCode] [LeetCode]Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock
- LeetCode Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- Leetcode: Best time to buy and sell a stock I
- Leetcode: Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock
- [Leetcode] Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock III
- LeetCode : Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock
- leetcode 16: Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock
- Segmentation obtained by thresholding the UCM at certain level
- [Leetcode] 41, 75, 34
- hdu6147 模拟
- 为什么是装饰模式而不是继承
- python 程序运行时间
- Leetcode Best Time to Buy and Sell Stock
- hdu2391Filthy Rich(动态规划基础)
- idea热部署静态资源无法及时生效
- Python单元测试(doctest和unittest)
- nodejs express template (模版)的使用 (ejs + express)
- JavaScript权威指南读书笔记——第十七章 事件处理
- C++ 带空格的字符串输入
- 回调函数例题
- HDU-1524-A Chess Game && POJ-2425 【sg】