Best Time to Buy and Sell Stock——最优买卖股票问题
2016-10-07 19:20
260 查看
问题描述:
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.
example:
Input: [7, 1, 5, 3, 6, 4]
Output: 5
这个例子中只做一次买卖交易。设定买入价和交易净利润初始值,当股票值大于买入价时,更新净利润值,否则,更新买入价。这样做,几时买入价不是数组的最小值,也能保证净利润是最大的。
程序如下:
下一个问题是可以多次买入卖出,问题描述如下:
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).
这个问题比较简单,只要后一个数值大于前一个数组,就可以交易。
程序如下:
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.
example:
Input: [7, 1, 5, 3, 6, 4]
Output: 5
这个例子中只做一次买卖交易。设定买入价和交易净利润初始值,当股票值大于买入价时,更新净利润值,否则,更新买入价。这样做,几时买入价不是数组的最小值,也能保证净利润是最大的。
程序如下:
class Solution { public: int maxProfit(vector<int>& prices) { int n = prices.size(); if(n==0) return 0; int bought = prices[0]; int temp=0; for (int i = 1;i < n;i++) { if (prices[i] > bought) { if (prices[i] - bought > temp) { temp = prices[i] - bought; } } else { bought = prices[i]; } } return temp; } };
下一个问题是可以多次买入卖出,问题描述如下:
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).
这个问题比较简单,只要后一个数值大于前一个数组,就可以交易。
程序如下:
class Solution { public: int maxProfit(vector<int>& prices) { int n = prices.size(); int result=0; for (int i = 0;i < n-1;i++) { if (prices[i] < prices[i + 1]) result +=(prices[i + 1] - prices[i]); } return result; } };
相关文章推荐
- LeetCode——Best Time to Buy and Sell Stock I (股票买卖时机问题1)
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
- array-股票买卖问题--- Best Time to Buy and Sell Stock
- LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)
- [Java语言] [Leetcode] Best Time to Buy and Sell Stock 买卖股票的最佳时机
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- 【LintCode】 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
- LeetCode OJ:Best Time to Buy and Sell Stock(股票买卖的最佳时期)
- LintCode--best-time-to-buy-and-sell-stock(买卖股票的最佳时机)
- Leetcode 123 Best Time to Buy and Sell Stock III 至多两次买卖股票最大收益
- [leetcode 121] Best Time to Buy and Sell Stock---只能买卖股票一次的最大收益
- Best Time to Buy and Sell Stock II DP买卖股票
- 股票最大收益问题3 Best Time to Buy and Sell Stock III
- Leetcode 188 Best Time to Buy and Sell Stock IV 至多k次买卖股票最大收益
- LeetCode OJ 之 Best Time to Buy and Sell Stock (买卖股票的最佳时间)
- 【LintCode】 Best Time to Buy and Sell Stock 买卖股票的最佳时机
- LeetCode | Best Time to Buy and Sell Stock III(股票购买抛售问题III)
- LeetCode | Best Time to Buy and Sell Stock II(股票购买抛售问题II)
- leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】