• Leetcode 714 买卖股票的最佳时机含手续费 dp


     

      C:

    #include <stdio.h>
    #include <stdlib.h>
    
    int dp(int *prices, int fee, int point, int hasShared, int *cache)
    {
        if (point == 0)
        {
            if (hasShared == 0)
                return 0;
            else
                return -prices[0];
        }
        int key = point * 2 + hasShared;
        if (cache[key] != 0)
            return cache[key];
        int re = 0;
        if (hasShared == 0)
        {
            int seal = dp(prices, fee, point - 1, 1, cache) + prices[point] - fee;
            int keep = dp(prices, fee, point - 1, 0, cache);
            re = seal > keep ? seal : keep;
        }
        else
        {
            int add = dp(prices, fee, point - 1, 0, cache) - prices[point];
            int keep = dp(prices, fee, point - 1, 1, cache);
            re = add > keep ? add : keep;
        }
        cache[key] = re;
        return re;
    }
    
    int maxProfit(int *prices, int pricesSize, int fee)
    {
        int len = pricesSize * 2;
        int *cache = (int *)malloc(sizeof(int) * len);
        for (int i = 0; i < len; i++)
            cache[i] = 0;
        int re = dp(prices, fee, pricesSize - 1, 0, cache);
        free(cache);
        return re;
    }

      JAVA:

     public final int maxProfit(int[] prices, int fee) {
            int len = prices.length;
            int[][] cache = new int[len][2];
            return max(prices, fee, len - 1, 0, cache);
        }
    
        private final int max(int[] prices, int fee, int point, int hasShares, int[][] cache) {
            if (point == 0 && hasShares == 0) return 0;
            if (point == 0 && hasShares == 1) return -prices[0];
            if (cache[point][hasShares] != 0) return cache[point][hasShares];
            int re;
            if (hasShares == 0) {
                re = Math.max(max(prices, fee, point - 1, 0, cache),
                        max(prices, fee, point - 1, 1, cache) + prices[point] - fee);
            } else {
                re = Math.max(max(prices, fee, point - 1, 1, cache),
                        max(prices, fee, point - 1, 0, cache) - prices[point]);
            }
            cache[point][hasShares] = re;
            return re;
        }

      JS:

    /**
     * @param {number[]} prices
     * @param {number} fee
     * @return {number}
     */
    var maxProfit = function (prices, fee) {
        let cache = new Array(prices.length);
        for (let i = 0; i < prices.length; i++) cache[i] = new Array(2).fill(0);
        return dp(prices, fee, prices.length - 1, 0, cache);
    };
    
    var dp = function (prices, fee, point, hasShared, cache) {
        if (point == 0) {
            if (hasShared == 0) return 0;
            else return -prices[0];
        }
        if (cache[point][hasShared]) return cache[point][hasShared];
        let re = 0;
        if (hasShared) {
            let get = dp(prices, fee, point - 1, 0, cache) - prices[point];
            let keep = dp(prices, fee, point - 1, 1, cache);
            re = get > keep ? get : keep;
        } else {
            let seal = dp(prices, fee, point - 1, 1, cache) + prices[point] - fee;
            let keep = dp(prices, fee, point - 1, 0, cache);
            re = seal > keep ? seal : keep;
        }
        cache[point][hasShared] = re;
        return re;
    }

  • 相关阅读:
    挑战程序设计竞赛 第2章习题 poj 1017 Packets 贪心模拟
    挑战程序设计竞赛 2章习题 poj 2376 Cleaning Shifts
    Leetcode 27. 移除元素 双指针
    Leetcode 26. 删除有序数组中的重复项 双指针
    Leetcode 31. 下一个排列
    webserver 发布问题
    [转]机器学习中的各种距离
    VUE3 + TYPESCRIPT 开发实践总结
    我和ABP vNext 的故事
    ABP Framework 为什么好上手,不好深入?探讨最佳学习姿势!
  • 原文地址:https://www.cnblogs.com/niuyourou/p/16223700.html
Copyright © 2020-2023  润新知