1+ package com .leetcode .arrays ;
2+
3+ /**
4+ * Level: Easy
5+ * Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
6+ * Problem Description:
7+ * Say you have an array for which the ith element is the price of a given stock on day i.
8+ * <p>
9+ * Design an algorithm to find the maximum profit. You may complete as many transactions as you
10+ * like (i.e., buy one and sell one share of the stock multiple times).
11+ * <p>
12+ * Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock
13+ * before you buy again).
14+ *
15+ * @author rampatra
16+ * @since 2019-04-23
17+ */
18+ public class BuySellStocksII {
19+
20+ /**
21+ * The key point is we need to consider every peak immediately following a valley to maximize the profit. In case
22+ * we skip one of the peaks (trying to obtain more profit), we will end up losing the profit over one of the
23+ * transactions leading to an overall lesser profit.
24+ * <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/solution/">Read this to learn more</a>.
25+ * <p>
26+ * Time complexity: O(n)
27+ * where,
28+ * n = no. of stock prices
29+ * <p>
30+ * Runtime: <a href="https://leetcode.com/submissions/detail/224655101/">0 ms</a>.
31+ *
32+ * @param prices
33+ * @return
34+ */
35+ public static int maxProfit (int [] prices ) {
36+ int valley ;
37+ int peak ;
38+ int maxProfit = 0 ;
39+
40+ for (int i = 0 ; i < prices .length ; i ++) {
41+ while (i < prices .length - 1 && prices [i ] > prices [i + 1 ]) {
42+ i ++;
43+ }
44+ valley = i ;
45+
46+ while (i < prices .length - 1 && prices [i ] < prices [i + 1 ]) {
47+ i ++;
48+ }
49+ peak = i ;
50+
51+ maxProfit += prices [peak ] - prices [valley ];
52+ }
53+
54+ return maxProfit ;
55+ }
56+
57+ /**
58+ * This solution follows the logic used in the above approach {@link #maxProfit(int[])}, but with only a slight
59+ * variation. In this case, instead of looking for every peak following a valley, we can simply go on crawling over
60+ * the slope and keep on adding the profit obtained from every consecutive transaction.
61+ * In the end, we will be using the peaks and valleys effectively, but we need not track the costs corresponding to
62+ * the peaks and valleys along with the maximum profit, but we can directly keep on adding the difference between the
63+ * consecutive numbers of the array if the second number is larger than the first one, and at the total sum we obtain
64+ * will be the maximum profit. This approach will simplify the solution.
65+ * <p>
66+ * Time complexity: O(n)
67+ * where,
68+ * n = no. of stock prices
69+ *
70+ * @param prices
71+ * @return
72+ */
73+ public static int maxProfitSimplified (int [] prices ) {
74+ int maxProfit = 0 ;
75+ for (int i = 1 ; i < prices .length ; i ++) {
76+ if (prices [i ] > prices [i - 1 ]) {
77+ maxProfit += prices [i ] - prices [i - 1 ];
78+ }
79+ }
80+ return maxProfit ;
81+ }
82+
83+ public static void main (String [] args ) {
84+ System .out .println (maxProfit (new int []{7 , 1 , 5 , 3 , 6 , 4 }));
85+ System .out .println (maxProfit (new int []{1 , 2 , 3 , 4 , 5 }));
86+ System .out .println (maxProfit (new int []{7 , 6 , 4 , 3 , 1 }));
87+
88+ System .out .println ("----" );
89+
90+ System .out .println (maxProfitSimplified (new int []{7 , 1 , 5 , 3 , 6 , 4 }));
91+ System .out .println (maxProfitSimplified (new int []{1 , 2 , 3 , 4 , 5 }));
92+ System .out .println (maxProfitSimplified (new int []{7 , 6 , 4 , 3 , 1 }));
93+ }
94+ }
0 commit comments