面试题38:股票最大收益问题
2017-06-05 11:27
330 查看
leetcode中股票问题指的是股票的价格处于波动当中,分别在最多购买一次、买卖任意多次、买卖k次的最大收益。
Say you have an array for which the ithelement 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.
Say you have an array for which the ithelement 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 ithelement is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most twotransactions.
Note:
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 ithelement 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.
1 class Solution { 2 public: 3 int maxProfit(vector<int> &prices) { 4 int res = 0; 5 int buy = INT_MAX; 6 for(int price : prices){ 7 buy = min(buy,price); 8 res = max(res,price-buy); 9 } 10 return res; 11 } 12 };
Say you have an array for which the ithelement 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).
1 class Solution { 2 public: 3 int maxProfit(vector<int> &prices) { 4 int sum = 0; 5 for(size_t i =1;i<prices.size();i++){ 6 if(prices[i] > prices[i-1]){ 7 sum += prices[i] - prices[i-1]; 8 } 9 } 10 return sum; 11 } 12 };
Say you have an array for which the ithelement is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most twotransactions.
Note:
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(); if(n < 2) return 0; vector<vector<int>> g(n,vector<int>(3,0)); vector<vector<int>> l(n,vector<int>(3,0)); for(int i=1;i<n;i++){ int diff = prices[i] - prices[i-1]; for(int j=1;j<=2;j++){ l[i][j] = max(g[i-1][j-1] +max(diff,0),l[i-1][j] + diff); g[i][j] = max(g[i-1][j],l[i][j]); } } return g[n-1][2]; } };
相关文章推荐
- JAVA股票问题计算你能获得的最大收益
- 买卖股票收益最大问题
- 最大股票收益问题(数组最大差问题)
- 第四章 最大子数组问题(股票最大收益) C++实现 算法导论
- 股票最大收益问题
- 最多n次购买时股票最大收益问题
- 股票最大收益问题及数组最大差值问题
- 股票最大收益问题3 Best Time to Buy and Sell Stock III
- 找出买卖股票的最大收益及其时间
- 【算法导论学习-27】动态规划经典问题01:钢条切割的最大收益
- 百度面试题——最大连续数字串问题
- 股票买卖之问题转化为最大字段和
- 股票买进卖出问题—百度实习招聘面试题
- 一道亚马逊面试题(股票买卖问题)
- 网易游戏面试--两次股票买入卖出的最大收益
- 关于序列的面试题3------------股票问题
- 面试题——栈的最大值问题
- 如何买卖股票以获得最大收益 java算法
- IT公司面试题--最大储水量问题
- 程序员面试题精选100题(38)-输出1到最大的N位数[算法]