-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBestTimetoBuyandSellStock.java
More file actions
28 lines (23 loc) · 970 Bytes
/
BestTimetoBuyandSellStock.java
File metadata and controls
28 lines (23 loc) · 970 Bytes
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
/*
Runtime: 2 ms, faster than 93.61% of Java online submissions for Best Time to Buy and Sell Stock.
Memory Usage: 84 MB, less than 34.66% of Java online submissions for Best Time to Buy and Sell Stock.
*/
// TC: O(n) SC: O(1)
class Solution {
public int maxProfit(int[] prices) {
// Declaring And Initialising Variables To Store Result And Minimum Value Of Prices
int maxProfit = 0;
int minimumValue = Integer.MAX_VALUE;
// Loop To Store Minimum Price In currentValue & Calculating Profit With Every Element Greater Than currentValue
for(int price : prices){
if(price < minimumValue){
minimumValue = price;
}else{
// Calculating The Max Profit
maxProfit = Math.max(maxProfit, (price - minimumValue));
}
}
//Returning The Final Result
return maxProfit;
}
}