768. Max Chunks To Make Sorted 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 to10**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]
.
Approach #1: Array. [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;
}
}
Analysis:
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 arrys 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 verl too.
Reference:
768. Max Chunks To Make Sorted II的更多相关文章
- [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 ...
- [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 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 ...
- Max Chunks To Make Sorted II LT768
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 可排序的最大块数
Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into som ...
随机推荐
- sql嵌套更新
原地址:http://blog.csdn.net/ycb1689/article/details/43834445 方法一: update a set HIGH=b.NEW from SPEC1 a ...
- PAT 1055 集体照 (25)(STL-list+代码)
1055 集体照 (25)(25 分)提问 拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下: 每排人数为N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排 ...
- Fully Update And Upgrade Offline Debian-based Systems
Let us say, you have a system (Windows or Linux) with high-speed Internet connection at work and a D ...
- DB2错误码
SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 + 未限定的列名被解释为一个有相互联系的引用 + 动态SQL语句用分号结束 + 没有找到满足SQL语句的行 + 用DATA CAPTUR ...
- 如何查看路由器的mac和计算机的mac
如何查看路由器的mac和计算机的mac 一.查看路由器的mac 方法一: 直接看路由器的背面,如下图,即可看到MAC地址 打开命令提示符窗口,输入ipconfig,找到网关地址,如192.168. ...
- java读取网页图片路径并下载到本地
java读取网页图片路径并下载到本地 最近公司需要爬取一些网页上的数据,自己就简单的写了一个demo,其中有一些数据是图片,需要下载下来到本地并且 将图片的路径保存到数据库,示例代码如下: packa ...
- 2018.10.05 NOIP模拟 上升序列(状压dp)
传送门 状压dp好题. 首先需要回忆O(nlogn)O(nlog n)O(nlogn)求lislislis的方法,我们会维护一个单调递增的ddd数组. 可以设计状态f(s1,s2)f(s1,s2)f( ...
- 2018.09.11 bzoj47214721: [Noip2016]蚯蚓(单调队列)
传送门 好题. 目测只会多带一个log2(n+m)" role="presentation" style="position: relative;"& ...
- PHP二个高精确度数字相加减
1.相加 string bcadd(string left operand, string right operand, int [scale]); 2.相减 string bcsub(string ...
- Django-组件
https://www.cnblogs.com/yuanchenqi/articles/8034442.html