# LeetCode 122. Best Time to Buy and Sell Stock II: C++ Solution

## Problem Statement

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit.

You can complete as many transactions as you want. However, you cam not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

```
Function Signature (C++):
int maxProfit(vector<int>& prices)
Inputs:
prices = [7, 1, 5, 3, 6, 4]
Outputs:
7
We can buy at 1 and then sell at 5 => 6 - 1 = 4.
Then, we can buy at 3 and then sell at 6 => 6 - 3 = 3.
Our total profit, then, is 7 (4 + 3).
```

## TL;DR Code Solution

```
int maxProfit(vector<int>& prices) {
int total = 0;
for (int i = 1; i < prices.size(); i++) {
if (prices[i] > prices[i-1]) {
total += prices[i] - prices[i-1];
}
}
return total;
}
```