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:
arrwill 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 问题描述: 问题求解: 由于没有重复,所以直观的来看对于每个遇到数,其能够被划分出来的前提是其前面已经有 ...
随机推荐
- NPM安装依赖速度慢问题
[NPM安装依赖速度慢问题] npm config set registry http://registry.npm.taobao.org 参考:http://blog.csdn.net/rongbo ...
- idea 安装和破解
https://blog.csdn.net/SmileLvCha/article/details/78936659
- 可上下拖动且有浮沉动画的View
package com.ifenglian.superapp1; import android.animation.Animator;import android.animation.Animator ...
- phpstorm添加站点
1.在界面上快捷键 ctrl+alt+s 或打开File->settings 2.找到Deployment 设置完后确定即可使用.
- python if __name__ == 'main' 的作用和原理()
相信初学者在学习Python的过程中,不可避免的总会遇到 if __name__ == 'main'语句,虽然一直知道它的作用,但是可能一直比较模糊,今天菜鸟分析就与大家一起举例说说我的理解. 举个例 ...
- Flask中的蓝图(BluePrint)、
蓝图 1.初识Flask蓝图(BluePrint) 创建一个项目然后将目录结构做成: user.py中的内容 from flask import Blueprint, render_template ...
- HDU-1087.SuperJUmpingJUmpingJumping.(DP and LISPP)
本题大意:给定一个长度为n的序列a,让你输出这个序列子序列中元素和最大的最大上升子序列. 本题思路:一开始肯定可以想到用LIS实现,我们用LIS实现的时候可以发现这个问题并不满足LIS问题的最优子结构 ...
- supervisor安装和简单使用
安装 pip install superviosr echo_supervisord_conf > /etc/supervisord.conf 编辑配置文件: [unix_http_server ...
- VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到
VS中,添加完Web引用(WebServer引用/Web服务引用),写代码时引用不到 添加完之后要等一会儿 等一会儿 等一会儿 就有了
- IIS下载,WebClient().DownloadFile下载
new System.Net.WebClient().DownloadFile(serverPath, localPath); 有时候使用的时候,文件下载不下来.需要设置一下服务器上IIS的权限