-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL122.java
More file actions
36 lines (36 loc) · 1.62 KB
/
L122.java
File metadata and controls
36 lines (36 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution122 {
class Solution {
/**
* 122. Best Time to Buy and Sell Stock II https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
*
* @param prices
* @return Max profit obtainable
* @timeComplexity O(n)
* @sapceComplexity O(1)
*/
public int maxProfit(int[] prices) {
int totalProfit = 0;
int minInPart = Integer.MAX_VALUE;
int maxInPart = Integer.MIN_VALUE;
// We find all the partitions of the array such that first number is least and last number is max in the partition
// A transaction is buying in first day of partition and selling on second day. All such partitions may not be
// constitute full array
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minInPart) {
minInPart = prices[i];
// Reset the max in partition because maxInPartition must come after min in partition
maxInPart = Integer.MIN_VALUE;
}
maxInPart = Math.max(maxInPart, prices[i]);
// Condition for a transaction to execute. If it is end of array, execute anyway else execute
// if this is partition boundary
if (minInPart < maxInPart && (i == prices.length - 1 || prices[i] > prices[i + 1])) {
totalProfit += maxInPart - minInPart;
minInPart = Integer.MAX_VALUE;
maxInPart = Integer.MIN_VALUE;
}
}
return totalProfit;
}
}
}