0
点赞
收藏
分享

微信扫一扫

LeetCode Top 100 Liked Questions 121. Best Time to Buy and Sell Stock (Java版; Easy)


LeetCode Top 100 Liked Questions 121. Best Time to Buy and Sell Stock (Java版; Easy)

题目描述

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.

第二次做; 核心: 1)根据状态转移图直接写的递归公式, 没有进行优化, 必须会话状态转移图 2)统一框架: 初始化各个s[0], 循环从i=1开始 3)状态转移图中入度为2的节点才有递推公式, 需要比较两个来源中哪一个更大 4)下面的代码可以优化, 因为s0是不变的,一直为0, 所以可以去掉s0状态, 直接用0表示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0uDgE7i1-1583720445897)(https://note.youdao.com/yws/public/resource/e96cb69c31962d0c71dc627efb67fa99/xmlnote/FE40BA86875D48F38A2ECE403CB6E7C5/66212)]

//代码可以优化
class Solution {
    public int maxProfit(int[] prices) {
        //input check
        if(prices==null || prices.length==0)
            return 0;
        int n = prices.length;
        int[] s0 = new int[n];
        int[] s1 = new int[n];
        int[] s2 = new int[n];
        //初始化
        s1[0] = -prices[0];
        for(int i=1; i<n; i++){
            s0[i] = s0[i-1];
            s1[i] = Math.max(s1[i-1], s0[i-1]-prices[i]);
            s2[i] = Math.max(s2[i-1], s1[i-1]+prices[i]);
        }
        return s2[n-1];
    }
}

//优化后: 去掉s0数组
/*
class Solution {
    public int maxProfit(int[] prices) {
        //input check
        if(prices==null || prices.length==0)
            return 0;
        int n = prices.length;
        int[] s1 = new int[n];
        int[] s2 = new int[n];
        //初始化
        s1[0] = -prices[0];
        for(int i=1; i<n; i++){
            s1[i] = Math.max(s1[i-1], -prices[i]);
            s2[i] = Math.max(s2[i-1], s1[i-1]+prices[i]);
        }
        return s2[n-1];
    }
}
*/

第一次做; 数组看似无序, 其实是按照日期排序的; 下面的做法是动态规划的思想 dp[i][0] =max(dp[i-1][0], dp[i-1][1]+prices[i]); dp[i][1] = max(dp[i-1][1], dp[0][0]-prices[i])

/*
数组看似无序, 其实不然, 是按照日期排序的
从左往右遍历数组, 维护一个数组的最小值变量, 以及一个收益最大值变量
每一次遍历:
    如果prices[i]小于min,则更新min,进入下一轮循环
    如果prices[i]不小于min,则根据Math.max()更新max, 进入下一轮循环
*/
import java.util.LinkedList;

class Solution {
    public int maxProfit(int[] prices) {
        if(prices==null || prices.length<2)
            return 0;
        //最小值
        int min = Integer.MAX_VALUE;
        int max = 0;
        for(int i=0; i<prices.length; i++){
            if(prices[i] < min){
                min = prices[i];
            }
            else{
                max = Math.max(max, prices[i] - min);
            }
        }
        return max;
    }
}

极其强大的题解, 原文链接

LeetCode Top 100 Liked Questions 121. Best Time to Buy and Sell Stock (Java版; Easy)_状态转移图


举报

相关推荐

0 条评论