假设你有一个数组prices,长度为n,其中prices[i]是股票在第i天的价格,请根据这个价格数组,返回买卖股票能获得的最大收益
1.你可以买入一次股票和卖出一次股票,并非每天都可以买入或卖出一次,总共只能买入和卖出一次,且买入必须在卖出的前面的某一天
2.如果不能获取到任何利润,请返回0
3.假设买入卖出均无手续费
数据范围: ![](https://www.nowcoder.com/equation?tex=0%20%5Cle%20n%20%5Cle%2010%5E5%20%2C%200%20%5Cle%20val%20%5Cle%2010%5E4)
要求:空间复杂度
,时间复杂度 ![](https://www.nowcoder.com/equation?tex=O(n))
[8,9,2,5,4,7,1]
5
在第3天(股票价格 = 2)的时候买入,在第6天(股票价格 = 7)的时候卖出,最大利润 = 7-2 = 5 ,不能选择在第2天买入,第3天卖出,这样就亏损7了;同时,你也不能在买入前卖出股票。
[2,4,1]
2
[3,2,1]
0
# # # @param prices int整型一维数组 # @return int整型 # class Solution: def maxProfit(self , prices ): # write code here lst = [] if len(prices) == 0 or len(prices)==1 : return 0 for i in range (len(prices)-1): for j in range(i+1,len(prices)): x = prices[j]-prices[i] lst.append(x) if max(lst)<=0: return 0 else: return max(lst)
# # # @param prices int整型一维数组 # @return int整型 # class Solution: def maxProfit(self , prices ): # write code here lst = [] if len(prices) == 0&nbs***bsp;len(prices)==1 : return 0 for i in range (len(prices)-1): for j in range(i+1,len(prices)): x = prices[j]-prices[i] lst.append(x) if max(lst)<=0: return 0 else: return max(lst)
class Solution { public: int maxProfit(vector<int>& prices) { int min_price = prices[0]; // 最低价 int max_money = 0; // 最高利润 for (int i = 0; i < prices.size(); ++i) { min_price = min(min_price, prices[i]); max_money = max(prices[i] - min_price, max_money); } return max_money; } };
import java.util.*; public class Solution { /** * f(x) = prices[x] - min(price[0 ~ x-1]) * f(1) = max(prices[1] - prices[0], 0); * f(0) = 0; * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { // write code here if(prices==null||prices.length <= 1){ return 0; } int[] f = new int[prices.length]; f[0] = 0; f[1] = Math.max(prices[1] - prices[0],0); int max = Math.max(f[0],f[1]); int min = Math.min(prices[1] , prices[0]); for(int x = 2; x < prices.length; x++){ min = Math.min(min,prices[x]); max = Math.max(max,f[x] = prices[x] - min); } return max; } }
class Solution { public: int maxProfit(vector<int> &prices) { int len = prices.size(); int tempIncome = 0; int maxIncome = 0; for(int i=1;i<len;i++) { if(tempIncome < 0) tempIncome = prices[i] - prices[i-1]; else tempIncome = tempIncome + prices[i] - prices[i-1]; if(tempIncome > maxIncome) maxIncome = tempIncome; } return maxIncome; } };
public class Solution { public int maxProfit(int[] prices) { if (prices.length<1) return 0; int sum = 0, min = prices[0]; for (int i=0; i<prices.length; i++){ if (prices[i] < min) min = prices[i]; else if (prices[i] > min) sum = Math.max(sum, prices[i] - min); } return sum; } }
/* * Runtime: Runtime: 1 ms.Your runtime beats 86.61 % of java submissions */ public int maxProfit(int[] prices) { if(prices==null||prices.length<2) return 0; int minNum=prices[0],maxPro=0; for(int i=1;i<prices.length;i++){ if(prices[i]<minNum) minNum=prices[i]; else if(prices[i]>minNum) maxPro=Math.max(maxPro, prices[i]-minNum); } return maxPro; }
public class Solution { public int maxProfit(int[] prices) { if(prices==null || prices.length<1) return 0; int maxProfit=0; int minPrice=prices[0]; for(int i=1;i<prices.length;i++){ if(prices[i]-minPrice>maxProfit) maxProfit = prices[i]-minPrice; else if(prices[i]<minPrice) minPrice = prices[i]; } return maxProfit; } }
public class Solution { public int maxProfit(int[] prices) { if(prices == null || prices.length==0) return 0; int min = prices[0]; int res = 0; for(int i =1;i<prices.length;i++){ if(prices[i]>min) res = res>prices[i]-min?res:prices[i]-min; else min = prices[i]; } return res; } }
//贪心算法,从左往右遍历向量,遇到当前最小值,则保存,
//如果不是最小值,则计算它到最小值的距离,保存为最大利润
class Solution {
public:
int maxProfit(vector<int> &prices) {
int min_num = prices[0];
int max_profit = 0;
for(int i=1; i<prices.size(); ++i){
if(prices[i]<min_num)
min_num = prices[i];//更新最小值
else
max_profit = max(max_profit, prices[i]-min_num);
}
return max_profit;
}
};
思路如下:
我们需要可以遍历这个数组,在遍历的同时维护一个变量min来记录当前为止,历史中的最小的价格(作为买入价格),之后我们用当前的价格减去这个买入的价格,就可以得到可以赚的价格,因此我们再来维护一个max来记录最高可赚的价格,遍历完成之后的max就是最终的结果,
代码如下,仅供参考:
int maxProfit(int *prices,int pricesSize){ if(prices == NULL || pricesSize == 0) return 0; int min = prices[0]; int max = -1; for(int i = 0;i < pricesSize; ++i){ //min用来维护历史的最小值 if(min > prices[i]) min = prices[i]; //max用来管理最大的利润 if(max < prices[i] - min) max = prices[i] - min; } return max; }
class Solution { /** * 找最小值和此后的最大值是最简单的 * 为了方便理解后面系列题目,使用转移方程 * 假设第i天持有股票的收益是 f(i,1), 未持有是f(i,0) * f(i,0) = max(f(i-1, 0), f(i-1, 1)+prices[i]) * f(i,1) = max(f(i-1, 1), f(i-1, 0)-prices[i]) * 对于f(i,1)因为只能买一次所以f(i-1,0)=0 * f(i,1) = max(f(i-1, 1), 0-prices[i]) */ public int maxProfit(int[] prices) { if(prices.length<2) return 0; int i0=0,i1=-prices[0]; for(int i = 1; i < prices.length; i++){ i0 = Math.max(i0, i1+prices[i]); i1 = Math.max(i1, -prices[i]); } return i0; } }
# # # @param prices int整型一维数组 # @return int整型 # class Solution: def maxProfit(self , prices ): # write code here if not prices&nbs***bsp;prices==1: return 0 min_p=prices[0] money=0 max_m=0 for p in prices: money = p-min_p if money<0: min_p=p if money>max_m: max_m=money return max_money
class Solution { public: int maxProfit(vector<int> &prices) { int size = prices.size(); if(size<=0) return 0; int max = 0; int gain = 0; for(int i=0;i<size;i++) for(int j=i;j<size;j++) { gain = prices[j]-prices[i]; //记录长和跌的情况 if(max<gain)max = gain; //与所有长幅相比,最后为最大收益 } return max; } };
class Solution { public: /** * * @param prices int整型vector * @return int整型 */ int maxProfit(vector<int>& prices) { int size = prices.size(), max = INT_MIN, dp = 0; if(size<2){ return dp; } for(int i=0;i<size-1;i++){ dp = dp + prices[i+1] - prices[i] > 0 ? dp + prices[i+1] - prices[i] : 0; max = dp > max ? dp : max; } return max; } };