[Leetcode] [JAVA] best time to Buy and Sell Stock I, II, III

Source: Internet
Author: User

best time to Buy and Sell Stock


Say you had an array for which the ith element was 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), design an AL Gorithm to find the maximum profit.

Buying and selling stocks can only be bought and sold once. Then simply traverse through, record the profit value and buy value, each encounter a greater profit value is updated, encountered a smaller buy value to update. In this way, the profit value calculated at each day I is the maximum profit that can be obtained by selling in the first days. Constantly update this profit, and finally get the maximum profit value.

1      Public intMaxprofit (int[] prices) {2         if(prices.length<=0)3             return0;4         intbuy = Prices[0];5         intBenifit = 0;6          for(inti=0;i<prices.length;i++) {7Benifit = Math.max (Benifit, prices[i]-buy);8buy =math.min (Buy, prices[i]);9         }Ten         returnbenifit; One}

best time to Buy and Sell Stock II

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

Buying and selling stocks indefinitely, seemingly more difficult, is actually simpler. Only need to get the total value of all climbing segments, that is, the overall maximum profit. So as long as the second day is more expensive than the first day, the difference is added to the total profit.

1      Public intMaxprofit (int[] prices) {2         intRe = 0;3          for(inti=1;i<prices.length;i++) {4             if(prices[i]>prices[i-1])5Re + = Prices[i]-prices[i-1];6         }7         returnre;8}

best time to Buy and Sell Stock III

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

Discussion someone has proposed a DP method for the K-sale situation, very good understanding. Here is a direct copy of his ideas:

F[k, ii] represents the maximum profit until Prices[ii] in the case of a maximum of K transactions.

//Transfer function: F[k, ii] = MAX (F[k, ii-1], Prices[ii]-PRICES[JJ] + f[k-1, JJ]) {JJ in range of [0, Ii-1]} = max (F[k, ii-1], Prices[ii] + max (f[k-1, JJ]-PRICES[JJ]))

Basic condition: f[0, ii] = 0; 0 trades will not be profitable

Basic situation: f[k, 0] = 0; If only one day there will be no profit

1      Public intMaxprofit (int[] prices) {2         if(prices.length<=1)3             return0;4         intk=2;5         int[] DP =New int[K+1][prices.length];6         intRe = 0;7          for(inti=1;i<=k;i++) {8             inttemp = Dp[i-1][0]-prices[0];9              for(intj=1;j<prices.length;j++) {Tentemp = Math.max (temp, dp[i-1][j]-prices[j]); OneDP[I][J] = Math.max (dp[i][j-1], prices[j]+temp); A             } -         } -         returnDp[k][prices.length-1]; the}

[Leetcode] [JAVA] best time to Buy and Sell Stock I, II, III

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.