大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode309. 最佳买卖股票时机含冷冻期 | Best Time to Buy and Sell Stock with Cooldown,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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:
Example:
Input: [1,2,3,2] Output: 3 Explanation: transactions = [buy,sell,cooldown,buy,sell]
给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。?
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
示例:
输入: [1,2] 输出: 3 解释: 对应的交易状态为: [买入,卖出,冷冻期,买入,卖出]
16ms
1 class Solution { 2 func maxProfit(_ prices: [Int]) -> Int { 3 if prices.count <= 1 { 4 return 0 5 } 6 var s0: Int = 0 7 var s1: Int = -prices[0] 8 var s2: Int = Int.min 9 for i in 1 ..< prices.count { 10 let pre0 = s0 11 let pre1 = s1 12 let pre2 = s2 13 s0 = max(pre0,pre2) 14 s1 = max(pre0 - prices[i],pre1) 15 s2 = pre1 + prices[i] 16 } 17 return max(s0,s2) 18 } 19 }
28ms
以上是大佬教程为你收集整理的[Swift]LeetCode309. 最佳买卖股票时机含冷冻期 | Best Time to Buy and Sell Stock with Cooldown全部内容,希望文章能够帮你解决[Swift]LeetCode309. 最佳买卖股票时机含冷冻期 | Best Time to Buy and Sell Stock with Cooldown所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。