• 算法——股票买卖问题


    无限次买卖

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
    设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
    注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
    链接: leetcode.

    解题思路:一次遍历,手里先握着一个价格,如果当前价格比自己高,那就卖出,添加利润,然后再买入。如果当前价格比自己低,那就直接买入。

    // created by lippon
    class Solution {
        public int maxProfit(int[] prices) {
            int res = 0;
            int n = prices.length; 
            if(n == 0) return res;
            int cur = prices[0];
    
            for(int i = 1; i < n; i++) {
                if(prices[i] > cur) {
                    res += prices[i] - cur;
                    cur = prices[i];
                } else {
                    cur = prices[i];
                }
            }
    
            return res;
        }
    }
    

    只有两次买卖机会

    给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
    设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
    注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
    链接: leetcode.

    解题思路:可以将问题分解为,如果找到一个中间点,使得中间点左右两段中一次买卖的利润和最大。这样,就需要两个遍历,一个顺序一个逆序,动态规划,找到以每个元素结尾的最大利润。

    class Solution {
        public int maxProfit(int[] prices) {
            int n = prices.length;
            if(n == 0) return 0;
            int[] f1 = new int[n], f2 = new int[n];
            int min = prices[0], max = prices[n - 1];
    
    
            for(int i = 1; i < n; i ++) {
                f1[i] = Math.max(f1[i - 1], prices[i] - min);
                min = Math.min(min, prices[i]);
            }
    
            for(int i = n - 2; i >= 0; i --) {
                f2[i] = Math.max(f2[i + 1], max - prices[i]);
                max = Math.max(max, prices[i]);
            }
    
            int res = 0;
            for(int i = 0; i < n; i++) {
                res = Math.max(res, f1[i] + f2[i]);
            }
    
            return res;
    
        }
    }
    

    含有冷冻期

    给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。​
    设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
    你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
    卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。

    class Solution {
        public int maxProfit(int[] prices) {
            int res = 0;
            int n = prices.length;
            int[][] f = new int[n][3];
    
            if(n == 0) return 0;
    
            f[0][1] = -prices[0];
            f[0][0] = 0;
            for(int i = 1; i < n; i++) {
            	// 第i天不持有股票,则前一天可能卖出了股票,也可能不持有股票
                f[i][0] = Math.max(f[i - 1][2], f[i - 1][0]);
                // 当天持有股票,则前一天可能持有股票,也可能当天买入了股票
                f[i][1] = Math.max(f[i - 1][1], f[i - 1][0] - prices[i]);
                // 当天卖出了股票,则前一天一定持有股票
                f[i][2] = f[i - 1][1] + prices[i];
            }
    
            return Math.max(f[n - 1][0], Math.max(f[n - 1][1], f[n - 1][2]));
        }
    }
    
  • 相关阅读:
    git rebase命令
    java中HashSet对象内的元素的hashCode值不能变化
    Spring中WebApplicationInitializer的理解
    mysql判断表字段或索引是否存在,然后修改
    mysql存储过程
    判断地图上的点是否在圆形,多边形,区域内
    计算任意多边形的面积、中心、重心
    判断点是否在任意多边形内
    springMvc将对象json返回时自动忽略掉对象中的特定属性的注解方式
    String.format()详细用法
  • 原文地址:https://www.cnblogs.com/lippon/p/14117703.html
Copyright © 2020-2023  润新知