Skip to content

Commit 870e028

Browse files
best time to buy and sell stock II
1 parent 31f38f3 commit 870e028

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package medium;
2+
3+
public class BestTimeToBuyAndSellStockII {
4+
5+
/**It turns out to be a super simple one, it's really a greedy one! Just keep being greedy if it's possible.*/
6+
public int maxProfit(int[] prices) {
7+
int pro = 0;
8+
for(int i = 0; i < prices.length-1; i++){
9+
if(prices[i+1] > prices[i]) pro += prices[i+1] - prices[i];
10+
}
11+
return pro;
12+
}
13+
14+
}

0 commit comments

Comments
 (0)