[LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
2016-12-03 23:37
701 查看
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Have you met this question in a real interview?
Yes
Example
Given array
LeetCode上的原题,请参见我之前的解法Best Time to Buy and Sell Stock。
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Have you met this question in a real interview?
Yes
Example
Given array
[3,2,3,1,2], return
1.
LeetCode上的原题,请参见我之前的解法Best Time to Buy and Sell Stock。
class Solution { public: /** * @param prices: Given an integer array * @return: Maximum profit */ int maxProfit(vector<int> &prices) { int res = 0, mn = INT_MAX; for (int i = 0; i < prices.size(); ++i) { mn = min(mn, prices[i]); res = max(res, prices[i] - mn); } return res; } };
相关文章推荐
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
- LeetCode OJ 之 Best Time to Buy and Sell Stock II (买卖股票的最佳时间 2)
- [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
- LintCode--best-time-to-buy-and-sell-stock(买卖股票的最佳时机)
- LeetCode-121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- LeetCode OJ 之 Best Time to Buy and Sell Stock (买卖股票的最佳时间)
- 121. Best Time to Buy and Sell Stock 最佳买卖股票时间
- 122. Best Time to Buy and Sell Stock II 最佳买卖股票的时间
- 【LintCode】 Best Time to Buy and Sell Stock 买卖股票的最佳时机
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- 【LintCode】 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
- 【LeetCode】Best Time to Buy and Sell Stock(买卖股票的最佳时间)
- Best Time to Buy and Sell Stock 【最佳买卖股票的时间】【容易】
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- **[Lintcode]Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV Leetcode