• leetcode 股票相关


    121.买卖股票的最佳时机
    package com.example.lettcode.test;
    
    /**
     * @Class MaxProfit
     * @Description 121 买卖股票的最佳时机
     * 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
     * 如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),
     * 设计一个算法来计算你所能获取的最大利润。
     * 注意:你不能在买入股票前卖出股票。
     * <p>
     * 示例 1:
     * 输入: [7,1,5,3,6,4]
     * 输出: 5
     * 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,
     * 最大利润 = 6-1 = 5 。
     * 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
     * <p>
     * 示例 2:
     * 输入: [7,6,4,3,1]
     * 输出: 0
     * 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
     * @Author
     * @Date 2020/7/10
     **/
    public class MaxProfit_121 {
    
        /**
         * 解法1:
         */
        /*public static int maxProfit(int[] prices) {
            if (prices == null || prices.length <= 1) return 0;
            int minPrice = prices[0];
            int maxProfit = 0;
            for (int i = 1; i < prices.length; i++) {
                minPrice = Math.min(minPrice, prices[i]);
                maxProfit = Math.max(maxProfit, prices[i] - minPrice);
            }
            return maxProfit;
        }*/
    
        /**
         * 解法2:动态规划
         */
        public static int maxProfit(int[] prices) {
            if (prices == null || prices.length <= 1) return 0;
    
            int minPrice = prices[0];
            int len = prices.length;
            int[] dp = new int[len];
            // dp[i] 表示前i天的最大利润
            dp[0] = 0;
            for (int i = 1; i < len; i++) {
                minPrice = Math.min(minPrice, prices[i]);
                dp[i] = Math.max(dp[i - 1], prices[i] - minPrice);
            }
            return dp[len - 1];
        }
    
        public static void main(String[] args) {
            int[] price = new int[]{7, 1, 5, 3, 6, 4};
            int ans = maxProfit(price);//5
            System.out.println("MaxProfit demo01 result:" + ans);
    
            price = new int[]{7, 6, 4, 3, 1};
            ans = maxProfit(price);//0
            System.out.println("MaxProfit demo02 result:" + ans);
        }
    }
    
    309 最佳买卖股票时机含冷冻期
    package com.example.lettcode.dailyexercises;
    
    /**
     * @Class MaxProfit
     * @Description 309 最佳买卖股票时机含冷冻期
     * 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。
     * 设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
     *
     * 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
     * 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
     *
     * 示例:
     * 输入: [1,2,3,0,2]
     * 输出: 3
     * 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
     * @Author
     * @Date 2020/7/10
     **/
    public class MaxProfit {
        public static int maxProfit(int[] prices) {
            if (prices == null || prices.length <= 1) return 0;
            int len = prices.length;
    
            // dp[i]表示第i天结束之后的累计最大收益」
            // dp[i][0]:手上持有股票的累计最大收益
            // dp[i][1]:手上不持有股票,且处于冷冻期的累计最大收益
            // dp[i][2]:手上不持有股票,且不处于冷冻期的累计最大收益
            int[][] dp = new int[len][3];
            // 相当于第0天只买入
            dp[0][0] = -1 * prices[0];
            ;
            // 第0天不持股且当天没卖出,相当于啥样每干
            dp[0][1] = 0;
            // 第0天不持股且当前卖出,相当于第0天买入又卖出
            dp[0][2] = 0;
    
            /**
             * 对于dp[i][0],我们目前持有的这一支股票可以是在第i-1天就已经持有的,对应的状态为dp[i-1][0];
             * 或者是第i天买入的,那么第i-1天就不能持有股票并且不处于冷冻期中,对应的状态为dp[i-1][2]加上买入股票的负收益
             * 因此状态转移方程为: dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - prices[i]);
             *
             * 对于dp[i][1],我们在第i天结束之后处于冷冻期的原因是在当天卖出了股票,那么说明在第i-1天时我们必须持有一支股票,
             * 对应的状态为dp[i-1][0]加上卖出股票的正收益
             * 因此状态转移方程为:dp[i][1] = dp[i - 1][0] + prices[i];
             *
             * 对于dp[i][2],我们在第i天结束之后不持有任何股票并且不处于冷冻期,说明当天没有进行任何操作,即第i-1天时不持有任何股票:
             * 如果不处于冷冻期,对应的状态为dp[i-1][1];
             * 如果处于冷冻期,对应的状态为dp[i-1][2];
             * 因此状态转移方程为: dp[i][2] = Math.max(dp[i - 1][1], dp[i - 1][2]);
             *
             * 总结:最后一天的最大收益有两种可能,而且一定是“不持有”状态下的两种可能,持有的话是没有意义的,
             * 把这两种“不持有”比较一下大小,返回即可
             */
            for (int i = 1; i < len; i++) {
                dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - prices[i]);
                dp[i][1] = dp[i - 1][0] + prices[i];
                dp[i][2] = Math.max(dp[i - 1][1], dp[i - 1][2]);
            }
            // 一定是“不持有”状态下的两种可能
            return Math.max(dp[len - 1][1], dp[len - 1][2]);
        }
    
        public static void main(String[] args) {
            int[] prices = new int[]{1, 2, 3, 0, 2};
            int ans = maxProfit(prices);
            System.out.println("MaxProfit demo01 result:" + ans);
    
            prices = new int[]{1, 2, 3, 1, 2, 3};
            ans = maxProfit(prices);
            System.out.println("MaxProfit demo02 result:" + ans);
        }
    }
    
  • 相关阅读:
    Ubuntu 12.04 LTS 及ubuntu14.10 -- NFS安装
    AutoFac文档4(转载)
    能粘贴Word 内容(含公式)的在线编辑器
    能粘贴Word 内容(含图片)的在线编辑器
    js+SpringBoot分片上传大文件
    js+SpringMVC分片上传大文件
    js+vue分片上传大文件
    js+csharp分片上传大文件
    js+c#.net分片上传大文件
    js+c#分片上传大文件
  • 原文地址:https://www.cnblogs.com/fyusac/p/13282736.html
Copyright © 2020-2023  润新知