作者: 负雪明烛
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. Oracle基础入门

    说明:钓鱼君昨天在网上找到一份oracle项目实战的文档,粗略看了一下大致内容,感觉自己很多知识不够扎实,便跟着文档敲了一遍,目前除了机械性代码没有实现外,主要涉及知识:创建表空间.创建用户.给用户赋 ...

  2. 【模板】一般图最大匹配(带花树算法)/洛谷P6113

    题目链接 https://www.luogu.com.cn/problem/P6113 题目大意 给定一个 \(n\) 个点 \(m\) 条边的无向图,求该图的最大匹配. 题目解析 二分图最大匹配,一 ...

  3. 年底巩固下 CS 知识「GitHub 热点速览 v.21.49」

    作者:HelloGitHub-小鱼干 期末到了!是时候来一波 CS 复习资料了,从本科基础知识开始到实用编程技术.本周 GitHub 热点趋势榜给你提供了最全的复习资料:清华的 CS 四年学习资料.W ...

  4. 日常Java 2021/10/25

    ArrayList存储数字 import java.util.ArrayList; public class Arr_test { public static void main(String[] a ...

  5. Hadoop的HA机制浅析

    Zookeeper在Hadoop的HA中的应用 非HA的弊端: HDFS集群的分布式存储是靠namenode节点(namenode负责响应客户端请求)来实现.在非HA集群中一旦namenode宕机,虽 ...

  6. Mapreduce中的join操作

    一.背景 MapReduce提供了表连接操作其中包括Map端join.Reduce端join还有半连接,现在我们要讨论的是Map端join,Map端join是指数据到达map处理函数之前进行合并的,效 ...

  7. Oracle之DBMS_LOCK包用法详解

    概述与背景 某些并发程序,在高并发的情况下,必须控制好并发请求的运行时间和次序,来保证处理数据的正确性和完整性.对于并发请求的并发控制,EBS系统可以通过Concurrent Program定义界面的 ...

  8. Linux:-e、-d、-f、-L、-r、-w、-x、-s、-h、

    -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L fil ...

  9. django搭建示例-ubantu环境

    python3安装--------------------------------------------------------------------------- 最新的django依赖pyth ...

  10. Oracle 表结构管理

    表其实是数据的'容器'.oracle有几种类型的表: 普通表(ordinary table)又叫堆组织表. 聚簇表(clustered table) 分区表(partition table) 外部表( ...