Classic best time to Buy and Sell Stock

Source: Internet
Author: User

There are a number of variations on this series of optimal values.

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

If you were-permitted to complete at most one transaction (ie, buy one and sell one share of the stock), des IGN an algorithm to find the maximum profit.

The simplest problem, there is a scenario: I came back from the future, I know a commodity in the back of the price of N days, the limit is only bought and sold once, then how I use the advantage of crossing back to make the biggest money with one chance.

Simple violence practices, TC is O (n^2), assuming that the global maximum profit is Gmax, by using Min to record the previously known minimum value, and then using the day I value minus min, is the best profit to be sold in I day. The entire process needs to update min o (n) times, update the maximum profit Gmax O (n) times, the total TC is O (n).

2. 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 could complete as many transactions as (ie, buy one and sell one share of the stock multiple times). However, engage in multiple transactions for the same time (ie, you must sell the stock before you buy again).

A similar scenario, but there are some restrictions that can be bought and sold multiple times, but not more than one inventory on hand. That is to say, there is only one stock of stocks problem.

Simple violence Practice, TC is O (2^n); But if there is such a scene, we will choose high clearance, low bottom, so with GMAX record maximum profit, with min record each minimum, with Max record the next maximum value, Gmax + = max-min update. The whole process, each day to update the minimum or maximum value once, a total of O (n) times, update gmax, the worst case o (n) times. So the total TC is O (n).

3. 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 .

The limit is more moderate, the situation is more general, up to two trades. That is to find two and the largest curve.

Simple violence, choose 4 days out, TC is O (n^4); From the 1th, we can get the maximum benefit from the No. 0 day to the first day; then flip the array over and get the maximum benefit from the nth day to the N-i day; and then, from 0 to N, go through it again, Finding the greatest benefit of the accumulated value before and after, I seamless connection with N-i also guarantees the possibility of only one transaction.

4. 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. Transactions at the most K.

This time the basic is no limit, the most general situation. According to the idea of the 3rd question, we can think of the practice is O (n^2 + n^ (k-1)) complexity. However, the complexity is too high. If there are M (valley,peak) pairs, M < K, select M pair, but M >= K, first select the first k large pairs, accumulate and gmax, and then the K to the middle of the k-1 gap from small to large arrangement, if the k+1 large contrast gap of the minimum value, then gmax + = diff until the k+i large number is smaller than the gap bounce value, or k+i = = m, then stop the algorithm.

5. 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 could complete as many transactions as (ie, buy one and sell one share of the stock multiple times) WI Th the following restrictions:

    • Engage in multiple transactions on the same time (ie, you must sell the stock before you buy again).
    • After you sell your the stock, you cannot buy the stock on next day. (ie, cooldown 1Day)

Compared to the 2nd question, more than one COOLDOWN,DP encountered this problem, usually the solution is one more record bit per node, to counteract the subsequent impact of cooldown. Downward trend, find the lowest point as the buy price, up to the top there are two options, sell or not sell, not sell is because the next day there may be a substantial decline, you can adjust the buy price at that time; opt[i][1] represents the total proceeds of the first day of sale, Opt[i][0] represents the total revenue not sold for the first day.

Classic best time to Buy and Sell Stock

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.