[Swift]LeetCode768. 最多能完成排序的塊 II | Max Chunks To Make Sorted II

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

This question is the same as "Max Chunks to Make Sorted" except the integers of the given array are not necessarily distinct, the input array could be up to length 2000, and the elements could be up to 10**8.git

Given an array arr of integers (not necessarily distinct), we split the array into some number of "chunks" (partitions), and individually sort each chunk.  After concatenating them, the result equals the sorted array.github

What is the most number of chunks we could have made?數組

Example 1:微信

Input: arr = [5,4,3,2,1]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.

Example 2:app

Input: arr = [2,1,3,4,4]
Output: 4
Explanation:
We can split into two chunks, such as [2, 1], [3, 4, 4].
However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.

Note:ide

  • arr will have length in range [1, 2000].
  • arr[i] will be an integer in range [0, 10**8].

這個問題和「最多能完成排序的塊」類似,但給定數組中的元素能夠重複,輸入數組最大長度爲2000,其中的元素最大爲10**8ui

arr是一個可能包含重複元素的整數數組,咱們將這個數組分割成幾個「塊」,並將這些塊分別進行排序。以後再鏈接起來,使得鏈接的結果和按升序排序後的原數組相同。spa

咱們最多能將數組分紅多少塊?code

示例 1:

輸入: arr = [5,4,3,2,1]
輸出: 1
解釋:
將數組分紅2塊或者更多塊,都沒法獲得所需的結果。
例如,分紅 [5, 4], [3, 2, 1] 的結果是 [4, 5, 1, 2, 3],這不是有序的數組。 

示例 2:

輸入: arr = [2,1,3,4,4]
輸出: 4
解釋:
咱們能夠把它分紅兩塊,例如 [2, 1], [3, 4, 4]。
然而,分紅 [2, 1], [3], [4], [4] 能夠獲得最多的塊數。 

注意:

  • arr的長度在[1, 2000]之間。
  • arr[i]的大小在[0, 10**8]之間。

Runtime: 56 ms
Memory Usage: 19 MB
 1 class Solution {
 2     func maxChunksToSorted(_ arr: [Int]) -> Int {
 3         var res:Int = 1
 4         var n:Int = arr.count
 5         var curMax:Int = Int.min
 6         var b:[Int] = arr
 7         for i in stride(from:n - 2,through:0,by:-1)
 8         {
 9             b[i] = min(arr[i], b[i + 1])
10         }
11         for i in 0..<(n - 1)
12         {
13             curMax = max(curMax, arr[i])
14             if curMax <= b[i + 1]
15             {
16                 res += 1            
17             }
18         }
19         return res
20     }
21 }

60ms

 1 class Solution {
 2     func maxChunksToSorted(_ arr: [Int]) -> Int {
 3         var befores: [Int] = []
 4         var afters: [Int] = []
 5         var curMax = Int.min
 6         for num in arr {
 7             if num > curMax {
 8                 curMax = num
 9             }
10             befores.append(curMax)
11         }
12         var curMin = Int.max
13         for num in arr.reversed() {
14             if num < curMin {
15                 curMin = num
16             }
17             afters.append(curMin)
18         }
19         afters.reverse()
20         var res = 0
21         for i in 0..<arr.count-1 {
22             if befores[i] <= afters[i+1] {
23                 res += 1
24             }
25         }
26         return res + 1
27     }
28 }

64ms

 1 class Solution {
 2     func maxChunksToSorted(_ arr: [Int]) -> Int {
 3         guard arr.isEmpty == false else {
 4             return 0
 5         }
 6         let sortedArr = arr.sorted()
 7         let n = arr.count
 8         var res = 0
 9         var sum1 = 0
10         var sum2 = 0
11         for i in 0..<n {
12             sum1 += arr[i]
13             sum2 += sortedArr[i]
14             if sum1 == sum2 {
15                 res += 1
16                 sum1 = 0
17                 sum2 = 0
18             }
19         }
20         return res
21     }
22 }

76ms

 1 class Solution {
 2     func maxChunksToSorted(_ arr: [Int]) -> Int {
 3         let n = arr.count
 4         var minOfRight = Array(repeating: 0, count: n)
 5         var maxOfLeft = Array(repeating: 0, count: n)
 6         
 7         maxOfLeft[0] = arr[0]
 8         minOfRight[n-1] = arr[n-1]
 9         
10         for i in 1..<n {
11             maxOfLeft[i] = max(maxOfLeft[i-1], arr[i])
12         }
13         
14         for i in (0..<(n - 1)).reversed() {
15             minOfRight[i] = min(minOfRight[i+1], arr[i])
16         }
17         
18         var res = 1
19         
20         for i in 1..<n {
21             res += maxOfLeft[i-1] <= minOfRight[i] ? 1 : 0
22         }
23         
24         return res
25     }
26 }

Runtime: 80 ms
Memory Usage: 19.3 MB
 1 class Solution {
 2     func maxChunksToSorted(_ arr: [Int]) -> Int {
 3         var nums = [Int : Int]()
 4         for item in arr {
 5             if let num = nums[item] {
 6                 nums[item] = num + 1
 7             } else {
 8                 nums[item] = 1
 9             }
10         }
11         
12         var sum = 0
13         let sortNums = nums.sorted { (dic1, dic2) -> Bool in
14             return dic1.key < dic2.key
15         }
16         for item in sortNums {
17             nums[item.key] = sum
18             sum += item.value
19         }
20         
21         let count = arr.count
22         var left = 0
23         var right = nums[arr[left]]!
24         
25         var preIndex = 0
26         var result = 0
27 
28         while left < count {
29             if arr[left] > arr[preIndex] {
30                 if let num = nums[arr[left]] {
31                     preIndex = left
32                     right = num
33                 }
34             } else if arr[left] == arr[preIndex] {
35                 left += 1
36             }
37             
38             while left <= right {
39                 if arr[left] == arr[preIndex] && left != preIndex && right < count - 1 {
40                     right += 1
41                 } else if arr[left] > arr[preIndex] {
42                     if let num = nums[arr[left]] {
43                         preIndex = left
44                         right = num
45                     }
46                 }
47                 left += 1
48             }
49             result += 1
50         }
51         return result
52     }
53 }

100ms

 1 class Solution {
 2     func maxChunksToSorted(_ arr: [Int]) -> Int {
 3         var map = [Int: Int]()
 4         var sorted = arr.sorted()
 5         var nonZeroCount = 0
 6         var result = 0
 7         for i in 0..<sorted.count {
 8             let x = arr[i]
 9             let y = sorted[i]
10             map[x] = (map[x] ?? 0) + 1
11             if map[x] == 0 {
12                 nonZeroCount -= 1
13             } else if map[x] == 1 {
14                 nonZeroCount += 1
15             }
16             map[y] = (map[y] ?? 0) - 1
17             if map[y] == 0 {
18                 nonZeroCount -= 1
19             } else if map[y] == -1 {
20                 nonZeroCount += 1
21             }
22             if nonZeroCount == 0 {
23                 result += 1
24             }
25         }
26         return result
27     }
28 }
相關文章
相關標籤/搜索