作者: 负雪明烛
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:

  1. arr will have length in range [1, 10].
  2. 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++)的更多相关文章

  1. [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 ...

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

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

  8. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

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

  9. 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...

随机推荐

  1. 对Javascript中的对象Object改变内存及其变量改变的图解

    Object 存储变量时,变量属性的内存改变图解 左边: 对象的内存   中间:变量属性的内存   右边:属性值的内存 [图一]创建一个对象,存obj1 变量--里面存age 属性和属性值--12. ...

  2. python-3.x- 序列操作

    1. list操作 A.添加元素 1 list = ["C++","C", "Java", "Python"] 2 &q ...

  3. [php代码审计] Typecho 1.1 -反序列化Cookie数据进行前台Getshell

    环境搭建 源码下载:https://github.com/typecho/typecho/archive/v1.1-15.5.12-beta.zip 下载后部署到web根目录,然后进行安装即可,其中注 ...

  4. GO 通过进程号输出运行运行信息

    操作系统应用可以使用PID来查找关于进程本身的信息.当进程失败时获取到的PID就非常有价值,这样就可以使用PID跟踪整个系统中的系统日志,如/var/log/messages./var/log/sys ...

  5. Can references refer to invalid location in C++?

    在C++中,引用比指针更加的安全,一方面是因为引用咋定义时必须进行初始化,另一方面是引用一旦被初始化就无法使其与其他对象相关联. 但是,在使用引用的地方仍然会有一些例外. (1)Reference t ...

  6. spring注解事务管理

    使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...

  7. 【Linux】【Services】【Cache】使用Sentinel搭建高可用Redis

    1. 简介 1.1. 一些基础概念请参考 http://www.cnblogs.com/demonzk/p/7453494.html 1.2. 几种常用的集群方式. -- Redis Sentinel ...

  8. 【力扣】两个数组的交集 II

    给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2]输出:[2,2]示例 2: 输入:nums1 = [4,9,5], nu ...

  9. 「Python实用秘技02」给Python函数定“闹钟”

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第2期 ...

  10. 🔥🔥🔥Flutter 字节跳动穿山甲广告插件发布 - FlutterAds

    前言 Flutter 已成为目前最流行的跨平台框架之一,在近期的几个大版本的发布中都提到了 Flutter 版本 Google 广告插件 [google_mobile_ads] .对于"出海 ...