• 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;
    }

  • 相关阅读:
    Spring经典视频教程大集合
    使用jsonlib进行Java和JSON之间的转换
    c:import Unable to get RequestDispatcher for Context
    打开IE都会弹出欢迎界面“欢迎使用Internet Explorer 8”
    Struts2 标签 用法示例 (
    as3相关的frameworks
    as相关的框架引擎应用集合
    init
    XNA框架基础——疑难解答
    XNA框架基础——XNA的注意事项
  • 原文地址:https://www.cnblogs.com/niuyourou/p/16223700.html
Copyright © 2020-2023  润新知