Max Chunks To Make Sorted LT769
Given an array arr
that is a permutation of [0, 1, ..., arr.length - 1]
, 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 = [4,3,2,1,0]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn't sorted.
Example 2:
Input: arr = [1,0,2,3,4]
Output: 4
Explanation:
We can split into two chunks, such as [1, 0], [2, 3, 4].
However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.
Note:
arr
will have length in range[1, 10]
.arr[i]
will be a permutation of[0, 1, ..., arr.length - 1]
.
1. Iterate the array, if all the elements on the left are smaller than the elements on the left, there is a new chunk. The first solution use two arrays, leftMax[i] to record the max element ending at i and starting from 0, rightMin[i] to record element starting at i and ending at 0.
Time complexity: O(n)
Space complexity: O(n)
class Solution {
public int maxChunksToSorted(int[] arr) {
if(arr == null) return 1; int sz = arr.length;
int[] leftMax = new int[sz];
int[] rightMin = new int[sz]; leftMax[0] = arr[0];
for(int i = 1; i < sz; ++i) {
leftMax[i] = Math.max(leftMax[i-1], arr[i]);
} rightMin[sz-1] = arr[sz-1];
for(int i = sz-2; i >= 0; --i) {
rightMin[i] = Math.min(rightMin[i+1], arr[i]);
} int count = 1;
for(int i = 0; i < sz-1; ++i) {
if(leftMax[i] < rightMin[i+1] ) ++count;
} return count;
}
}
1a. Since we iterate either from left to right or right to left, we do not need two arrays to keep all the previous record and can use one varible to record the max element from the left so far, as long as the max element is smaller than the min element on the right, there is a new chunk
class Solution {
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 max = arr[0];
int count = 1;
for(int i = 0; i < sz-1; ++i) {
max = Math.max(max, arr[i]);
if(max < rightMin[i+1]) ++count;
} return count;
}
}
1b. Iterate from right to left:
class Solution {
public int maxChunksToSorted(int[] arr) {
if(arr == null) return 1; int sz = arr.length;
int[] leftMax = new int[sz];
leftMax[0] = arr[0];
for(int i = 1; i < sz; ++i) {
leftMax[i] = Math.max(leftMax[i-1], arr[i]);
} int rightMin = arr[sz-1];
int count = 1;
for(int i = sz-1; i >= 1; --i) {
rightMin = Math.min(rightMin, arr[i]);
if(leftMax[i-1] < rightMin) {
++count;
}
}
return count;
}
}
2. Since arr[i]
will be a permutation of [0, 1, ..., arr.length - 1], each element is unique and after sorted, arr[i] = i, the elements on the left will be smaller than the elemnts on the right, as long as the max element at index i is arr[i].
Time complexity: O(n)
Space complexity: O(1)
class Solution {
public int maxChunksToSorted(int[] arr) {
if(arr == null) return 1; int maxSoFar = arr[0];
int count = 0;
for(int i = 0; i < arr.length; ++i) {
maxSoFar = Math.max(maxSoFar, arr[i]);
if(maxSoFar == i) ++count;
} return count;
}
}
2a Another slightly optimisation to terminate the loop early if the max element arr[arr.length-1] is found
class Solution {
public int maxChunksToSorted(int[] arr) {
if(arr == null) return 1; int maxSoFar = arr[0];
int count = 0;
for(int i = 0; i < arr.length; ++i) {
maxSoFar = Math.max(maxSoFar, arr[i]);
if(maxSoFar == arr.length-1) return count+1;
if(maxSoFar == i) ++count;
} return count;
}
}
3. Another way to think, if we consider each chunk, as a range [min, max] ended at max, if the next element is smaller than the previous max, we need to merge the range by poping up the max element of chunks which max element is bigger, we need to include the new element in the poped up chunks, otherwise, push the new max element. The number of elements on the stack means the number of chunks.
[4, 3, 2, 1, 0] -> [4] for 4 -> [4] for 3 -> [4] for 2 -> [4] for 1 -> [0]
[1, 0, 2, 3, 4] -> [1] -> [1] -> [1, 2] -> [1, 2, 3] -> [1, 2, 3, 4]
[1, 2, 0, 3] -> [1] -> [1, 2] -> [2] -> [2, 3]
Time complexity: O(n)
Space complexity: O(n)
class Solution {
public int maxChunksToSorted(int[] arr) {
Deque<Integer> maxStack = new LinkedList<Integer>(); for(int num: arr) {
if(maxStack.isEmpty() || num > maxStack.peek()) {
maxStack.push(num);
}
else {
int max = maxStack.peek();
while(!maxStack.isEmpty() && num < maxStack.peek()) {
maxStack.pop();
}
maxStack.push(max);
}
} return maxStack.size();
}
}
3a It can be observed from the code that we always push the current max as where the range ends.
public class Solution { public int maxChunksToSorted(int[] arr) {
Deque<Integer> maxStack = new LinkedList<Integer>(); for(int num: arr) {
int currMax = maxStack.isEmpty()? num: Math.max(num, maxStack.peek()); while(!maxStack.isEmpty() && num < maxStack.peek()) {
maxStack.pop();
} maxStack.push(currMax);
} return maxStack.size();
}
}
4. Another way is to caculate the distance between the current index with the expected sorted index, if the sum is 0, the whole chunk could be a sorted array.
Time complexity: O(n)
Space complexity: O(1)
public class Solution { public int maxChunksToSorted(int[] arr) { int count = 0, sum = 0;
for(int i = 0; i < arr.length; ++i) {
sum += arr[i] - i;
if(sum == 0) ++count;
}
return count;
} }
Max Chunks To Make Sorted LT769的更多相关文章
- [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
- [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 ...
- [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]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 - 768. Max Chunks To Make Sorted II
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
- 最多的划分来使数组有序 Max Chunks To Make Sorted
2018-12-01 11:05:46 一.Max Chunks To Make Sorted 问题描述: 问题求解: 由于没有重复,所以直观的来看对于每个遇到数,其能够被划分出来的前提是其前面已经有 ...
- Max Chunks To Make Sorted II LT768
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] 769. 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 ...
随机推荐
- 1.5.4、CDH 搭建Hadoop在安装之前(定制安装解决方案---配置自定义Java主目录位置)
配置自定义Java主目录位置 注意: Cloudera强烈建议安装JDK/ usr / java / jdk-version,允许Cloudera Manager自动检测并使用正确的JDK版本.如果在 ...
- Linus运行jar包的操作
cd / 返回最顶层文件夹cd home/numa 进入home下的numa文件夹ll 查看当前文加夹下的所有文件ps -ef | grep java ...
- Numpy数据处理函数
Numpy函数介绍 import numpy as np #sqrt 计算各元素的平方根 arr = np.arange(10) np.sqrt(arr) array([0. , 1. , 1.414 ...
- hibernate中调用query.list()而出现的黄色警告线
使用hibernate的时候会用到hql语句查询数据库, 那就一定会用到query.list();这个方法, 那就一定会出现一个长长的黄色的警告线, 不管你想尽什么办法, 总是存在, 虽然说这个黄色的 ...
- leetcode 链表类型题总结
链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...
- ucore-lab1-练习2report
练习二实验报告 1.从CPU加电后执行的第一条指令开始,单步跟踪BIOS: 1.1默认的gdb需要进行一些额外的配置才能进行qemu的调试任务,qemu和gdb之间使用网络端口1234进行通信. la ...
- FPGA基础知识1
1.乘法 在FPGA中,乘法运算可以分为 1)信号与信号之间的运算,用乘法器核实现: 2)常数与信号之间的运算,利用移位及加减法实现. A x 16 = A左移4位: A x 20 = A x 16 ...
- Python开发【第二篇】:Python基本数据类型
运算符 设定:a=10,b=20 . 算数运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为- ...
- Java自学之路(小白向)
首先学习一门语言无非就受教与自学两种方式,本博文针对自学(穷逼没钱报班,大学又不是计算机专业,只能自学啊) 开个玩笑,其实有很多人是适合自学的,下面介绍. 一.要充满自信我自己就可以学好(够面试用的入 ...
- Angular之特性模块 ( Feature Module )
项目结构 一 创建特性模块,及其包含的组件.服务. ng g module art ng g component art/music ng g component art/dance ng g ser ...