Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most k transactions.

解题思路:

本题是Best Time to Buy and Sell Stock系列最难的一道,需要用到dp,JAVA实现如下:

    public int maxProfit(int k, int[] prices) {
        if (k == 0 || prices.length < 2)
            return 0;
        if (k > prices.length / 2) {
            int maxProfitII = 0;
            for (int i = 1; i < prices.length; ++i)
                if (prices[i] > prices[i - 1])
                	maxProfitII += prices[i] - prices[i - 1];
            return maxProfitII;
        }
        int[] buy=new int[k];
        int[] sell=new int[k];
        for(int i=0;i<buy.length;i++)
        	buy[i]=Integer.MIN_VALUE;
        for (int i = 0; i < prices.length; i++)
            for (int j = k - 1; j >= 0; j--) {
                sell[j] = Math.max(sell[j], buy[j] + prices[i]);
                if (j == 0)
                    buy[j] = Math.max(buy[j], -prices[i]);
                else
                    buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]);
            }
        return sell[k - 1];
    }
时间: 2024-10-07 18:58:44

Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】的相关文章

Leetcode #188 Best Time to Buy and Sell Stock IV

题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 当 k ≥ prices.size() / 2 时:题目等价于 k 无限大的情形. 当 k < prices.size() / 2 时: 用dp[m][n+1]表示在[0,n]区间,进行了m次买卖操作,获得的最大利润. 那么这个利润必为以下几个数据的最大值: dp[m-1][n+1],即在[0,n]区间,进行了m-1次买卖操作,获得的最大利润. dp[m]

LeetCode 188. Best Time to Buy and Sell Stock IV (动态规划)

题目 题意:给你一个数组代表每天的股价.你有k次买入和卖出的机会,问你最多能赚多少钱.买入之前必须卖出已有股份.同一天是可以先买,再卖,或者先卖再买的. 题解:题目没有说数据范围,但是经过我实际测试 k 最大为10^8 ,n最大为10^4.当然k最多只需要取n/2就好了,因为当天买当天卖是没有意义的. 那这道题的效率就应该控制在O(n*k),再加一点就要超时了,所以两个循环嵌套.第一层是k,第二层是n 状态数组dp[k][i] 表示开始第k次买卖的交易时,第i天口袋里可以赚到的最多的钱.那么状态

188. Best Time to Buy and Sell Stock IV (Array; DP)

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note:You may not engage in multiple transactions at the same time (ie, yo

Java for LeetCode 121 Best Time to Buy and Sell Stock

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 (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 解题思路

Java for LeetCode 123 Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (ie,

Java for LeetCode 122 Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

188. Best Time to Buy and Sell Stock IV leetcode解题笔记

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note:You may not engage in multiple transactions at the same time (ie, yo

188. Best Time to Buy and Sell Stock IV Leetcode Python

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, y

leetcode解题报告:188 Best Time to Buy and Sell Stock IV

问题: 给定一个列表,第i个元素代表股票第i天的价值,最多只允许买入卖出k次,求最大收益 思路:动态规划 输入为列表p1p2...pm 代码:Python