leetcode 768. Max Chunks To Make Sorted II(可排序的最大块数II)

jeecg-boot首页加载速度优化全过程 load时间从二十多秒降至三四秒,来看看都经历了啥吧 阅读详情

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:

arr will have length in range [1, 2000].
arr[i] will be an integer in range [0, 10**8].

和769.可排序最大块数类似,分块局部排序,拼在一起后整体要是升序的。不同之处在于数字可以有相同的。

思路:
769题采用下标来判断某数字之前的数字是否全出现过,因为有相同的数字所以不能再用此方法。

方法1: 局部和
能单独分块的部分元素的和 == 排序后相同部分的元素和
出现元素和相等的部分时块数++

保存和时用long型

//1ms
    public int maxChunksToSorted(int[] arr) {
        int n = arr.length;
        int[] sorted = arr.clone();
        Arrays.sort(arr);
        long sum1 = 0;
        long sum2 = 0;
        int result = 0;
        
        for(int i = 0; i < n; i++) {
            sum1 += arr[i];
            sum2 += sorted[i];
            if(sum1 == sum2) {
                result ++;
            } 
        }
        return result;
    }

方法2:单调stack
和769题思路类似,能分块的部分要保证<=当前元素的值都已经出现过。>当前元素的值来时说明块数可+1,<=当前元素的值来时要找到之前比它大的值作为一块。
按照这个思路,>当前元素的值来时压入stack保存,<=当前元素的值来时从stack取出元素直到找到比它大的值,把这个比它大的值压入stack作为某块的结尾。
最后只要看stack中有几个元素,就代表存了几块的结尾。

//3ms
    public int maxChunksToSorted(int[] arr) {
        int n = arr.length;
        Stack<Integer> stack = new Stack<>();
        stack.push(arr[0]);
        int maxNum = 0;
        
        for(int i = 1; i < n; i++) {
            if(arr[i] >= stack.peek()) {
                stack.push(arr[i]);
            } else {
                maxNum = stack.pop();
                while(!stack.empty() && stack.peek() > arr[i]) {
                    stack.pop();
                }
                stack.push(maxNum);
            }
        }
        return stack.size();
    }

参考资料

AttifyOS 3.0实战:从固件提取到物联网设备安全评估全流程解析 本文详细解析了使用AttifyOS 3.0进行物联网设备安全评估的全流程。从获取D-Link路由器固件开始,到利用其预置的Binwalk等工具完成固件提取、解包Squashfs文件系统,并深入分析Web源码进行初步安全审计。这套集成系统极大简化了环境配置,让研究人员能专注于漏洞挖掘本身,是物联网安全评估的高效工具箱。 阅读详情

相关推荐

ENVI与Sarscape协同处理哨兵一号数据的完整流程解析

本文详细解析了利用ENVI与Sarscape软件协同处理哨兵一号(Sentinel-1)SAR数据的完整工作流程。从环境搭建、数据导入与裁剪,到核心的DEM辅助多时相配准、斑点噪声滤波,再到最终的地理编码与辐射定标,提供了标准化的操作指南与实用技巧,帮助用户高效地将原始雷达数据转化为可用于分析的地理编码产品。

weixin_29251131的博客 349

整理webpack不同的一些配置

webpack的配置收纳

weixin_43842488的博客 1236

工控AI从2026到2030:OPC UA、MCP与数字孪生的落地实践

工业自动化与AI的融合正从概念走向工程落地。其核心原理在于通过标准化协议打通设备层、数据层与AI层,实现感知、决策、执行链路的闭环。OPC UA统一了设备信息模型,MCP则标准化了AI与工具间的调用接口,二者结合将大幅降低系统集成复杂度。技术价值在于提升数据质量与连接可靠性,使AI推理能安全地辅助甚至优化控制。典型应用包括产线数据采集、数字孪生同步、PLC代码生成与边缘智能。本文深入探讨了这些关键技术在工控场景中的实操要点与常见陷阱。

weixin_32025789的博客 192

前端限制打包文件数量

一般来说考虑到加载文件过大,会打包成多个js或css文件来加载,按甲方的意思就不需要考虑到文件加载问题了,先把静态资源文件过多问题解决了。如果是Webpack搭建的项目,直接看webpack官网,LimitChunkCountPlugin插件的maxChunks、minChunkSize这两个配置。官方对于这个模块的解释是:在编写代码时,您可能已经添加了许多代码拆分点来按需加载内容。编译后您可能会注意到某些块太小会产生更大的 HTTP 开销。

weixin_45088043的博客 1848

量化策略分享之ST股弱转强,回测年化123%

本策略的核心是精选ST股票,国九条过滤,防止直接暴雷退市,只做ST股票的弱转强,弱转强的认定标准是前日涨停但昨日不涨停,昨日收盘价大于前一日最低价,昨日收盘价大于10日线,昨日成交量大于前一日的成交量但小于前一日成交量的10倍,股价大于1;这类股票强势,有资金介入,能获得超额收益。

qq_29301281的博客 1285

Unity UGUI高斯模糊背景效果 简易解决方案

UnityUI背景模糊效果简易解决方案1.功能描述简易界面框架搭建合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 1.功能描述 在我们玩游戏的时候,经常会看到一些游戏会对UI做这样的处理: 可以看到,新开的上层UI界面会以下层UI界面的模糊

SaberZG的博客 1万+

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 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

蓝羽飞鸟的博客 237

多模型对抗性审查:用AI辩论提升技术文档质量

在软件工程领域,技术文档是团队协作和项目成功的关键基础。传统的文档评审依赖人工经验,存在盲区和效率瓶颈。通过引入多模型对抗性审查机制,不同大语言模型从各自视角对文档进行批判性分析,模拟现实中的专家评审过程。这种技术方法的核心价值在于利用模型间的思维差异,自动发现单个视角容易忽略的边界情况、逻辑漏洞和隐含假设。在应用场景上,特别适合产品需求文档和技术规格书这类需要多角度审视的复杂文档。adversarial-spec工具通过Claude协调多模型辩论,实现从概念到实现的完整文档质量保障,显著提升文档的完整性和

weixin_42531579的博客 389

最多的划分来使数组有序 Max Chunks To Make Sorted

2018-12-01 11:05:46 一、Max Chunks To Make Sorted 问题描述: 问题求解: 由于没有重复,所以直观的来看对于每个遇到数,其能够被划分出来的前提是其前面已经有相应的数字,具体可以看下面的图片。 public int maxChunksToSorted(int[] arr) { if (arr == ...

aitie1479的博客 212

芯片低功耗设计的两种常用EDA流程

如题。

EricWZY2008的专栏 5578

【FISCO-BCOS】 八、ubuntu配置Java环境

ubuntu配置Java环境需根据自己的java版本号进行细节修改,本文以jdk11.0.18版本为例。ps:通过此法还可以查看java安装路径,但必须是环境配置成功的情况下才能使用。二、添加Java环境路径。四、检查java环境设置。

naihe_fish的博客 4328

[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 length2000, and the elements could be up t...

weixin_30716725的博客 125

海思3559av100平台lan9500模块调试记录

1. 前言 通过USB2.0接口扩展一路百兆虚拟网口 2. 配置lan9500驱动 make ARCH=arm64 menuconfig 开启LAN95XX驱动,编译进内核 保存内核配置 3. 编译内核 make ARCH=arm64 CROSS_COMPILE=aarch64-himix100-linux- uImage -j 20 cd ../../arm-trusted-firmware/arm-trusted-firmware ./mk.sh cp ...

jzwjzw19900922的博客 913

LeetCode //C - 768. Max Chunks To Make Sorted II

Article summary: The question requires that the array be divided into as many blocks as possible, so that each block is sorted separately and then spliced ​​together to be equal to the result of the original array after sorting. The key to the solution is:

Made in Code 605

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

[Leetcode] 768. Max Chunks To Make Sorted II 解题报告

题目: 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 res

魔豆(Magicbean)的博客 722

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

LeetCode – 768. Max Chunks To Make Sorted II

This question is the same as &quot;Max Chunks to Make Sorted&quot; except the integers of the given array are not necessarily distinct, the input array could be up to length&amp;nbsp;2000, and the elements could b...

Yanliang 600

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[Swift]LeetCode768. 最多能完成排序的块 II | Max Chunks To Make Sorted II

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30553065的博客 214

LeetCode 768. Max Chunks To Make Sorted II

原题链接在这里:https://leetcode.com/problems/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 disti...

weixin_30650859的博客 109

769/768. Max Chunks To Make Sorted 1 and 2

769 题意: 一个数组是 0-N-1 的组合, 数组可以被分成多个chunks, 主要chunks 内部排序好了,整个数组就可以排序好。 768:题目: 和768 类似,只是数组是任意的,不是0-N-1的组合。 Input: arr = [1,0,2,3,4] Output: 4 Explanation: We can split into two chunks, ...

apma88030的博客 134

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 array are not necessarily distinct, the input array could be up to length2000, and the elements could be up t...

cmy203的博客 121

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 some number of “chunks” (partitions), and individually sort each chunk. After concatenating them, the res...

柳婼 の blog 1421

leetcode 困难题 768. Max Chunks To Make Sorted II 最多能完成排序的块 II-耗时100%

邹九的个人博客 613

LeetCode-768. Max Chunks To Make Sorted II [C++][Java]

We splitarrinto some number ofchunks(i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array. Returnthe largest number of chunks we can make to sort the array.

贫道绝缘子的博客 196

768. Max Chunks To Make Sorted II

https://leetcode.com/problems/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 arr...

Young's Blog 138

LeetCode 768. 最多能完成排序的块 II 贪心 + 单调栈 + 前缀和

算法

qq_45766916的博客 358

Max Chunks To Make Sorted

LeetCode 768.Max Chunks To Make Sorted I Given an arrayarrof integers (not necessarily distinct), we split the array into some number of "chunks" (partitions), and individually sort each ch...

weixin_30951743的博客 130
上一篇: leetcode 719. Find K-th Smallest Pair Distance(找到第k小的距离)
下一篇: leetcode 896. Monotonic Array(单调数组)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址