# Best Time to Buy and Sell Stock I && II && III

### 题目1：Best Time to Buy and Sell Stock

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.

AC代码:
public class Solution {

public int maxProfit(int[] prices) {
int size = prices.length;
if (size == 0){
return 0;
}
int maxPrice = prices[size-1];//初始化最大price
int maxMoney = 0;//初始化利润值
for (int i=size-1; i>=0; --i){
maxPrice = maxPrice > prices[i] ? maxPrice : prices[i];//如果第i天的值大于最大price,则更新最大price的值
maxMoney = maxMoney > (maxPrice - prices[i]) ? maxMoney : (maxPrice - prices[i]);//更新最大利润值
}
return maxMoney;
}
}

### 题目2：Best Time to Buy and Sell Stock II

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).

1、第1天买入(price==1)，第2天卖出 (price==3), 第3天买入(price==4),第4天卖出(price==10)  ：   8
2、第1天买入(price==1)，第3天卖出(price==4)       ： 3
3、第2天买入 (price==3)，第3天卖出(price==4)       ： 1
4、第3天买入(price==4)，第4天卖出(price==10)     ： 6
5、第1天买入 (price==1)，第4天卖出(price==10)     ： 9

3 - 1 = 2
4 - 3 = 1
10 - 4 = 6

index :  1 ~~ prices.size()-1

AC代码：
public class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
int size = prices.length;
if (size < 2){
return profit;
}
for (int index=1; index<size; ++index){
int value = prices[index] - prices[index-1];
if (value > 0){
profit += value;
}
}
return profit;
}
}

### Best Time to Buy and Sell Stock III

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 at most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

AC代码：
public class Solution {
public int maxProfit(int[] prices) {
int size = prices.length;
if (size < 2)
return 0;
int[] left = new int[size];
int[] right = new int[size];
int minValue = prices;
int maxValue = prices[size-1];
for (int i=1; i<size; ++i){
left[i] = left[i-1] > (prices[i] - minValue) ? left[i-1] : (prices[i] - minValue);
minValue = minValue < prices[i] ? minValue : prices[i];
}
for (int i=size-2; i>=0; --i){
right[i] = right[i+1] > (maxValue - prices[i]) ? right[i+1] : (maxValue - prices[i]);
maxValue = maxValue > prices[i] ? maxValue : prices[i];
}
int profit=0;
for (int i=0; i<size; ++i){
profit = profit > (left[i] + right[i]) ? profit : (left[i] + right[i]);
}
return profit;
}
}                                                                          03-15 28
03-20 255
07-24 4752
07-22 101
03-09 133
07-28 562
04-09 42
03-28 214
04-07 84
06-11 19
02-18 5259
08-27 17
03-17 397
12-25 877
08-10 45
05-16 332
04-05 1万+
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客