This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

Say you have 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 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).

b'

\n## Summary

\n## Solution

\n

\n#### Approach #1 Brute Force [Time Limit Exceeded]

\n\n

\n#### Approach #2 (Peak Valley Approach) [Accepted]

\n\n

\n#### Approach #3 (Simple One Pass) [Accepted]

\n\n

'
\n\n

\nWe have to determine the maximum profit that can be obtained by making the transactions (no limit on the number of transactions done). For this we need to find out those sets of buying and selling prices which together lead to the maximization of profit.

\n\n

In this case, we simply calculate the profit corresponding to all the possible sets of transactions and find out the maximum profit out of them.

\n**Java**

class Solution {\n public int maxProfit(int[] prices) {\n return calculate(prices, 0);\n }\n\n public int calculate(int prices[], int s) {\n if (s >= prices.length)\n return 0;\n int max = 0;\n for (int start = s; start < prices.length; start++) {\n int maxprofit = 0;\n for (int i = start + 1; i < prices.length; i++) {\n if (prices[start] < prices[i]) {\n int profit = calculate(prices, i + 1) + prices[i] - prices[start];\n if (profit > maxprofit)\n maxprofit = profit;\n }\n }\n if (maxprofit > max)\n max = maxprofit;\n }\n return max;\n }\n}\n

**Complexity Analysis**

- \n
- Time complexity : . Recursive function is called times. \n
- Space complexity : . Depth of recursion is . \n

\n

**Algorithm**

Say the given array is:

\n[7, 1, 5, 3, 6, 4].

\nIf we plot the numbers of the given array on a graph, we get:

\n\nIf we analyze the graph, we notice that the points of interest are the consecutive valleys and peaks.

\nMathematically speaking:\n\n

\nThe key point is we need to consider every peak immediately following a valley to maximize the profit. In case we skip one of the peaks (trying to obtain more profit), we will end up losing the profit over one of the transactions leading to an overall lesser profit.

\nFor example, in the above case, if we skip and trying to obtain more profit by considering points with more difference in heights, the net profit obtained will always be lesser than the one obtained by including them, since will always be lesser than .

\n**Java**

class Solution {\n public int maxProfit(int[] prices) {\n int i = 0;\n int valley = prices[0];\n int peak = prices[0];\n int maxprofit = 0;\n while (i < prices.length - 1) {\n while (i < prices.length - 1 && prices[i] >= prices[i + 1])\n i++;\n valley = prices[i];\n while (i < prices.length - 1 && prices[i] <= prices[i + 1])\n i++;\n peak = prices[i];\n maxprofit += peak - valley;\n }\n return maxprofit;\n }\n}\n

**Complexity Analysis**

- \n
- \n
Time complexity : . Single pass.

\n \n - \n
Space complexity : . Constant space required.

\n \n

\n

**Algorithm**

This solution follows the logic used in Approach 2 itself, but with only a slight variation. In this case, instead of looking for every peak following a valley, we can simply go on crawling over the slope and keep on adding the profit obtained from every consecutive transaction. In the end,we will be using the peaks and valleys effectively, but we need not track the costs corresponding to the peaks and valleys along with the maximum profit, but we can directly keep on adding the difference between the consecutive numbers of the array if the second number is larger than the first one, and at the total sum we obtain will be the maximum profit. This approach will simplify the solution.\nThis can be made clearer by taking this example:

\n[1, 7, 2, 3, 6, 7, 6, 7]

\nThe graph corresponding to this array is:

\n\nFrom the above graph, we can observe that the sum is equal to the difference corresponding to the difference between the heights of the consecutive peak and valley.

\n**Java**

class Solution {\n public int maxProfit(int[] prices) {\n int maxprofit = 0;\n for (int i = 1; i < prices.length; i++) {\n if (prices[i] > prices[i - 1])\n maxprofit += prices[i] - prices[i - 1];\n }\n return maxprofit;\n }\n}\n

**Complexity Analysis**

- \n
- \n
Time complexity : . Single pass.

\n \n - \n
Space complexity: . Constant space needed.

\n \n

Analysis written by: @vinod23

\n