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 问题描述: 问题求解: 由于没有重复,所以直观的来看对于每个遇到数,其能够被划分出来的前提是其前面已经有 ...
随机推荐
- pip安装离线包
离线包从pypi.org下载 pip download -r requirements.txt -d /tmp/paks/ 在linux下 1.下载指定的包到指定文件夹. ...
- Mysql 数据类型(基础5)
Mysql 常用的4种数据类型: 整型 int. 浮点型 double. 日期类型 datetime .字符型( varchar char text ) #创建一张表 mysql> create ...
- serv-U使用
该软件是设置ftp服务器的 可以百度查询ftp服务器安装攻略,如 https://jingyan.baidu.com/article/cb5d6105c00bba005c2fe0ca.html 问题: ...
- 缩点+最小路径覆盖 hdu 3861
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意:输入t,表示t个样例.接下来每个样例第一行有两个数n,m表示点数和有向边的数量,接下来输入 ...
- leecode 978. Longest Turbulent Subarray(最长连续波动序列,DP or 滚动数组)
传送门:点我 978. Longest Turbulent Subarray A subarray A[i], A[i+1], ..., A[j] of A is said to be turbule ...
- http://ctf.bugku.com/challenges#%E6%B8%B8%E6%88%8F%E8%BF%87%E5%85%B3--游戏过关
做成功这道逆向题了,哈哈哈哈. 启程. 运行了一下子程序,发现它是要保证所有灯亮着才会给flag.如下图所示. 我聪明滴认为首先可以通过关键字符串找到关键代码位置哦. 1.找到关键代码 ...
- Netty实践一(数据通信)
我们需要了解下在真正项目应用中如何去考虑Netty的使用,大体上对于一些参数设置都是根据服务器性能决定的.这个不是最主要的. 我们需要考虑的问题是两台机器(甚至多台)使用Netty的怎样进行通信,大体 ...
- java函数方法
1.方法重载 (1)源代码 // MethodOverload.java // Using overloaded methods public class MethodOverload { publi ...
- shell脚本计算斐波那契数列
计算斐波那契数列 [1,1,2,3,5,8,,,,,] #!/bin/bash n=$ num=( ) i= while [[ $i -lt $n ]] do let num[$i]=num[$i-] ...
- linux命令学习之:cd
cd命令用来切换工作目录至dirname. 其中dirName表示法可为绝对路径或相对路径.若目录名称省略,则变换至使用者的home directory(也就是刚login时所在的目录).另外,~也表 ...