为有牺牲多壮志,敢教日月换新天。

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10536217.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

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

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:

  • 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]的一种排列,我们将这个数组分割成几个“块”,并将这些块分别进行排序。之后再连接起来,使得连接的结果和按升序排序后的原数组相同。

我们最多能将数组分成多少块?

示例 1:

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

示例 2:

输入: 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 }

 

posted @ 2019-03-15 12:14  为敢技术  阅读(241)  评论(0编辑  收藏  举报