[LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
2017-01-01 03:09
435 查看
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).
Have you met this question in a real interview?
Yes
Example
Given an example [2,1,2,0,1], return 2
LeetCode上的原题,请参见我之前的博客Best Time to Buy and Sell Stock II。
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).
Have you met this question in a real interview?
Yes
Example
Given an example [2,1,2,0,1], return 2
LeetCode上的原题,请参见我之前的博客Best Time to Buy and Sell Stock II。
class Solution { public: /** * @param prices: Given an integer array * @return: Maximum profit */ int maxProfit(vector<int> &prices) { int res = 0, n = prices.size(); for (int i = 0; i < n - 1; ++i) { if (prices[i] < prices[i + 1]) { res += prices[i + 1] - prices[i]; } } return res; } };
相关文章推荐
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- 122. Best Time to Buy and Sell Stock II 最佳买卖股票的时间
- 【LintCode】 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
- LeetCode OJ 之 Best Time to Buy and Sell Stock II (买卖股票的最佳时间 2)
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
- 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 (买卖股票的最佳时间)
- LeetCode-121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)
- 最佳时间买入卖出股票 Best Time to Buy and Sell Stock LeetCode