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. IIS文件名解析漏洞扼要分析

    概括: 从技术角度分析IIS6文件名解析漏洞的原理与IIS7的相关情况. a.IIS6错误解析文件类型现象 1.当WEB目录下,文件名以 xxx.asp;xxx.xxx 来进行命名的时候,此文件将送交 ...

  2. 765A Neverending competitions

    A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...

  3. hdu-1061(快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061 思路:快速幂 #include<iostream> #include<cstd ...

  4. Spinner功能和用法

    书中只是简单写了选择的界面,没有写出选择之后的结果显示,我做了进一步功能. MainActivity.java public class MainActivity extends Activity { ...

  5. 关于on commit preserve rows与on commit delete rows的测试

    1:先分别建立两张表 sql> CREATE GLOBAL TEMPORARY TABLE tmp_session on commit preserve rows as select * fro ...

  6. (转)C#静态方法使用经验浅谈

    转自:http://developer.51cto.com/art/200908/147734.htm C#静态方法有什么弊端? 我们在实际的开发过程中会注意到C#静态方法对于我们程序的影响,那么有哪 ...

  7. /usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory

    https://stackoverflow.com/questions/39111930/usr-include-boost-python-detail-wrap-python-hpp5023-fat ...

  8. 20155209 2016-2017-2 《Java程序设计》第八周学习总结

    20155209 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从J ...

  9. java基础-day3

    第03天 java基础知识 今日内容介绍 u 选择流程控制语句 u 循环流程控制语句 u 控制循环语句 第1章   选择流程控制语句 1.1  顺序结构的基本使用 1.1.1 顺序结构概述 是程序中最 ...

  10. RepositionBars的用法和参数的意义(引用别人的)

    MFC窗口位置管理详细分析及实例 在一般用MFC编写的程序的窗口客户区中,可能有好几个子窗口(具有WM_CHILD风格的窗口).上边是工具栏,中间是视图窗口,下边是状态栏.三个窗 口在框架的客户区里和 ...