Leetcode #188 best time to Buy and Sell Stock IV

Source: Internet
Author: User

Title Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/

When K≥prices.size ()/2 o'clock: the title is equivalent to the case of K infinity.

When K < Prices.size ()/2 o'clock:

Using dp[m][n+1] to represent the [0,n] interval, the M-sale operation, the maximum profit obtained.

Then this profit must be the maximum value of the following data:

  • Dp[m-1][n+1], that is, in the [0,n] interval, the m-1 trading operation, the maximum profit obtained.
  • Dp[m][n], that is, in the [0,n-1] interval, the M-sale operation, the maximum profit obtained.

    or a

  • Max (Dp[m-1][z] + (Prices[n]-prices[z]), that is, in the [0,z-1] interval, a m-1 trade operation, and the last buy at z time, the last time to sell at n time, the maximum profit obtained.

While the value of n is fixed for Each loop, the value of Z < n is constantly updated, so it is rewritten as Prices[n] + max (Dp[m-1][z]-prices[z]).

Leetcode #188 best time to Buy and Sell Stock IV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.