Say you have an array for which the i<sup style="box-sizing: border-box; position: relative; font-size: 12px; line-height: 0; vertical-align: baseline; top: -0.5em;">th element is the price of a given stock on day i.
給定一個數組,其中第i個元素是第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.
如果你僅被允許交易一次(即買一支股票和賣一支股票),設計一個算法來找到最大的利潤
這個題目有點繞,其實就是在一個數組中找到差值(利潤)最大的兩個數,但是關鍵在于題目設定的賣出的價格必須大于買進的價格,也就是后面的數要大于前面的數,這就難了,如果是通常的判斷,勢必要每一個數都要判斷一遍,因為還沒有循環到后面的事后你不知道到底后面有沒有更大的數
For example 1
Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
For example 2
Input: [7, 6, 4, 3, 1]
Output: 0
In this case, no transaction is done, i.e. max profit = 0.
My Solution
(Java) Version 1 Time: Time Limit Exceeded:
樸素的超時的方法
public class Solution {
public int maxProfit(int[] prices) {
int min=0;
for(int i=0;i<prices.length-1;i++){
for(int j=i+1;j<prices.length;j++){
int temp=prices[i]-prices[j];
if(temp<min)min=temp;
}
}
return -min;
}
}
(Java) Version 2 Time: 2ms:
解決超時的問題,首先肯定是減小計算量,我們可以發現在超時算法中有很多不必要的判斷步驟,比如[1,2,3,4]中,如果我們找到了1和4那么判斷2,3顯然是多余,所以可以以找到的第一個最大的數為界(這個數的下一個比它小),然后計算多個“最大值”然后比較這些最大值找到最大的值
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length==1||prices.length==0)return 0;
int max=0,tempmax=0,a=prices[0],b=prices[0];
for(int i=1;i<prices.length;i++){
if(prices[i]>b){
b=prices[i];
tempmax=b-a;
}else if(prices[i]<a){
max=tempmax>max?tempmax:max;
a=prices[i];
b=prices[i];
tempmax=b-a;
}else{
continue;
}
}
return tempmax>max?tempmax:max;
}
}
(Java) Version 3 Time: 2ms (By reddy2005):
從頭開始遍歷,遇到小于min的就存下了,遇到大于min的就把這個數減去min的差保存下來,然后讓max在后面跟著判斷,找到最大的
public class Solution {
public int maxProfit(int[] prices) {
int len=prices.length;
if(len==0) return 0;
int min=prices[0],max=0;
for(int i=0;i<len;i++){
if(prices[i]<min) min=prices[i];
prices[i]-=min;
if(max<prices[i]) max=prices[i];
}
return max;
}
}
(Java) Version 4 Time: 3ms (By tiffanyTown):
據說這是一個DP解法,然而DP我并不是很懂,所以只能留到以后再看了
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length<2) return 0;
int[] dp=new int[prices.length];
int minPrice=prices[0];
dp[0]=0;
for(int i=1;i<prices.length;i++){
if(prices[i]>=minPrice){
dp[i]=Math.max(dp[i-1],prices[i]-minPrice);
}
else{
minPrice=prices[i];
dp[i]=dp[i-1];
}
}
return dp[prices.length-1];
}
}
(Java) Version 5 Time: 3ms (By anton15):
總有些人就是想搞個大新聞,瘋狂使用Java自帶函數,來達到盡可能少的代碼,這很6
/*
*Proper Java - 6 lines:
*/
public class Solution {
public int maxProfit(int[] prices) {
int min = Integer.MAX_VALUE, max = 0;
for (int i = 0; i < prices.length; i++) {
min = Math.min(min, prices[i]);
max = Math.max(max, prices[i] - min);
}
return max;
}
}
/*
*Proper Java with shortcuts - 4 lines:
*/
public class Solution {
public int maxProfit(int[] prices) {
int min = Integer.MAX_VALUE, max = 0;
for (int i = 0; i < prices.length; i++)
max = Math.max(max, prices[i] - (min = Math.min(min, prices[i])));
return max;
}
}
/*
*Java 8 streams - 2 lines:
*/
public class Solution {
int min = Integer.MAX_VALUE;
public int maxProfit(int[] prices) {
return Arrays.stream(prices).map(i -> i - (min = Math.min(min, i))).max().orElse(0);
}
}