[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:
arrwill 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 可排序的最大块数的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [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 就是给你一个字符串,能不 ...
- 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 ...
- 【LeetCode】769. Max Chunks To Make Sorted 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 【LeetCode】768. Max Chunks To Make Sorted II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-chun ...
- [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
随机推荐
- 16、Python面向对象进阶
一.对象的继承 Python中支持一个类同时继承多个父类 class Parent1: pass class Parent2: pass class Sub1(Parent1, Parent2): p ...
- 用mysql实现类似于oracle dblink的功能
用mysql实现类似于oracle dblink的功能 首先看看有没有federated 引擎. mysql> show engines; +------------+----------+ ...
- 命令查询windows&Linux系统版本信息
Linux 查询系统名字输入"cat /proc/version",说明正在运行的内核版本uname -rwindows 查询系统名字win+r -> winversyste ...
- How do I fix "selector not recognized" runtime exceptions when trying to use category methods from a static library?
https://developer.apple.com/library/content/qa/qa1490/_index.html A: If you're seeing a "select ...
- IMP self _cmd
The only way to circumvent dynamic binding is to get the address of a method and call it directly as ...
- Laravel 解决blade模板转义html标签问题
当我们使用富文本编译器(如:Ueditor编译器)保存编辑的内容后,在blade模板中,想要显示原生的html标签内容时该怎么做? 首先,了解下laravel {{ 变量名 }} 与{!! 变量名 ! ...
- A~G)C004
AGC004 A Divide a Cuboi 我不会,谁能教教我/kk https://agc004.contest.atcoder.jp/submissions/7919351 B Colorfu ...
- 高考数学答卷策略[K12论坛转载]
一.试卷上给你的启发 1.试卷上有参考公式,80%是有用的,它为你的解题指引了方向: 2.解答题的各小问之间有一种阶梯关系,通常后面的问要使用前问的结论.如果前问是证明,即使不会证明结论,该结论在后问 ...
- smartnic
19年趋势: Intel® 2019网络技术研讨会圆满落幕 SANTOS: Flow and HQoS Acceleration Over DPDK Using Intel Programmable ...
- Fluent——UDF监测指定点的物理量
Fluent版本:19.0 Fluent当中提供了监测某一点物理量随迭代次数或者随时间变化的功能,下面我们就介绍如何在UDF当中实现相同的功能,并且UDF更加灵活,通过UDF的方式我们在知道某点运动规 ...