All problems
Stock Profit with Unlimited Transactions
Problem
Given daily prices, return the maximum profit from any number of buy-then-sell transactions, holding at most one share at a time.
Every upward step prices[i] - prices[i-1] can be captured independently, so sum all positive daily differences. Return 0 for empty or decreasing prices.
Hint
Buy before each rise and sell at its top; only the positive daily changes matter.
Test cases
case 1
in: [[7,1,5,3,6,4]]
expected: 7
case 2
in: [[1,2,3,4,5]]
expected: 4
case 3
in: [[7,6,4,3,1]]
expected: 0
case 4
in: [[]]
expected: 0
case 5
in: [[2,2,2]]
expected: 0
Starter codePython
def max_profit_unlimited(prices):
# Your code here
pass5 test cases · run your code against them in the editor