My LeetCode grinding. Trying to do a problem a day.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

problem.txt 1.0KB

1234567891011121314151617181920212223242526
  1. Say you have an array for which the ith element is the price of a given stock on day i.
  2. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
  3. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
  4. Example 1:
  5. Input: [7,1,5,3,6,4]
  6. Output: 7
  7. Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
  8. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
  9. Example 2:
  10. Input: [1,2,3,4,5]
  11. Output: 4
  12. Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
  13. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
  14. engaging multiple transactions at the same time. You must sell before buying again.
  15. Example 3:
  16. Input: [7,6,4,3,1]
  17. Output: 0
  18. Explanation: In this case, no transaction is done, i.e. max profit = 0.