Skip to content

Commit 94fc620

Browse files
committed
Time: 2 ms (86.99%), Space: 75.4 MB (78.90%) - LeetHub
1 parent 91b9c59 commit 94fc620

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int l = 0;
4+
int r = 0;
5+
//l = buy r = sell
6+
int maxprofit = 0;
7+
while( r < prices.length ) {
8+
if(prices[l] < prices[r]){
9+
int profit = prices[r] - prices[l];
10+
maxprofit = Math.max(profit,maxprofit);
11+
}
12+
else{
13+
l = r;
14+
}
15+
r = r + 1;
16+
}
17+
return maxprofit;
18+
}
19+
}

0 commit comments

Comments
 (0)