[061] LeetCode 309演算法【Best Time to Buy and Sell Stock with Cooldown】 股票機器人擬人版

M
Leetcode 演算法教學
Oct 23, 2020

309. Best Time to Buy and Sell Stock with Cooldown (Medium)

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 may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

這題跟上一題完全一模一樣,只是把條件變成前一天而已,因為至少暫停一天交易。

大家加油。

上一篇:[060] LeetCode 188演算法【Best Time to Buy and Sell Stock IV】 股票機器人 IV

下一篇:[062] LeetCode 11 演算法【Container With Most Water】盛水最多的容器

--

--