This question is the same as "Max Chunks to Make Sorted" except the integers of the given array are not necessarily distinct, the input array could be up to length 2000, and the elements could be up to 10**8.


Given an array arr of integers (not necessarily distinct), 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 = [5,4,3,2,1]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.

Example 2:

Input: arr = [2,1,3,4,4]
Output: 4
Explanation:
We can split into two chunks, such as [2, 1], [3, 4, 4].
However, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.

Note:

  • arr will have length in range [1, 2000].
  • arr[i] will be an integer in range [0, 10**8].

Approach #1: Array. [Java]

class Solution {
public int maxChunksToSorted(int[] arr) {
int n = arr.length; int[] maxOfLeft = new int[n];
int[] minOfRight = new int[n]; maxOfLeft[0] = arr[0];
for (int i = 1; i < n; ++i)
maxOfLeft[i] = Math.max(maxOfLeft[i-1], arr[i]); minOfRight[n-1] = arr[n-1];
for (int i = n-2; i >= 0; --i)
minOfRight[i] = Math.min(minOfRight[i+1], arr[i]); int res = 0;
for (int i = 0; i < n-1; ++i)
if (maxOfLeft[i] <= minOfRight[i+1])
res++; return res+1;
}
}

  

Analysis:

Iterate through the array, each time all elements to the left are smaller (or equal) to all elements to the right, there is a new chunck.

Use two arrys to store the left max and right min to achieve O(n) time complexity. Space complexity is O(n) too.

This algorithm can be used to solve verl too.

Reference:

https://leetcode.com/problems/max-chunks-to-make-sorted-ii/discuss/113462/Java-solution-left-max-and-right-min.

768. Max Chunks To Make Sorted II的更多相关文章

  1. [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 就是给你一个字符串,能不 ...

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

  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】768. Max Chunks To Make Sorted II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-chun ...

  5. [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  6. [Swift]LeetCode768. 最多能完成排序的块 II | Max Chunks To Make Sorted II

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  7. Max Chunks To Make Sorted II LT768

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

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

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

随机推荐

  1. SVN的标准目录结构:trunk、branches、tags

    原文链接:http://techlife.blog.51cto.com/212583/223704/ 我们在一些著名开源项目的版本库中,通常可以看到trunk, branches, tags等三个目录 ...

  2. 《Google软件测试之道》摘录

    以下是最近看的一本书<Google软件测试之道>里的一些摘录,收获很多. 1.讨论测试开发比并没有什么意义,如果你是一名开发人员,同时也是一名测试人员,如果你的职位头衔上有测试的字样,你的 ...

  3. 挪过来的spring mvc 的入门 介绍

    目录  一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明 ...

  4. 利用ks构建ISO中的一些坑

    构建ISO的基本流程 1.获取rpm包源码 2.将源码增量编译成二进制包 3.编写ks的包列表决定ISO制作时需要从什么地方(二进制仓库repo)取哪些二进制包 4.通过createiso命令并指定k ...

  5. 更新vs2017 15.9.2后,在指定-T v141_xp情况下载编译会报下面warning MSB8051

    更新vs2017 15.9.2后,在指定-T v141_xp情况下载编译会报下面warning: C:\Program Files (x86)\Microsoft Visual Studio\2017 ...

  6. SharedPreferences的基本使用-----存,删,改,查

    1.创建一个SharedPreferences对象 SharedPreferences spf = context.getSharedPreferences("imageload" ...

  7. mysql的UseAffectedRows问题 以及其他常见配置说明

    遇到MySQL中on duplicate key update语句返回值不正确: 在server5.1.*的返回分别为insert=1,update=3,nochange=2 在server5.5.* ...

  8. 2018.07.28 uoj#169. 【UR #11】元旦老人与数列(线段树)

    传送门 线段树好题. 维护区间加,区间取最大值,维护区间最小值,历史区间最小值. 同样先考虑不用维护历史区间最小值的情况,这个可以参考这道题的解法,维护区间最小和次小值可以解决前两个操作,然后使用历史 ...

  9. 对于java的命名规范(标识符)

    java的命名规范基本都需要使用标识符来命名的: 标识符的介绍: 标识符作用: 给变量起名字的用的. 标识符的组成: 1.Unicode编码:包含大.小写字母,数字,汉字(不建议使用).  2.美元符 ...

  10. node.js+express+mongodb

    主要是想用node.js链接mongodb,用的是mongoose.用ejs引擎,扩展到.html比较容易 小例子结构简单,框架清晰. 提交方法 路径 方法 作用 get add     post a ...