【LeetCode】769. Max Chunks To Make Sorted 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/max-chunks-to-make-sorted/description/
题目描述
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].
题目大意
一个数组,其数组是[0, 1, ..., arr.length - 1]
打乱次序的一种组合。我们要把它进行划分成若干chunks,使得每个chunks进行排序并拼接之后得到的总数组是有序的。求最多多少个chunks.
解题方法
为什么新题总是那么相似?这个题也是只需要遍历一次即可。
思考一个问题,如果想要每个chunk排序拼接之后,得到的总chunk有序,那么说明每个chunk里面数字应该在某个区间内才可以。否则在chunk内进行排序,拼接之后会和其他的chunk的元素顺序不匹配。
因为所有数字是[0, 1, ..., arr.length - 1]
的一个排列,很容易想到,一个区间内的最大的数字,不应该大于这个区间最右的index。
因此,我们从左向右进行遍历,如果已经观测到的最大值小于等于这个区间的index,那么就可以划分区间了。
举例子:
对于:[1,0,2,3,4]
从左到右遍历:
1 目前最大值1,index = 0, 不可划分
0 目前最大值1,index = 1, 可划分
2 目前最大值2,index = 2, 可划分
3 目前最大值3,index = 3, 可划分
4 目前最大值3,index = 4, 可划分
代码如下:
class Solution:
def maxChunksToSorted(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
chunks = 0
pre_max = 0
for i, num in enumerate(arr):
if num > pre_max:
pre_max = num
if pre_max == i:
chunks += 1
return chunks
二刷,使用的方法和上面一样。需要注意的是,第二个if并不是if else,因为我们要时刻保持当前的最大值,当最大值等于当前的索引的时候,把结果+1.
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
const int N = arr.size();
int res = 0;
int preMax = 0;
for (int i = 0; i < N; i++) {
if (arr[i] > preMax)
preMax = arr[i];
if (i == preMax)
res++;
}
return res;
}
};
日期
2018 年 5 月 28 日 —— 太阳真的像日光灯~
2018 年 12 月 18 日 —— 改革开放40周年
【LeetCode】769. Max Chunks To Make Sorted 解题报告(Python & C++)的更多相关文章
- [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 - 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 ...
- 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 944 Delete Columns to Make Sorted 解题报告
题目要求 We are given an array A of N lowercase letter strings, all of the same length. Now, we may choo ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
随机推荐
- LeetCode:旋转图像
题目描述 给定一个 n × n 的二维矩阵 matrix 表示一个图像.请你将图像顺时针旋转 90 度. 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要 使用另一个矩阵来旋转图 ...
- dart系列之:还在为编码解码而烦恼吗?用dart试试
目录 简介 为JSON编码和解码 UTF-8编码和解码 总结 简介 在我们日常使用的数据格式中json应该是最为通用的一个.很多时候,我们需要把一个对象转换成为JSON的格式,也可以说需要把对象编码为 ...
- ZooKeeper 04 - ZooKeeper 集群的节点为什么必须是奇数个
目录 1 - 关于节点个数的说明 2 - ZooKeeper 集群的容错数 3 - ZooKeeper 集群可用的标准 4 - 为什么不能是偶数个节点 4.1 防止由脑裂造成的集群不可用 4.2 奇数 ...
- addict, address, adequate.四级
addict addiction – a biopsychosocial [生物社会心理学的 bio-psycho-social] disorder characterized by persiste ...
- 大数据学习day24-------spark07-----1. sortBy是Transformation算子,为什么会触发Action 2. SparkSQL 3. DataFrame的创建 4. DSL风格API语法 5 两种风格(SQL、DSL)计算workcount案例
1. sortBy是Transformation算子,为什么会触发Action sortBy需要对数据进行全局排序,其需要用到RangePartitioner,而在创建RangePartitioner ...
- Type difference of character literals in C and C++
Every literal (constant) in C/C++ will have a type information associated with it. In both C and C++ ...
- Xcode中匹配的配置包的存放目录
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
- MySQL(5):安装MySQL
下载地址 下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-winx64.zip 安装步骤 第一步:下载得到压缩包,并解压 ...
- 理解JMX之介绍和简单使用
JMX最常见的场景是监控Java程序的基本信息和运行情况,任何Java程序都可以开启JMX,然后使用JConsole或Visual VM进行预览.下图是使用Jconsle通过JMX查看Java程序的运 ...
- 【Linux】【Shell】【Basic】条件测试和变量
bash脚本编程 脚本文件格式: 第一行,顶格:#!/bin/bash 注释信息:# 代码注释: 缩进,适度添加空白行: ...