best time to buy and sell stocks I II III IV

Source: Internet
Author: User

I

Suppose there is an array, the first element of which is the price of a given stock in the first day. If you are allowed to complete only one transaction at most (for example, buying and selling a stock), design an algorithm to find out the maximum profit.

Ii

Suppose there is an array, the first element of which is the price of a given stock in the first day. Design an algorithm to find the maximum profit. You can do as many trades as possible (buy and sell stocks multiple times). However, you cannot participate in multiple transactions at the same time (you must sell the stock before you buy again).

Iii

Suppose you have an array, the first element of which is the price of a given stock in the first day. Design an algorithm to find the maximum profit. You can complete up to two trades.

Sample Example

Give a sample array [4,4,6,1,1,4,2,5], return 6


IV

Suppose you have an array, the first element of which is the price of a given stock in the first day.

Design an algorithm to find the maximum profit. You can complete the k transaction at most.

Attention

You may not participate in multiple transactions at the same time (you must sell the stock before you buy again)

Leetcode "best time to Buy and Sell Stock I II III IV Problem Solving report

best time to Buy and Sell Stock I

Test Instructions: an array representing the price of the stock per day, and the number of I in the array represents the price of the stock on the first day. If only one transaction is allowed, that is to say, buy only one stock and sell it for maximum benefit.

Analysis: dynamic Programming method. From the previous traversal of the array, record the current lowest price, as the buy price, and calculate the return on the day of the sale of the proceeds, as the maximum possible benefits, throughout the process, the greatest benefit is the demand.

code: time O (n), Space O (1).

best time to Buy and Sell Stock II

title: use an array to represent the price of the stock each day, and the number of I in the array represents the price of the stock on the first day. There is no limit to the number of trades, but only one stock can be traded at a time, that is, only one stock can be held on hand, seeking maximum profit.

Analysis: greedy method. The array is traversed backwards, as long as the price of the day is higher than the previous one, even if it proceeds.

code: time O (n), Space O (1).

best time to Buy and Sell Stock III

Test Instructions: an array representing the price of the stock per day, and the number of I in the array represents the price of the stock on the first day. Trade up to two times, with up to one stock on hand, for maximum benefit.

Analysis: dynamic Programming method. To calculate the maximum yield of a transaction prior to the first day of the Preprofit[i], and the maximum benefit of one transaction after the first day of the day (s) as a dividing line (i) Postprofit[i], the last traversal, Max{preprofit[i] + postprofit[i]} ( 0≤I≤N-1) is the biggest benefit. The maximum benefit for the first day before and after the first day is calculated with best time to Buy and Sell Stock I.

code: time O (n), space O (n).

best time to Buy and Sell Stock IV

Test Instructions: an array representing the price of the stock per day, and the number of I in the array represents the price of the stock on the first day. Most trades k times, the hand can only hold one stock, seek the maximum benefit.

Analysis: Special Dynamic Programming method. Traditional dynamic programming we would like to think that the maximum benefit of J transactions on the first day of trading is either the maximum benefit of the J-Transaction at I-1 (the price of I is lower than the price of the i-1 day) or the j-1 transaction on the i-1. Then the first day of the transaction (the price of the first day is higher than the price of I-1 day). The equation of motion is obtained as follows (where diff = prices[i]–prices[i–1]):

PROFIT[I][J] = max (Profit[i–1][j], profit[i–1][j–1] + diff)

It seems reasonable, but it's not right, why not? Because diff is the difference between day I and day i-1, if there is a transaction on the day of day I-1, then the two trades can be traded as a single transaction, so that profit[i–1][j–1] + diff actually carries out only j-1 transactions, not a maximum of J times, So the biggest gains will be small.

So how do you calculate the maximum benefit of trading on the first day of the transaction before you avoid having to calculate a transaction less? We use a local optimal solution and the global most solutions to the first day of the J-time proceeds, this is the dynamic planning of the special.

Using local[i][j] means to reach the first day, the most local optimal solution of J transactions, with Global[i][j] to reach the first day, up to J times global optimal solution. The relationship between them is as follows (where diff = prices[i]–prices[i–1]):

LOCAL[I][J] = max (global[i–1][j–1] + max (diff, 0), Local[i–1][j] + diff)
GLOBAL[I][J] = max (Global[i–1][j], local[i][j])

One of the local[i–1][j] + diff is to avoid a single transaction and one less return on trading with the first day trading and the I-1 day transaction. Reference: http://www.cnblogs.com/grandyang/p/4295761.html

code: time O (n), Space O (k).

best time to buy and sell stocks I II III 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.