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 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.
class Solution {
public int maxChunksToSorted(int[] arr) {
if (arr == null)
return 0;
int[] arr2 = new int[arr.length];
for (int i=0; i<arr.length; i++) {
arr2[i] = arr[i];
}
Arrays.sort(arr2);
int sum1 = 0, sum2 = 0, ret = 0;
for (int i=0; i<arr.length; i++) {
sum1 += arr[i];
sum2 += arr2[i];
if (sum1 == sum2)
ret ++;
}
return ret;
}
}
LeetCode - 768. Max Chunks To Make Sorted II的更多相关文章
- [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]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 ...
- [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 ...
随机推荐
- 实验楼-Git实战教程
实验1-git介绍 1.版本控制系统: 1)集中式版本控制系统:版本库是集中存放在中央服务器的,工作时需要先从中央服务器取得最新的版本,然后工作完成后把自己的修订推送给中央服务器.这类系统都有一个单一 ...
- django之模版层(template)
上篇主要介绍了django的MTV模型,主要介绍了视图层之路由配置系统url分发和视图层之视图函数view,本篇主要讲解MTV模型中的模版层template. 本篇导论: 模版简介 模版之变量 模版之 ...
- dom4j解析xml字符串实例
DOM4J 与利用DOM.SAX.JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异.功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml ...
- JSOUP 打开url的方式
一般采用这种方式: try{ doc = Jsoup.connect(url) .header("User-Agent", "Mozilla/5.0 (Windows N ...
- 移动应用开发技术选型:WebApp>HybridApp>NativeApp
一:概念辨析 Web App:生存在浏览器里的应用,只能运行在浏览器里,宿主是浏览器,不是操作系统.资源一般都在网络上,就是一个触屏版的网站.如:微信公众号.不需要在设备上下载安装,只需通过浏览器即可 ...
- C# WebAPI学习
WebApi是微软在VS2012 MVC4版本中绑定发行的,WebApi是完全基于Restful标准的框架.RestFul: (英文:Representational State Transfer,简 ...
- Java之Servlet
Servlet规范了JavaWeb项目的结构Servlet的规范约束了服务器如何来实现Servlet规范,如何解析JavaWeb项目的结构. Java就是通过接口来约束 Servlet规范的jar就在 ...
- The best manual of how to use "The easiest Xdebug" addon for Firefox
Installation notes 0. Install the best Firefox add-on for remote debugging The easiest Xdebug. I'm n ...
- 每天进步一点点——mysql——mysqlbinlog
一. 简单介绍 mysqlbinlog:用于查看server生成的二进制日志的工具. 二. 命令格式 mysqlbinlog 选项日志文件1 三. 经常使用參数 ...
- asp.net mvc 微信支付代码分析(根据沐雪微信平台3.1商城业务来分析)
开发微信应用,微信支付是永远要面对的.现在的微信支付相对以往已经很稳定,很少出现诡异情况.再加上无数人开发的经验分享,现在开发微信支付已经没什么难度了. 我这次主要是想基于沐雪微信平台的微商城业务来分 ...