[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 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].
769. Max Chunks To Make Sorted的拓展,这一题可以有重复的数字。
解法:能形成块儿的数字之和跟排序后的数组的相同长度的子数组的数字之和是相同的。
Algorithm: Iterate through the array, each time all elements to the left are smaller (or equal) to all elements to the right, there is a new chunck.
Use two arrays to store the left max and right min to achieve O(n) time complexity. Space complexity is O(n) too.
This algorithm can be used to solve ver1 too.
Java:
class Solution {
public int maxChunksToSorted(int[] arr) {
int n = arr.length;
int[] maxOfLeft = new int[n];
int[] minOfRight = new int[n];
maxOfLeft[0] = arr[0];
for (int i = 1; i < n; i++) {
maxOfLeft[i] = Math.max(maxOfLeft[i-1], arr[i]);
}
minOfRight[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--) {
minOfRight[i] = Math.min(minOfRight[i + 1], arr[i]);
}
int res = 0;
for (int i = 0; i < n - 1; i++) {
if (maxOfLeft[i] <= minOfRight[i + 1]) res++;
}
return res + 1;
}
}
Python:
def maxChunksToSorted(self, arr):
res, c1, c2 = 0, collections.Counter(), collections.Counter()
for a, b in zip(arr, sorted(arr)):
c1[a] += 1
c2[b] += 1
res += c1 == c2
return res
C++:
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
int res = 0, sum1 = 0, sum2 = 0;
vector<int> expect = arr;
sort(expect.begin(), expect.end());
for (int i = 0; i < arr.size(); ++i) {
sum1 += arr[i];
sum2 += expect[i];
if (sum1 == sum2) ++res;
}
return res;
}
};
C++:
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
int res = 1, n = arr.size();
vector<int> f = arr, b = arr;
for (int i = 1; i < n; ++i) f[i] = max(arr[i], f[i - 1]);
for (int i = n - 2; i >= 0; --i) b[i] = min(arr[i], b[i + 1]);
for (int i = 0; i < n - 1; ++i) {
if (f[i] <= b[i + 1]) ++res;
}
return res;
}
};
C++:
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
int res = 1, n = arr.size(), curMax = INT_MIN;
vector<int> b = arr;
for (int i = n - 2; i >= 0; --i) b[i] = min(arr[i], b[i + 1]);
for (int i = 0; i < n - 1; ++i) {
curMax = max(curMax, arr[i]);
if (curMax <= b[i + 1]) ++res;
}
return res;
}
};
C++:
class Solution {
public:
int maxChunksToSorted(vector<int>& arr) {
stack<int> st;
for (int i = 0; i < arr.size(); ++i) {
if (st.empty() || st.top() <= arr[i]) {
st.push(arr[i]);
} else {
int curMax = st.top(); st.pop();
while (!st.empty() && st.top() > arr[i]) st.pop();
st.push(curMax);
}
}
return st.size();
}
};
类似题目:
[LeetCode] 769. Max Chunks To Make Sorted 可排序的最大块数
All LeetCode Questions List 题目汇总
[LeetCode] 768. Max Chunks To Make Sorted II 可排序的最大块数 II的更多相关文章
- 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 ...
- [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 ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-chun ...
- 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 ...
- [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 ...
随机推荐
- SpringBoot中实现Spring容器中注入类型相同但名不同Bean
@Bean(autowire = Autowire.BY_NAME,value = "kaptchaProducer") public Producer kaptchaProduc ...
- frp开机启动
暂时只介绍linux版本的做法,作为备忘. 添加systemd配置文件: vim /usr/lib/systemd/system/frp.service 文件内容如下: [Unit] Descript ...
- 05_配置交换机SSH服务(数通华为)
1. 网络拓扑: 2. SW1配置:2.1 配置为Access口,vlan 10:[SW1]vlan 10[SW1-GigabitEthernet0/0/1]port link-type access ...
- 第04组alpha冲刺(3/4)
队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(3/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.收集各个组员的进度 2.写博客 展示GitHub当日代码/文档签入记录: 接下 ...
- nginx 反向代理配置示例
Nginx反向代理在生产环境中使用很多的. 场景1: 域名没有备案,可以把域名解析到香港一台云主机上,在香港云主机做个代理,而网站数据是在大陆的服务器上. server { listen ; serv ...
- Shell 逐行读取文件的4中方法
方法1:while循环中执行效率最高,最常用的方法. function while_read_LINE_bottm(){ While read LINE do echo $LINE done < ...
- 二分法递归版本(c++)
利用二分法求解在区间[0,π/2]上的根 #include<iostream> #include <cmath> using namespace std; double dic ...
- c语言用指针定义一个类型进行输入输出
1 整型数组 // #include "stdafx.h" #include "stdlib.h" int _tmain(int argc, _TCHAR* a ...
- vs2017添加区域或者视图出错
删除以下文件的信息:C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
- 【Beta】Scrum meeting 6 & 助教参会记录
github 本此会议项目由PM召开,召开时间为5月13日晚上10点 召开时长15分钟 任务表格 姓名 当前任务 下阶段任务 袁勤 初步实现后端题库功能 优化后端 彭一夫 向数据库导入新题 查看评论功 ...