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. 项目中有私有仓库模块时,使用 npm ci 命令的安装步骤

    项目中有私有仓库模块时,使用 npm ci 命令的安装步骤: 先安装私有仓库模块:npm install <npm包名> --registry=<npm包源> 再运行命令:np ...

  2. 命令查询windows&Linux系统版本信息

    Linux 查询系统名字输入"cat /proc/version",说明正在运行的内核版本uname -rwindows 查询系统名字win+r -> winversyste ...

  3. Makefile 与tab

    Makefile文件由一系列规则(rules)构成.每条规则的形式如下. <target> : <prerequisites> [tab] <commands> 上 ...

  4. 2019湖南省赛H题——概率转移&&逆矩阵

    题意 题目链接 Bobo有一个 $n+m$ 个节点的有向图,编号分别为 $1 \sim n$,他还有一个 $n$ 行 $n+m$ 列的矩阵 $P$. 如果在 $t$ 时刻他位于节点 $u(1 \leq ...

  5. 2.深入学习Servlet的ServletContext对象

    一.建立项目servlet01 在入门Servlet项目中建立一个子项目模块(此处不再赘述如何建立),补全maven项目中的java和resources文件夹,添加类HelloServlet.java ...

  6. github Actions 使用方法

    http://www.ruanyifeng.com/blog/2019/09/getting-started-with-github-actions.html Actions 是github提供的持续 ...

  7. 腾讯蓝鲸cmdb部署

    蓝鲸配置平台 (CMDB)http://172.16.6.10:8088 环境(单机测试): Centos6 16G 200G 依赖环境: Java 1.8.0_92 python 2.7 ZooKe ...

  8. 用于C#的极速序列化/反序列工具 MessagePack

    MessagePack 比MsgPack-Cli快10倍,并且优于其他C#序列化器.MessagePack for C#还内置了对LZ4压缩的支持 - 一种极快的压缩算法.对于性能追求很重要,特别是在 ...

  9. 洛谷 P1873 【砍树】

    P1873 传送门 题外话 话说我们也要当一当光头强?? 大体题意 就是让你砍树,统一的高度,然后让你砍树,看看订什么高度合适. 思路: 二分答案,对高度二分,如果砍得树长度不够,那就说明高度高了. ...

  10. Codeforces 828F Best Edge Weight - 随机堆 - 树差分 - Kruskal - 倍增算法

    You are given a connected weighted graph with n vertices and m edges. The graph doesn't contain loop ...