LeetCode Problem: Best Time to Buy and Sell Stock II
Problem statement:
You are given an array prices where prices[i] is the price of a given stock on the ith day.
On any day, you can buy or sell a stock. You can hold max one share at a time.
Return the maximum profit you can achieve.
Example:
Input:
[7,1,5,3,6,4]
Expected output:
7
If you would like to solve the problem on Leetcode, here is the link to the problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii
Golang Solution:
func maxProfit(prices []int) int {
profit := 0
for i := 1; i < len(prices); i++ {
if prices[i] > prices[i-1] {
profit += prices[i] - prices[i-1]
}
}
return profit
}