best time to Buy and Sell Stock | & | | & III

Source: Internet
Author: User

Best time to Buy and Sell Stock I

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), design an AL Gorithm to find the maximum profit.

Example

Given array [3,2,3,1,2] , return 1 .

Analysis: Because the sell is always after buying, so as long as there is a lower buy price, we can update the buy price, if the price is lower than the buy price, we will update Tempmax. Look at the code and you'll understand.

1  Public classSolution {2     /**3 * @param prices:given an integer array4 * @return: Maximum profit5 * Cnblogs.com/beiyeqingteng6      */7      Public intMaxprofit (int[] prices) {8         if(Prices = =NULL|| Prices.length <=1)return 0;9         Ten         intTempmax =0; One         intBuyprice = prices[0]; A          -          for(inti =1; i < prices.length; i++) { -             if(Prices[i] >Buyprice) { theTempmax = Math.max (Tempmax, Prices[i]-buyprice); -}Else { -Buyprice =Prices[i]; -             } +         } -         returnTempmax; +     } A}
View CodeBest 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).

Example

Given An example [2,1,2,0,1], return 2

Analysis: Since allow unlimited trading, then, each price crest is a sell point, each price trough is a buy point.

1 classSolution {2     /**3 * @param prices:given an integer array4 * @return: Maximum profit5 * cnblogs.com/beiyeqingteng/6      */7      Public intMaxprofit (int[] prices) {8         if(Prices = =NULL|| Prices.length <=1)return 0;9         Ten         intBuyprice = prices[0]; One         intTotal =0; A          -          for(inti =1; i < prices.length; i++) { -             if(Prices[i] < prices[i-1]) { the                 if(Prices[i-1] >Buyprice) { -Total + = (Prices[i-1] -buyprice); -                 } -Buyprice =Prices[i]; +             } -         } +          A         if(Prices[prices.length-1] >Buyprice) { atTotal + = (Prices[prices.length-1] -buyprice); -         } -         returnTotal ; -     } -};
View Code

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.

Example

Given An example prices = [4,4,6,1,1,4,2,5] , return 6 .

Analysis:

Since the title says, you can only trade at most two times, so it could be one or two times. If you are trading only once, then we will find the only one transaction maxprofit from the start point (0) to the end point (prices.length-1). If only done two times, then two times can only be produced in (0, K) and (k + 1, prices.length-1), while the range of K is 0 <= K <= prices.length-1.

1 classSolution {2     /**3 * @param prices:given an integer array4 * @return: Maximum profit5 * cnblogs.com/beiyeqingteng/6      */7      Public intMaxprofit (int[] prices) {8         if(Prices = =NULL|| Prices.length <=1)return 0;9 Ten         intMax =0; One          for(inti =0; i < prices.length; i++) { A             if(i = = prices.length-1) { -max = Math.max (max, Maxprofit (Prices,0, i)); -}Else { themax = Math.max (max, Maxprofit (Prices,0, i) + maxprofit (prices, i +1, Prices.length-1)); -             } -         } -         returnMax; +     } -      +     //once one transaction is allowed from point I to J A     Private intMaxprofit (int[] Prices,intIintj) { at         if(I >= J)return 0; -         intProfit =0; -          -         intLowestprice =Prices[i]; -          -          for(intK = i +1; K <= J; k++) { in             if(Prices[k] >Lowestprice) { -Profit = Math.max (Profit, Prices[k]-lowestprice); to}Else { +Lowestprice =Prices[k]; -             } the         } *         returnprofit; $     }Panax Notoginseng};
View Code 

best time to Buy and Sell Stock | & | | & 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.