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 问题描述: 问题求解: 由于没有重复,所以直观的来看对于每个遇到数,其能够被划分出来的前提是其前面已经有 ...
随机推荐
- redis 启动
C:\Users\Administrator>cd c:\ c:\>cd redis-2.6 c:\redis-2.6>redis-server.exe redis.conf 测试r ...
- RecyclerView的单击和长按事件(转)
转自:http://www.jianshu.com/p/f2e0463e5aef 前言 上一篇文章揭开RecyclerView的神秘面纱(一):RecyclerView的基本使用中,主要讲述了Recy ...
- elk6快速安装
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch [elasticsearch-6.x] name=Elasticsear ...
- 使用phpStudy运行伊人集项目
1.首次运行时,需要把system/config/install.look.php以及system/config/database.php(后面这个文件可以先不删除,若是安装过程中数据库报错,再来删除 ...
- Codeforces Beta Round #70 (Div. 2)
Codeforces Beta Round #70 (Div. 2) http://codeforces.com/contest/78 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #42 (Div. 2)
Codeforces Beta Round #42 (Div. 2) http://codeforces.com/contest/43 A #include<bits/stdc++.h> ...
- 对于装office 365时,visio不兼容的解决
先将office 365装好,之后从这个网址下载visio安装即可,但是没有 激活码,需要自己激活: 网址:链接: https://pan.baidu.com/s/1OqONPuJ0eumrpts-X ...
- 修改bootstrap-table中的分页样式
使用bootstrap-table时,使用$("")选择器没办法选中下方的分页button按钮,可能跟它是动态生成的有关吧. 最终找到与之对应的js(bootstrap-table ...
- MYSQL分析慢查询
mysql慢查询的日志文件路径一般为: /var/lib/mysql/slowquery.log,具体的路径可以通过mysql配置文件(/etc/my.cnf)查询,slow_query_log_fi ...
- 三角化---深度滤波器---单目稠密重建(高翔slam---十三讲)
一.三角化 [1]三角化得到空间点的三维信息(深度值) (1)三角化的提出 三角化最早由高斯提出,并应用于测量学中.简单来讲就是:在不同的位置观测同一个三维点P(x, y, z),已知在不同位置处观察 ...