Leetcode "best time to Buy and Sell Stock III" Python implementation

Source: Internet
Author: User
Tags diff

title :

Say you has an array for which the i-th element is the price of a given-stock on day I.

Design an algorithm to find the maximum profit. You are in most of the transactions.

Note:
Engage in multiple transactions on the same time (ie, you must sell the stock before you buy again).

Code : runtime:175 ms

1 classSolution:2     #@param prices, a list of integers3     #@return An integer4     defmaxprofit_with_k_transactions (self, prices, K):5Days =len (Prices)6Local_max = [[0 forIinchRange (k+1)] forIinchrange (days)]7Global_max = [[0 forIinchRange (k+1)] forIinchrange (days)]8          forIinchRange (1, days):9diff = prices[i]-prices[i-1]Ten              forJinchRange (1,k+1): OneLOCAL_MAX[I][J] = max (Local_max[i-1][j]+diff, global_max[i-1][j-1]+Max (diff,0)) AGLOBAL_MAX[I][J] = max (Local_max[i][j], global_max[i-1][j]) -         returnGlobal_max[days-1][k] -  the     defMaxprofit (Self, prices): -         ifPrices isNoneorLen (Prices) <2: -             return0 -         returnSelf.maxprofit_with_k_transactions (Prices, 2)

Ideas :

Not your own thinking, refer to this blog http://blog.csdn.net/fightforyourdream/article/details/14503469

The same as the above blog thinking is not repeated, the following is their own experience:

1. This type of topic, the ultimate train of thought must be to the dynamic planning, I myself summed up as " global optimal = all elements before the current element of the optimal or contains the current element of the optimal "

2. The difficulty with this problem is that the optimization cannot be accomplished by an iterative formula.

Ideas are as follows:

GLOBAL_MAX[I][J] = max (Global_max[i-1][j], local_max[i][j])

The idea of the iterative formula above is clear: "The global Optimal to the first element = The global optimal or contains the local optimal" of the current element, which does not contain the first element.

But here's the question, local_max[i][j] what is it? It can't be counted.

So why not have a dynamic solver for LOCAL_MAX[I][J]?

As a result, the following iteration formula is available:

LOCAL_MAX[I][J] = max (local_max[i-1][j]+diff, Global_max[i-1][j-1]+max (diff,0))

The above recursive formula regards Local_max as the goal of optimization, and the idea is to fellow the classical dynamic planning.

However, there is a part I have not figured out (blue word part), according to the classic dynamic planning ideas intuitive to analyze, it should be local_max[i-1][j] ah, how to come out a diff it?

Leetcode "best time to Buy and Sell Stock III" Python implementation

Related Article

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.