Max Chunks To Make Sorted II LT768
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的更多相关文章
- [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 就是给你一个字符串,能不 ...
- [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
- [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 ...
- 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 ...
- 768. Max Chunks To Make Sorted II
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
- [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 ...
- 【LeetCode】768. Max Chunks To Make Sorted II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-chun ...
- [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 ...
- 最多的划分来使数组有序 Max Chunks To Make Sorted
2018-12-01 11:05:46 一.Max Chunks To Make Sorted 问题描述: 问题求解: 由于没有重复,所以直观的来看对于每个遇到数,其能够被划分出来的前提是其前面已经有 ...
随机推荐
- 【OpenGL】glsl、glew、glfw
glsl: OpenGL着色语言(OpenGL Shading Language)是用来在OpenGL中着色编程的语言,也即开发人员写的短小的自定义程序,他们是在图形卡的GPU (Graphic Pr ...
- 安卓GreenDao框架一些进阶用法整理(转)
大致分为以下几个方面: 一些查询指令整理 使用SQL语句进行特殊查询 检测表字段是否存在 数据库升级 数据库表字段赋初始值 一.查询指令整理 1.链式执行的指令 return mDaoSession. ...
- c++三种进制格式
来源:c++ primer plus 常用的进制有二进制,八进制,十进制,十六进制,在c++的头文件iostream里除了提供了endl控制符之外,还提供了控制进制的控制符,(不含二进制),分别是八进 ...
- Python+Selenium学习--上传文件
场景 文件上传操作也比较常见功能之一,上传功能操作webdriver 并没有提供对应的方法,关键上传文件的思路.上传过程一般要打开一个系统的window 窗口,从窗口选择本地文件添加.所以,一般会卡在 ...
- Shell教程 之变量
1.Shell变量 1.1 定义变量 your_name="http://www.cnblogs.com/uniquefu" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有 ...
- WdatePicker控件Javascript取得当前时间、取得减30分钟时间
1.取得当前时间 function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var se ...
- NIO和IO(BIO)的区别及NIO编程介绍
IO(BIO)和NIO的区别:其本质就是阻塞和非阻塞的区别. 阻塞概念:应用程序在获取网络数据的时候,如果网络传输数据很慢,那么程序就一直等着,直到传输完毕为止. 非阻塞概念:应用程序直接可以获取已经 ...
- iOS - 常用本机URL跳转设置
import UIKit class ViewController: UIViewController { override func touchesBegan(_ touches: Set<U ...
- 视觉slam十四讲
对这个的学习一直都在,感觉到了这本书很强大呀!!! ch2---安装ubuntu:安装kdevelop. ch3---安装eigen3---几何模块:安装Pangolin可视化. ch4---安装So ...
- mysql定位慢查询
mysql定位慢查询 //显示数据库的状态 show status; //显示执行了多少次插入 show status like 'com_insert'; //显示执行了多少次更新 show sta ...