---
title: 122. Best Time to Buy and Sell Stock II
tags: Greedy
description: share source code.
---
# 122. Best Time to Buy and Sell Stock II
```java=
class Solution {
public int maxProfit(int[] prices) {
int buy = -prices[0];
int sell = 0;
int n = prices.length;
for(int i = 1; i < n; i++){
buy = Math.max(sell - prices[i], buy);
sell = Math.max(buy + prices[i], sell);
}
return Math.max(0, sell);
}
}
```