[LeetCode] 769. Max Chunks To Make Sorted 可排序的最大块数
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]
.
给一个长度为n的数组,里面的数字是[0, n-1]范围内的所有数字的枚举中的一中。将其分成若干块儿,要求分别给每一小块儿排序,再组合到一起,等于原数组的有序排列,问最多能分多少块。
跟45. Jump Game II那题很像,我们需要维护一个最远能到达的位置,这里的每个数字相当于那道题中的跳力,只有当我们刚好到达最远点的时候,就可以把之前断成一个新的块儿了。
解法:
The basic idea is to use max[] array to keep track of the max value until the current position, and compare it to the sorted array (indexes from 0 to arr.length - 1). If the max[i] equals the element at index i in the sorted array, then the final count++.
e.g:
original: 0, 2, 1, 4, 3, 5, 7, 6
max: 0, 2, 2, 4, 4, 5, 7, 7
sorted: 0, 1, 2, 3, 4, 5, 6, 7
index: 0, 1, 2, 3, 4, 5, 6, 7
The chunks are: 0 | 2, 1 | 4, 3 | 5 | 7, 6
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public int maxChunksToSorted( int [] arr) { if (arr == null || arr.length == 0 ) return 0 ; int [] max = new int [arr.length]; max[ 0 ] = arr[ 0 ]; for ( int i = 1 ; i < arr.length; i++) { max[i] = Math.max(max[i - 1 ], arr[i]); } int count = 0 ; for ( int i = 0 ; i < arr.length; i++) { if (max[i] == i) { count++; } } return count; } |
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public int maxChunksToSorted( int [] arr) { if (arr == null || arr.length == 0 ) return 0 ; int count = 0 , max = 0 ; for ( int i = 0 ; i < arr.length; i++) { max = Math.max(max, arr[i]); if (max == i) { count++; } } return count; } |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Solution( object ): def maxChunksToSorted( self , arr): """ :type arr: List[int] :rtype: int """ expectSum = 0 cnt = 0 realSum = 0 for i in range ( len (arr)): if arr[i] + realSum = = expectSum: cnt + = 1 realSum = 0 if i + 1 < len (arr): expectSum = i + 1 else : realSum + = arr[i] if i + 1 < len (arr): expectSum + = i + 1 return cnt |
Python:
1 2 3 4 5 6 7 8 | def maxChunksToSorted( self , arr): curMax = - 1 res = 0 for i, v in enumerate (arr): curMax = max (curMax, v) if curMax = = i: res + = 1 return res |
Python:
1 2 | def maxChunksToSorted( self , arr): return sum ( max (arr[:i + 1 ]) = = i for i in range ( len (arr))) |
C++:
1 2 3 4 5 6 7 8 9 10 11 | class Solution { public : int maxChunksToSorted(vector< int >& arr) { int res = 0, n = arr.size(), mx = 0; for ( int i = 0; i < n; ++i) { mx = max(mx, arr[i]); if (mx == i) ++res; } return res; } }; |
类似题目:
[LeetCode] 768. Max Chunks To Make Sorted II 可排序的最大块数 II
[LeetCode] 45. Jump Game II 跳跃游戏 II
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2018-03-05 [LeetCode] 139. Word Break 单词拆分
2018-03-05 [LeetCode] 140. Word Break II 单词拆分II
2018-03-05 [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化
2018-03-05 [LeetCode] 206. Reverse Linked List 反向链表
2018-03-05 [LeetCode] 92. Reverse Linked List II 反向链表II
2018-03-05 [LeetCode] 258. Add Digits 加数字