码迷,mamicode.com
首页 > 其他好文 > 详细

Best Time to Buy and Sell Stock | & || & III

时间:2016-07-05 10:19:51      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

Best Time to Buy and Sell Stock I

Say you have an array for which the ith element is the price of a given stock on day i.

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

Example

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

分析:因为卖出总是在买入后,所以,只要有更低的买入价格,我们就可以更新买入价格,如果价格比买入价格低,我们就更新tempMax。看代码后即可明白。

技术分享
 1 public class Solution {
 2     /**
 3      * @param prices: Given an integer array
 4      * @return: Maximum profit
 5      * cnblogs.com/beiyeqingteng
 6      */
 7     public int maxProfit(int[] prices) {
 8         if (prices == null || prices.length <= 1) return 0;
 9         
10         int tempMax = 0;
11         int buyPrice = prices[0];
12         
13         for (int i = 1; i < prices.length; i++) {
14             if (prices[i] > buyPrice) {
15                 tempMax = Math.max(tempMax, prices[i] - buyPrice);
16             } else {
17                 buyPrice = prices[i];
18             }
19         }
20         return tempMax;
21     }
22 }
View Code

Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Example

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

分析:既然允许unlimited 交易,那么,每个价格波峰都是卖出点,每个价格波谷都是买入点。

技术分享
 1 class Solution {
 2     /**
 3      * @param prices: Given an integer array
 4      * @return: Maximum profit
 5      * cnblogs.com/beiyeqingteng/
 6      */
 7     public int maxProfit(int[] prices) {
 8         if (prices == null || prices.length <= 1) return 0;
 9         
10         int buyPrice = prices[0];
11         int total = 0;
12         
13         for (int i = 1; i < prices.length; i++) {
14             if (prices[i] < prices[i - 1]) {
15                 if (prices[i - 1] > buyPrice) {
16                     total += (prices[i - 1] - buyPrice);
17                 }
18                 buyPrice = prices[i];
19             }
20         }
21         
22         if (prices[prices.length - 1] > buyPrice) {
23             total += (prices[prices.length - 1] - buyPrice);
24         }
25         return total;
26     }
27 };
View Code

 

Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions.

Example

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

分析:

既然题目说,最多只能交易两次,所以,可能是一次,也可能是两次。如果是只交易一次,那么我们就从开始点(0)到结束点(prices.length - 1) 找出只做一次交易的maxProfit. 如果只做两次,那么两次只能在 (0, k) 和 (k + 1, prices.length - 1) 产生,而k的范围是 0 <= k <= prices.length - 1.

技术分享
 1 class Solution {
 2     /**
 3      * @param prices: Given an integer array
 4      * @return: Maximum profit
 5      * cnblogs.com/beiyeqingteng/
 6      */
 7     public int maxProfit(int[] prices) {
 8         if (prices == null || prices.length <= 1) return 0;
 9 
10         int max = 0;
11         for (int i = 0; i < prices.length; i++) {
12             if (i == prices.length - 1) {
13                 max = Math.max(max, maxProfit(prices, 0, i));
14             } else {
15                 max = Math.max(max, maxProfit(prices, 0, i) + maxProfit(prices, i + 1, prices.length - 1));
16             }
17         }
18         return max;
19     }
20     
21     // once one transaction is allowed from point i to j
22     private int maxProfit(int[] prices, int i, int j) {
23         if (i >= j) return 0;
24         int profit = 0;
25         
26         int lowestPrice = prices[i];
27         
28         for (int k = i + 1; k <= j; k++) {
29             if (prices[k] > lowestPrice) {
30                 profit = Math.max(profit, prices[k] - lowestPrice);
31             } else {
32                lowestPrice =  prices[k];
33             }
34         }
35         return profit;
36     }
37 };
View Code

 

Best Time to Buy and Sell Stock | & || & III

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5628751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!