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].

题目大意:给0~arr.length-1的一个排列,我们将数组拆分成一些“块”(分区),并对每个块进行单独排序。 连接它们之后,结果等于排序后的数组。问最多能够分成多少个分区(块)

分析:因为数组的排序后正确顺序应该是arr[i]处的数是i。所以,遍历数组,每次将最大的那个值标记为maxn,maxn必须在i处才能满足对0~i数字排序后能够恰好是正确的位置,此时ans+1,表示前面的可以组为一个“块”,最后ans即为所求的值~

再解释详细些:maxn是第0~i个数字中的最大值,遍历的过程中如果maxn==i,就保证了前面i-1个数字必然都比maxn小(因为maxn是0~i中的最大值),则第0~i个数字必然能排列成正确顺序,以此类推,找到下一个满足maxn==i的地方(记为j),则i+1~j又能分为一个块…直到遍历到最后一个数为止得到答案~

class Solution {
public:int maxChunksToSorted(vector<int>& arr) {int ans = 0;for (int i = 0, maxn = 0; i < arr.size(); i++) {maxn = max(arr[i], maxn);if (maxn == i) ans++;}return ans;}
};

LeetCode 769. Max Chunks To Make Sorted相关推荐

  1. Leetcode 769. Max Chunks To Make Sorted

    题目 链接:https://leetcode.com/problems/max-chunks-to-make-sorted/ Level: Medium Discription: Given an a ...

  2. leetcode 769. Max Chunks To Make Sorted | 769. 最多能完成排序的块(Java)

    题目 https://leetcode.com/problems/max-chunks-to-make-sorted/ 题解 尽可能切成多份,使得上下的 set 包含相同的值. 用 diff 记录当前 ...

  3. 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 ...

  4. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  5. 【重要+细节】LeetCode 149. Max Points on a Line

    LeetCode 149. Max Points on a Line Solution1: 参考花花酱:https://zxi.mytechroad.com/blog/geometry/leetcod ...

  6. LeetCode 695. Max Area of Island

    LeetCode 695. Max Area of Island Given a non-empty 2D array grid of 0's and 1's, an island is a grou ...

  7. LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  8. 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数

    题目:LeetCode 004 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m an ...

  9. leetcode 695. Max Area of Island | 695. 岛屿的最大面积(DFS)

    题目 https://leetcode.com/problems/max-area-of-island/ 题解 class Solution {int M, N;public int maxAreaO ...

最新文章

  1. 【怎样写代码】确保对象的唯一性 -- 单例模式(二):解决方案
  2. 代码居中对齐_一篇文章带你了解CSS对齐方式
  3. 将会改变未来IT世界的十种编程语言
  4. MySQL 数据库 练习题
  5. win7计算机怎么找管理员,Win7系统Administrator不见了怎么解决?
  6. requirejs(shim)处理加载非AMD规范的js库
  7. java中常用的坑_Java技术开发中的坑
  8. C# Iterators
  9. 《阿里巴巴Java开发手册》2018年完整资料下载!
  10. 盈不足术与老鼠打洞问题的近似解
  11. js手机号码正则验证
  12. winRar禁止弹窗广告方法
  13. 状态转移矩阵 matlab,状态转移矩阵计算.PPT
  14. Redis 增加互斥锁
  15. 硬盘格式化后数据还可以恢复吗?格式化硬盘的恢复方法
  16. UML建模--用例图
  17. Python数据分析基础之CSV文件(5)
  18. info是Linux的帮助工具,Linux下的帮助命令(man/help/info)
  19. 误将D盘格式化该怎么恢复数据?用嗨格式扫描
  20. java poi 填充单元格_POI操作excel表格(建立工作薄、创建工作表、将数据填充到单元格中)...

热门文章

  1. django 引用css失效_如何使用Python中Django模板?
  2. qc是什么职位_质量管理部门该干什么?又该怎么干?
  3. android:configChanges属性
  4. android常用开源库分享
  5. react中使用charles实现本地数据mock
  6. 003《区块链开发指南》一一1.2 区块和区块链 转
  7. mongoDB Error:not master and slaveOk=false
  8. .NET解决[Serializable] Attribute引发的Json序列化k_BackingField
  9. 如何提高网络安全指数
  10. JS 中对象的简单创建和继承