[Swift]LeetCode769. 最多能完成排序的块 | Max Chunks To Make Sorted

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-sxqycwvm-me.html 
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into some number of "chunks" (partitions), and individually sort each chunk.  After concatenating them, the result equals the sorted array.git

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

Example 1:数组

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

Example 2:微信

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

Note:ui

  • arr will have length in range [1, 10].
  • arr[i] will be a permutation of [0, 1, ..., arr.length - 1].

数组arr[0, 1, ..., arr.length - 1]的一种排列,咱们将这个数组分割成几个“块”,并将这些块分别进行排序。以后再链接起来,使得链接的结果和按升序排序后的原数组相同。spa

咱们最多能将数组分红多少块?code

示例 1:htm

输入: arr = [4,3,2,1,0]
输出: 1
解释:
将数组分红2块或者更多块,都没法获得所需的结果。
例如,分红 [4, 3], [2, 1, 0] 的结果是 [3, 4, 0, 1, 2],这不是有序的数组。

示例 2:blog

输入: arr = [1,0,2,3,4]
输出: 4
解释:
咱们能够把它分红两块,例如 [1, 0], [2, 3, 4]。
然而,分红 [1, 0], [2], [3], [4] 能够获得最多的块数。

注意:

  • arr 的长度在 [1, 10] 之间。
  • arr[i]是 [0, 1, ..., arr.length - 1]的一种排列。

Runtime: 4 ms
Memory Usage: 19.1 MB
 1 class Solution {
 2     func maxChunksToSorted(_ arr: [Int]) -> Int {
 3         var res:Int = 0
 4         var n:Int = arr.count
 5         var mx:Int = 0
 6         for i in 0..<n
 7         {
 8             mx = max(mx, arr[i])
 9             if mx == i
10             {
11                 res += 1
12             }
13         }
14         return res
15     }
16 }

18268 kb

 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         return res
24     }
25 }

24ms

 1 class Solution {
 2     func maxChunksToSorted(_ arr: [Int]) -> Int {
 3         var result = 0
 4         let count = arr.count
 5         var left = 0
 6         var right = 0
 7         
 8         while left < count {
 9             right = arr[left]
10             while left <= right {
11                 right = max(right, arr[left])
12                 left += 1
13             }
14             result += 1
15         }
16         return result
17     }
18 }
相关文章
相关标签/搜索