[Swift]LeetCode918. 環形子數組的最大和 | Maximum Sum Circular Subarray

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-pwxthhpl-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C.git

Here, a circular array means the end of the array connects to the beginning of the array.  (Formally, C[i] = A[i]when 0 <= i < A.length, and C[i+A.length] = C[i] when i >= 0.)github

Also, a subarray may only include each element of the fixed buffer A at most once.  (Formally, for a subarray C[i], C[i+1], ..., C[j], there does not exist i <= k1, k2 <= j with k1 % A.length = k2 % A.length.)數組

 Example 1:微信

Input: [1,-2,3,-2]
Output: 3 Explanation: Subarray [3] has maximum sum 3 

Example 2:app

Input: [5,-3,5]
Output: 10 Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10 

Example 3:spa

Input: [3,-1,2,-1]
Output: 4 Explanation: Subarray [2,-1,3] has maximum sum 2 + (-1) + 3 = 4 

Example 4:code

Input: [3,-2,2,-3]
Output: 3 Explanation: Subarray [3] and [3,-2,2] both have maximum sum 3 

Example 5:orm

Input: [-2,-3,-1]
Output: -1 Explanation: Subarray [-1] has maximum sum -1 

 Note:htm

  1. -30000 <= A[i] <= 30000
  2. 1 <= A.length <= 30000

給定一個由整數數組 A 表示的環形數組 C,求 C 的非空子數組的最大可能和。

在此處,環形數組意味着數組的末端將會與開頭相連呈環狀。(形式上,當0 <= i < A.length 時 C[i] = A[i],而當 i >= 0 時 C[i+A.length] = C[i]

此外,子數組最多隻能包含固定緩衝區 A 中的每一個元素一次。(形式上,對於子數組 C[i], C[i+1], ..., C[j],不存在 i <= k1, k2 <= j 其中 k1 % A.length = k2 % A.length

示例 1:

輸入:[1,-2,3,-2]
輸出:3
解釋:從子數組 [3] 獲得最大和 3

示例 2:

輸入:[5,-3,5]
輸出:10
解釋:從子數組 [5,5] 獲得最大和 5 + 5 = 10

示例 3:

輸入:[3,-1,2,-1]
輸出:4
解釋:從子數組 [2,-1,3] 獲得最大和 2 + (-1) + 3 = 4

示例 4:

輸入:[3,-2,2,-3]
輸出:3
解釋:從子數組 [3] 和 [3,-2,2] 均可以獲得最大和 3

示例 5:

輸入:[-2,-3,-1]
輸出:-1
解釋:從子數組 [-1] 獲得最大和 -1

提示:

  1. -30000 <= A[i] <= 30000
  2. 1 <= A.length <= 30000

80 ms

 1 class Solution {
 2     func maxSubarraySumCircular(_ A: [Int]) -> Int {
 3         if A == nil || A.count == 0 {return 0}
 4         var preSumMin:Int = 0
 5         var preSumMax:Int = 0
 6         var preSum = 0
 7         var sumMin = Int.max
 8         var sumMax = Int.min
 9         let count = A.count
10         for i in 0..<count
11         {
12             preSum += A[i]
13             sumMax = max(preSum - preSumMin, sumMax)
14             if i != (count - 1)
15             {
16                 sumMin = min(preSum - preSumMax, sumMin)
17             }
18             preSumMin = min(preSumMin, preSum)
19             preSumMax = max(preSumMax, preSum)
20         }
21         return max(sumMax, preSum - sumMin)
22     }
23 }

300ms

 1 class Solution {
 2     func maxSubarraySumCircular(_ A: [Int]) -> Int {
 3         
 4         var maxSum = A.max()!
 5         
 6         var simpleA: [Int] = []
 7         simpleA.reserveCapacity(A.count)
 8         var isPos = A.first! > 0
 9         var sum = 0
10         for i in 0..<A.count {
11             if A[i] > 0 || A[i] < 0{
12                 if isPos == (A[i] > 0) {
13                     sum += A[i]
14                 } else {
15                     simpleA.append(sum)
16                     sum = A[i]
17                     isPos = A[i] > 0
18                 }
19             }
20         }
21         simpleA.append(sum)
22         
23         let AA = simpleA + simpleA
24         
25         iCycle: for i in 0..<simpleA.count {
26             if simpleA[i] < 0 {
27                 continue iCycle
28             }
29             var sum = simpleA[i]
30             maxSum = max(sum, maxSum)
31             jCycle: for j in (i+1)..<(i+simpleA.count) {
32                 sum += AA[j]
33                 if sum < 0 {
34                     continue iCycle
35                 }
36                 maxSum = max(sum, maxSum)
37             }
38         }
39         return maxSum
40     }
41 }

704ms

 1 class Solution {
 2     func maxSubarraySumCircular(_ A: [Int]) -> Int {
 3         if A == nil || A.count == 0 {return 0}
 4         var preSumMin:Int = 0
 5         var preSumMax:Int = 0
 6         var preSum = 0
 7         var sumMin = Int.max
 8         var sumMax = Int.min
 9         let len = A.count
10         for i in 0..<len
11         {
12             preSum += A[i]
13             sumMax = max(preSum - preSumMin, sumMax)
14             if i != (len - 1)
15             {
16                 sumMin = min(preSum - preSumMax, sumMin)
17             }
18             preSumMin = min(preSumMin, preSum)
19             preSumMax = max(preSumMax, preSum)
20         }
21         return max(sumMax, preSum - sumMin)
22     }
23 }

836ms

 1 class Solution {
 2     func maxSubarraySumCircular(_ A: [Int]) -> Int {
 3         guard A.count > 0 else {
 4             return 0
 5         }
 6         let s = A.reduce(0, +)
 7         var M = A[0]
 8         var m = A[0]
 9         var lastM = A[0]
10         var lastm = A[0]
11         for i in 1 ..< A.count {
12             let a = A[i]
13             lastM = max(lastM+a, a)
14             lastm = min(lastm+a, a)
15             M = max(M, lastM)
16             m = min(m, lastm)
17         }
18         return M > 0 ? max(M, s-m) : M
19     }
20 }

948ms

 1 class Solution {
 2     func maxSubarraySumCircular(_ A: [Int]) -> Int {
 3         var sum = 0
 4         var curMax = 0
 5         var curMin = 0
 6         var maxSum = -30000
 7         var minSum = 30000
 8         for i in 0..<A.count {
 9             sum += A[i]
10             curMax = max(curMax + A[i], A[i])
11             maxSum = max(maxSum, curMax)
12             curMin = min(curMin + A[i], A[i])
13             minSum = min(minSum, curMin)
14         }
15         return maxSum > 0 ? max(sum - minSum, maxSum) : maxSum
16     }
17 }
相關文章
相關標籤/搜索