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

Similar to previous discussion, we can use minRight to record the min value from the right, as long as left elements are smaller or equal than the right elements, there could be a new chunk.

Time complexity: O(n)

Space complexity: O(n)

public class SolutionLT768 {
public int maxChunksToSorted(int[] arr) {
if(arr == null) return 1; int sz = arr.length;
int[] rightMin = new int[sz];
rightMin[sz-1] = arr[sz-1]; for(int i = sz-2; i >= 0; --i) {
rightMin[i] = Math.min(rightMin[i+1], arr[i]);
} int currMax = arr[0];
int count = 1;
for(int i = 0; i < sz -1; ++i) {
currMax = Math.max(currMax, arr[i]);
if(currMax <= rightMin[i+1]) {
++count;
}
}
return count;
} public static void main(String[] args) {
SolutionLT768 subject = new SolutionLT768();
int[] testData1 = {5, 4, 3, 2, 1};
int[] testData2 = {2, 1, 3, 4, 4}; System.out.println(subject.maxChunksToSorted(testData1));
System.out.println(subject.maxChunksToSorted(testData2)); }
}

Using stack to record the max element of each chunk, if the next element is smaller than the max element on the stack, need to merge the chunk by poping out max elements on previous chunks; else if the next element is bigger, push it to the stack. Hence, the stack keeps the max element of the new chunk or merged chunk.

Time complexity: O(n)

Space complexity: O(n)

public class SolutionLT768 {
public int maxChunksToSorted(int[] arr) {
Deque<Integer> maxStack = new LinkedList<>(); for(int num: arr) {
int currMax = maxStack.isEmpty()? num: Math.max(num, maxStack.peek());
while(!maxStack.isEmpty() && maxStack.peek() > num) {
maxStack.pop();
}
maxStack.push(currMax);
} return maxStack.size();
}
}

Previously, based on the observation arr[i] = i, this won't work for duplicates, to fix the duplicates, we can use stable sort to get the index and then use the index array as the problem 764.

[2, 1, 4, 4, 3, 5, 7, 6] -> [1, 2, 3, 4, 4, 5, 6, 7] -> [1, 0, 4, 2, 3, 5, 6, 7]

[0, 1, 2, 3, 4, 5, 6, 7]

Time complexity: O(nlgn)

Space complexity: O(n)

class Solution {
public int maxChunksToSorted(int[] arr) {
List<Integer> stableSortedIndexes = new ArrayList<>();
for(int i = 0; i < arr.length; ++i) {
stableSortedIndexes.add(i);
} Comparator<Integer> comparator = (o1, o2) -> {
return arr[o1] - arr[o2];
};
Collections.sort(stableSortedIndexes, comparator); int currMax = stableSortedIndexes.get(0);
int count = 0;
for(int i = 0; i < stableSortedIndexes.size(); ++i) {
currMax = Math.max(currMax, stableSortedIndexes.get(i));
if(currMax == i) {
++count;
}
} return count;
}
}

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

  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] Max Chunks To Make Sorted II 可排序的最大块数之二

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

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

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

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

  7. 【LeetCode】768. Max Chunks To Make Sorted II 解题报告(Python)

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

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

  9. 最多的划分来使数组有序 Max Chunks To Make Sorted

    2018-12-01 11:05:46 一.Max Chunks To Make Sorted 问题描述: 问题求解: 由于没有重复,所以直观的来看对于每个遇到数,其能够被划分出来的前提是其前面已经有 ...

随机推荐

  1. Avatar

    [Avatar] 1.Retargeting of Humanoid animations Retargeting is only possible for humanoid models, wher ...

  2. 循环流程控制&方法(3)

    1.循环流程控制 当某一段代码需要重复执行多次的时候,就需要用到循环: 循环三要素: 循环的起点:循环的终点(结束条件):步长: 当循环条件不再成立,结束循环: for循环 for(循环起点:循环条件 ...

  3. metasploit framework(六):信息收集

    nmap 扫描 扫描完毕之后,hosts查看扫描的结果 auxiliary 扫描 使用arpsweep模块扫描 查看设置 设置网卡和目标IP 设置伪造的源IP和源MAC set SHOST <伪 ...

  4. 找回密码的url分析

    https://www.example.com/reset?email=user@example.com&key=b4c9a289323b21a01c3e940f150eb9b8c542587 ...

  5. Mysql 表 创建 / 删除(基础3)

    创建表 语法:  #进入数据库 mysql> use mydb123; Database changedmysql> select database();+------------+| d ...

  6. fiddler对浏览器、app抓包及证书安装(转)

    http://blog.csdn.net/u011608531/article/details/50838227 1.fiddler对浏览器抓包 1.1 对浏览器的http的抓包 Capturing开 ...

  7. JMeter学习(九)FTP测试计划(转载)

    转载自 http://www.cnblogs.com/yangxia-test FTP服务主要提供上传和下载功能.有时间需要我们测试服务器上传和下载的性能.在这里我通过JMeter做一个FTP测试计划 ...

  8. C# sqlserver ExecuteNonQuery()方法详解

    关于ExecuteNonQuery() 方法以前对这个一直都没在意,基本上都没有用其返回值,查了一下MSDN,如下:SqlCommand.ExecuteNonQuery 方法对连接执行 Transac ...

  9. overflow属性的用法

    <style type="text/css">div{ background-color:#00FFFF; width:150px; height:150px; ove ...

  10. 彻底弄懂css中单位px和em,rem的区别

    PX:PX实际上就是像素,用PX设置字体大小时,比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,如果改变了浏览器的缩放,这时会使用我们的Web页面布局被打破.这样 ...