8000 Create 122. Best Time to Buy and Sell Stock II · SreeHarsha070/Leetcode-Challenge@8a72523 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a72523

Browse files
authored
Create 122. Best Time to Buy and Sell Stock II
1 parent da6c61b commit 8a72523

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int n = prices.length;
4+
int profit = 0;
5+
6+
//Start with index 1 and compare the price
7+
//with previous day price to calculate profit if any
8+
9+
for(int i = 1; i < n; i++) {
10+
if(prices[i] > prices[i-1])
11+
profit += (prices[i] 4E47 - prices[i-1]);
12+
}
13+
14+
return profit;
15+
}
16+
}

0 commit comments

Comments
 (0)
0