[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 ...
随机推荐
- 不重装nginx实现动态添加模块
如果项目在提供服务的过程中,因为需求使然,需要对nginx进行模块的动态添加,可以按照如下流程进行操作 一.查看nginx当前已经安装了那些模块 1) 进入nginx执行文件目录 cd /usr/ ...
- linux定时任务(转)
转自:https://www.cnblogs.com/intval/p/5763929.html linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非 ...
- go mod 使用
go modules 是 golang 1.11 新加的特性.现在1.12 已经发布了,是时候用起来了.Modules官方定义为: 模块是相关Go包的集合.modules是源代码交换和版本控制的单元. ...
- 函数式编程—函数的关系—is-a、has-a、use-a
is-a:函数的实现与函数类型的关系: has-a:匿名(闭包)函数的创建者与匿名函数的关系:匿名函数与环境和上下文(函数)的关系: use-a:高阶函数与参量函数的关系: 函数式编程的基本功之一就是 ...
- iis webapi不间隔第一次访问超慢
第一种尝试(正在验证是否有效): 设置网站对应的应用程序池的"闲置超时"为0.如下图所示
- Passwords Gym - 101174E (AC自动机上DP)
Problem E: Passwords \[ Time Limit: 1 s \quad Memory Limit: 256 MiB \] 题意 给出两个正整数\(A,B\),再给出\(n\)个字符 ...
- Numpy | 14 字符串函数
本章函数用于对 dtype 为 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作. 它们基于 Python 内置库中的标准字符串函数. 这些函数在字符数组类(n ...
- day03 数据基础
1.列举字符串,列表,元组,字典每个常用的五个方法 字符串: strip() , lstrip(),restrip() count(),index(),find() startswith,endswi ...
- 在UE4C++中的宏
1. UE4蓝图的宏 在蓝图中,我们可以把一堆经常使用的节点封装为一个宏,然后通过多次使用这个宏,达到了减少重复代码量的效果. 如图: 2. UE4C++中的宏 那么,在UE4的C++中怎么实现宏呢? ...
- Fluent Meshing分离边界层网格
源视频链接: https://pan.baidu.com/s/1SYB7UdRuXOGYXYwmxKADdw 提取码: h7qj