Best Time to Buy and Sell Stock II

Problem Description

Given an array prices where prices[i] is the price on day i, return the maximum profit from buying and selling stocks. You may complete as many transactions as you like (buy one, sell one, repeat), but you must sell before you buy again. Example 1: Input: prices = [7,1,5,3,6,4] Output: 7 (buy day2 sell day3: +4, buy day4 sell day5: +3) Example 2: Input: prices = [1,2,3,4,5] Output: 4 Example 3: Input: prices = [7,6,4,3,1] Output: 0 DE Shaw Tier 3 — LC 122. Also practice LC 123 (at most 2 txns) and LC 188 (at most k txns). Greedy: accumulate all upward price differences. profit += max(0, prices[i] - prices[i-1])

Example Test Cases

Example 1
Input: [[7,1,5,3,6,4]]
Expected: 7
Example 2
Input: [[1,2,3,4,5]]
Expected: 4
Example 3
Input: [[7,6,4,3,1]]
Expected: 0

Run your code to see the results