LeetCode 121&&122. Best Time to Buy and Sell Stock (Java)

题目一:

(只可购入并卖出一次)
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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

解答:

//第一次,采用最简单的遍历循环,找出数组中的最大差
//时间复杂度为O(n²)
class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length==0||prices.length<=1){
            return 0;
        } 
        int num=0;
        for(int i=0;i<=prices.length;i++){
            for(int j=i+1;j<prices.length;j++){
                if(prices[j]-prices[i]>num){
                    num=prices[j]-prices[i];
                }
            }
        }
        return num;
    }
}
//第二次,用min表示数组中的最小值,用max始终表示最大收益,采用一重循环
//遍历更新的思想
//一旦碰到比min更小的值,就令min等于这个值,然后继续遍历;如果遍历到的值比当前的min值大,就求这个值与min值的差值,如果这个max比之前的max要大,则更新这个max值
//时间复杂度为O(n)
class Solution {
    public int maxProfit(int[] prices) {
        if(prices==null||prices.length<=1){
            return 0;
        }
        int min=prices[0];
        int max=0;
        for(int i=0;i<prices.length;i++){
            if(prices[i]<min){
                min=prices[i];
            }
            else{
                if(max<prices[i]-min){
                    max=prices[i]-min;
                }
            }
        }
        return max;
    }
}

题目二:

(可以购入并且卖出多次)
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 (i.e., buy one and sell one share of the stock multiple times).

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

Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.

Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

解答:

//基本思路与购入卖出一次相似
//注意卖出的当天也可以同时买入
//我们可以直接持续增加数组的连续数字之间的差值,如果第二个数字大于第一个数字,我们获得的总和将是最大利润,以简化解决方案
//如下图,A+B+C的和等于差值D所对应的连续峰和谷的高度之差
class Solution {
    public int maxProfit(int[] prices) {
        if(prices==null||prices.length<=1){
            return 0;
        }
        int min=prices[0];
        int max=0;
        for(int i=0;i<prices.length;i++){
            if(min<prices[i]){
                max+=prices[i]-min;
            }
            min=prices[i];
        }
        return max;
    }
}

LeetCode 121&&122. Best Time to Buy and Sell Stock (Java)_第1张图片

你可能感兴趣的:(LeetCode)