当前位置:网站首页>768. 最多能完成排序的块 II 贪心
768. 最多能完成排序的块 II 贪心
2022-07-27 01:49:00 【钰娘娘】
768. 最多能完成排序的块 II
这个问题和“最多能完成排序的块”相似,但给定数组中的元素可以重复,输入数组最大长度为
2000,其中的元素最大为10**8。
arr是一个可能包含重复元素的整数数组,我们将这个数组分割成几个“块”,并将这些块分别进行排序。之后再连接起来,使得连接的结果和按升序排序后的原数组相同。我们最多能将数组分成多少块?
示例 1:
输入: arr = [5,4,3,2,1] 输出: 1 解释: 将数组分成2块或者更多块,都无法得到所需的结果。 例如,分成 [5, 4], [3, 2, 1] 的结果是 [4, 5, 1, 2, 3],这不是有序的数组。示例 2:
输入: arr = [2,1,3,4,4] 输出: 4 解释: 我们可以把它分成两块,例如 [2, 1], [3, 4, 4]。 然而,分成 [2, 1], [3], [4], [4] 可以得到最多的块数。注意:
arr的长度在[1, 2000]之间。arr[i]的大小在[0, 10**8]之间。来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/max-chunks-to-make-sorted-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
做题结果
成功,还是错了1次,不细心
方法:贪心
1. 先后缀算下后面的最小值
2. 如果当前段的最大值小于等于后续的最小值,则说明此段落可以分割为单独一段
class Solution {
public int maxChunksToSorted(int[] arr) {
int ans = 0;
int n = arr.length;
int[] next = new int[n];
next[n-1]=arr[n-1];
for(int i = n-2; i >= 0; i--){
next[i] = Math.min(arr[i],next[i+1]);
}
int max = 0;
for(int i = 0; i < arr.length; i++){
max = Math.max(max,arr[i]);
if(i==n-1||next[i+1]>=max)++ans;
}
return ans;
}
}边栏推荐
- [flask] the server obtains the file requested by the client
- Pytorch损失函数总结
- pip3 设置阿里云
- 渗透测试-后渗透-痕迹清理
- 图解 SQL,这也太形象了吧!
- Spark Learning Notes (V) -- spark core core programming RDD conversion operator
- 食物链(DAY 79)
- [机缘参悟-52]:交浅言深要因人而异
- Worthington过氧化物酶活性的6种测定方法
- Comprehensive care analysis lyriq Ruige battery safety design
猜你喜欢

Code review pyramid

【树链剖分】2022杭电多校2 1001 Static Query on Tree

若依框架代码生成详解

How many implementation postures of delay queue? Daily essential skills!

Annotation summary of differences between @autowired and @resource

FactoryBean的getObject调用时机

注解@Autowired和@Resource的区别总结

Spark Learning Notes (VI) -- spark core core programming RDD action operator

Explain工具实际操作

MySQL中文失败问题
随机推荐
技术风向标 | 云原生技术架构成熟度模型解读
【flask】服务端获取客户端的请求头信息
Hcip 13th day notes
最大连续子序列(DAY 77)
volatile关键字及其作用
数据库概论 - 数据库的介绍
Customer cases | pay attention to the elderly user experience, and the transformation of bank app to adapt to aging should avoid falsehood and be practical
代码审查金字塔
Deep learning vocabulary embedded, beam search
Safe-arc/warner power supply maintenance xenon lamp power supply maintenance analysis
数据湖(二十):Flink兼容Iceberg目前不足和Iceberg与Hudi对比
Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议
redis秒杀案例,跟着b站尚硅谷老师学习
Explain详解
unity游戏,隐私协议最简单解决方案!仅3行代码就搞定!(转载)
30分钟彻底弄懂 synchronized 锁升级过程
数据库概论 - MySQL的简单介绍
What are "full five unique" and "full two unique"? Any difference?
安全员及环保员岗位职责
Indexing best practices