【LeetCode】Best Time to Buy and Sell Stock I && II && III

1、Best Time to Buy and Sell Stock 
       Total Accepted: 7973 Total Submissions: 25534 My Submissions
       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.
2、Best Time to Buy and Sell Stock II
       Total Accepted: 7787 Total Submissions: 21875 My Submissions
       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).
3、Best Time to Buy and Sell Stock III 
      Total Accepted: 4982 Total Submissions: 22994 My Submissions
      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).
解题思路
        1和2基本思路一致,1是可以买卖1次,2买卖不限次数。
        先说1吧,其实就是一个扫描数组,求数组中数字相差最大值的过程,但是这个最大值,永远都是后者减去前者。
        先设置最大利润maxProfit = 0,初始最小值min = prices[0],然后从第2个元素开始,扫描一遍数组,计算int tempValue = prices[i]-min,如果tempValue比maxProfit大,就更新maxProfit值,同时比较min和prices[i]的大小关系,更新最小值。这一步用来保证买入的时候值最小,这样就能保证利润最大化。
        关于2,这个就容易多了,第i次买入,第i+1次卖出,如果prices[i+1] - prices[i] > 0,就可以累加到利润里。通俗来讲,就是现在知道每天的股票价格,那么不限次数的买入卖出,最大利润是多少。这样想的话,如果是自己去买股票的话,那么今天买,发现明天比今天股价高了,就卖出,同时再买入,后天发现又比买的时候股价高了,再卖出,同时买入。不断的买入卖出,累加就是最大利润。
        3比较难。允许2次买入卖出。在prices中,选取i,其中0到i买卖一次,i+1到len-1买卖一次,求出这两次买卖的利润,相加就是一次买卖的利润。
        从0到i次买卖一次,求最大利润,利用1的思路,可以直接求出来,但是这样会有很多重复计算。
        利用动态规划,声明两个数组arrayA,arrayB,其中arrayA[i]表示从0到i买卖一次的最大利润,arrayA[i]可根据arrayA[i-1]计算得到。
        arrayB[i]表示从i到len-1买卖一次的最大利润,逆向思维来求解arrayB[i],arrayB[i]可根据arrayB[i+1]计算得到。
        这样针对每个i,累加就可以得到最大利润。

1 Java AC

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0){
            return 0;
        }
        int len = prices.length;
        int maxProfit = 0;
        for(int i = 1; i < len; i++){
            int tempProfit = prices[i] - prices[i-1];
            if(maxProfit < tempProfit){
                maxProfit = tempProfit;
            }
            if(min > prices[i]){
                min = prices[i];
            }
        }
        return maxProfit;
    }
}
2 Java AC

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0){
            return 0;
        }
        int len = prices.length;
        int maxProfit = 0;
        for(int i = 1; i < len; i++){
            int tempProfit = prices[i] - prices[i-1];
            if(tempProfit > 0){
                maxProfit += tempProfit;
            }
        }
        return maxProfit;
    }
}
3 Java AC

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0){
            return 0;
        }
        int len = prices.length;
        int maxProfit = 0;
        int min = prices[0];
        int arrayA[] = new int[len];
        for(int i = 1; i < len; i++){
            arrayA[i] = prices[i] - min;
            arrayA[i] = arrayA[i] > arrayA[i-1] ? arrayA[i] : arrayA[i-1];
            if(prices[i] < min){
                min = prices[i];
            }
        }
        int max = prices[len-1];
        int arrayB[] = new int[len];
        for(int i = len-2; i >= 0; i--){
            arrayB[i] = max - prices[i];
            arrayB[i] = arrayB[i] > arrayB[i+1] ? arrayB[i] : arrayB[i+1];
            if(prices[i] > max){
                max = prices[i];
            }
        }
        for(int i = 0; i < len; i++){
            int tempValue = arrayA[i] + arrayB[i];
            maxProfit = maxProfit > tempValue ? maxProfit : tempValue;
        }
        return maxProfit;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值