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:

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:

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:

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:

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:

def maxChunksToSorted(self, arr):
return sum(max(arr[:i + 1]) == i for i in range(len(arr)))

C++:

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

All LeetCode Questions List 题目汇总

[LeetCode] 769. Max Chunks To Make Sorted 可排序的最大块数的更多相关文章

  1. [LeetCode] Max Chunks To Make Sorted 可排序的最大块数

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

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

  3. [LeetCode] 768. Max Chunks To Make Sorted II 可排序的最大块数 II

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  4. [leetcode]Weekly Contest 68 (767. Reorganize String&&769. Max Chunks To Make Sorted&&768. Max Chunks To Make Sorted II)

    766. Toeplitz Matrix 第一题不说,贼麻瓜,好久没以比赛的状态写题,这个题浪费了快40分钟,我真是...... 767. Reorganize String 就是给你一个字符串,能不 ...

  5. LeetCode - 768. Max Chunks To Make Sorted II

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  6. 【LeetCode】769. Max Chunks To Make Sorted 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

  8. 【LeetCode】768. Max Chunks To Make Sorted II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-chun ...

  9. [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

随机推荐

  1. mysql-mysqldump

    备份(导出)所有数据库的数据和结构(注意:这种方式备份,还原时,无需先创建数据库,可直接导入) mysqldump -u root -p 'password' --all-databases > ...

  2. KVM-安装windows

    硬盘.网卡选择vitio 虚拟机配置2个cd-rom,分别挂载系统iso与virtio-win 开始安装无法识别硬盘,加载光驱驱动 安装完成进入系统之后,设备管理器添加驱动,识别设备

  3. Monkey面试整理

    1. 查找进程命令是什么? adb shell ps adb shell ps |findstr 名称 2. 如何获取包名 1)获取手机上的所有包名信息. adb shell pm list pack ...

  4. 浏览器绘图模型的解释:renderObject、renderlayer

    先来看这幅经典的图: https://juejin.im/entry/590801780ce46300617c89b8 renderObject相当于iOS 的view renderlayer完成了一 ...

  5. 「GXOI / GZOI2019」宝牌一大堆 (DP)

    题意 LOJ传送门 题解 可以发现「七对子」 和 「国士无双」直接暴力就行了. 唯一的就是剩下的"3*4+2". 考试的时候写了个爆搜剪枝,开了O2有50pts.写的时候发现可以D ...

  6. CSPS_115

  7. OpenFOAM——冲击斜坡

    本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL045: Oblique Shock Over an Inclined Ramp ...

  8. 记一次phpmyadmin 4.8.1 远程文件包含漏洞(BUUCTF web)

    题目很简单,一个滑稽 打开源码,发现存在source.php文件 于是访问文件,发现出现一串php源码 提示存在hint.php,于是访问发现一句话 flag not here, and flag i ...

  9. 【Alpha】“北航社团帮”小程序v1.0测试报告

    目录 测试计划.过程和结果 后端单元测试 后端压力测试 测试结果 指标解释 前端测试 授权登录与权限检查 功能测试 兼容性测试 性能测试 回答课程组问题 测试中发现的bug 场景测试 测试矩阵 出口条 ...

  10. js数组reduce()方法的使用和一些应用场景

    reduce()的使用 reduce()方法为归并类方法,最常见的应用场景就是,计算数组中每一项的总和. reduce()方法会遍历数组的每一项,它接收两个参数: 第一个参数是:每次遍历都会调用的函数 ...