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

Intuition and Algorithm

Let’s try to find the smallest left-most chunk. If the first k elements are [0, 1, …, k-1], then it can be broken into a chunk, and we have a smaller instance of the same problem.

We can check whether k+1 elements chosen from [0, 1, …, n-1] are [0, 1, …, k] by checking whether the maximum of that choice is k.

class Solution {public int maxChunksToSorted(int[] arr) {int ans = 0, max = 0;for (int i = 0; i < arr.length; ++i) {max = Math.max(max, arr[i]);if (max == i)ans++;}return ans;}
}

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

    Given an array arr that is a permutation of [0, 1, -, arr.length – 1], we split the array into some ...

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

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

  4. LeetCode github集合,附CMU大神整理笔记

    Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...

  5. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  6. Leetcode 部分题解

    算法思想 双指针 排序 快速选择 堆排序 桶排序 荷兰国旗问题 贪心思想 二分查找 分治 搜索 BFS DFS Backtracking 动态规划 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  8. LeetCode 力扣算法题解汇总,All in One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...

  9. Python内置函数max()高级用法

    不管是排序还是选取最大值或者最小值,都应该有个规则或者顺序,而平时我们所说的最大值或最小值实际上也是在某种排序规则或顺序下的最大值和最小值.Python内置函数max().min()和sorted() ...

最新文章

  1. Eclipse无法识别(手机)设备的解决方案
  2. python自动补全库_叼炸天的库! 自动补全 Python 代码,能节省 60% 敲码时间
  3. mybatis中的xml中拼接sql中参数与字符串的方法
  4. ASA对FTP的审查抓包测试
  5. 扫地机器人的特点描写_描写扫地机器人五年级作文500字
  6. css布局笔记(二)Flex
  7. Notepad++在线安装使用JSON插件
  8. 中科曙光服务器配置与虚拟系统安装等优化
  9. 网络篇-NSURLSession介绍
  10. QCC3005 Line_IN 优先级
  11. ASP.Net网站管理工具配置
  12. 学生信息管理系统python面向对象_Python学员管理系统【面向对象实现】
  13. 1357: 逆序数字
  14. android雪花飘落动画,Android自定义View——从零开始实现雪花飘落效果
  15. PPM,PGM图片格式
  16. ensp MSTP实验搭建
  17. 人人视频显示服务器睡着了,人人视频显示连接超时
  18. 互联网行业职位介绍 —— OD、PM、RD、FE、UE、QA、OP、DBA...
  19. svn访问报错500
  20. 用Python处理Excel的14个常用操作

热门文章

  1. Matlab学习日记(1)简单介绍与help界面
  2. 以可靠性为中心的维修(RCM)
  3. 总结的一些微信API接口
  4. Java程序设计吃显卡吗_3D建模和渲染吃CPU还是显卡?专业显卡和游戏显卡的区别...
  5. ceph 运维操作-RADOS
  6. 好教程推荐系列:《程序员的自我修养》和《程序员修炼之道:通向务实的最高境界(第2版)》
  7. ViewPager2+Fragment
  8. Flink:Sink、自定义Sink
  9. 爬虫学习4-HTML和XML数据的分析与解析
  10. 第十二章 结合flume+mapreduce+hive+sqoop+mysql的综合实战练习