Leetcode —— Best Time to Buy and Sell Stock
2014-04-25 11:29
204 查看
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.
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.
class Solution { public: int maxProfit(vector<int> &prices) { if (0 == prices.size()) { return 0; } int minPrice = prices[0]; int maxProfit = 0; for (vector<int>::const_iterator iter = prices.begin() + 1; iter != prices.end(); ++iter) { minPrice = min(minPrice, *(iter)); maxProfit = max(maxProfit, *(iter) - minPrice); } return maxProfit; } };
相关文章推荐
- Leetcode 122 Best Time to Buy and Sell Stock II
- 【leetcode】【121】Best Time to Buy and Sell Stock
- leetcode 188---Best Time to Buy and Sell Stock IV
- LeetCode-Best Time to Buy and Sell Stock
- [leetcode] 123. Best Time to Buy and Sell Stock III 解题报告
- 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 II(2014.2.17)
- [LeetCode]: 121: Best Time to Buy and Sell Stock
- LeetCode——Best Time to Buy and Sell Stock
- LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)
- LeetCode---Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- LeetCode-Best Time to Buy and Sell Stock II-解题报告
- Best Time to Buy and Sell Stock——LeetCode
- [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 II
- LeetCode 188 Best Time to Buy and Sell Stock IV (动态规划 推荐)