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的更多相关文章

  1. [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 ...

  2. [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 就是给你一个字符串,能不 ...

  3. 【LeetCode】768. Max Chunks To Make Sorted II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-chun ...

  4. 768. Max Chunks To Make Sorted II

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  5. [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 ...

  6. [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

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

  8. Max Chunks To Make Sorted II LT768

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

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

随机推荐

  1. Hibernate(9)_双向n对n

    1.概述 ①双向 n-n 关联需要两端都使用集合属性 ②双向n-n关联必须使用连接表 ③集合属性应增加 key 子元素用以映射外键列, 集合元素里还应增加many-to-many子元素关联实体类 ④在 ...

  2. CentOS 7创建自定义KVM模板(现有KVM迁移到另外一台机)

    说明:创建KVM模板有个好处,不用每次都运行命令创建,并且可以为迁移做准备. 一.创建KVM模板 1.下载iso(省略) 2.创建磁盘 qemu-img create -f raw centos7.r ...

  3. C# Activator

    需要动态的创建一个实例模型的时候,就用Activator.CreateInstance(Type type);如果是明确的知道要创建哪个实例的模型,就可以用 new C#在类工厂中动态创建类的实例,所 ...

  4. 为什么样本方差分母是n-1

    https://blog.csdn.net/qq_39521554/article/details/79633207 为什么样本方差的分母是n-1?为什么它又叫做无偏估计? 至于为什么是n-1,可以看 ...

  5. cd4与cd8比值的意义

    正常情况下CD4/CD8比值介于1.5—2.5之间,如CD4是每微升血750个,CD8是每微升血460个,这样两者的比值就是1.63. 虽然95%的正常人CD4/CD8的比值都在1以上,但是也有一些正 ...

  6. linux平台下Tomcat的安装与优化

    Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选.对于一个初学者来说,可以这样 ...

  7. 转自: linux svn命令行无法拉取中文名称的文件

    转自: https://blog.csdn.net/shaohui/article/details/3996274#commentBox svn: Can't convert string from  ...

  8. Win10远程桌面提示你的凭据不工作的处理方法

    需要确保在组策略编辑器(Win+R 输入 gpedit.msc )中计算机配置->Windows设置->安全设置->本地策略->安全选项->右侧的网络访问:本地帐户的共享 ...

  9. [dubbo] Dubbo API 笔记——配置参考

    schema 配置参考 所有配置项分为三大类 服务发现:表示该配置项用于服务的注册与发现,目的是让消费方找到提供方 服务治理:表示该配置项用于治理服务间的关系,或为开发测试提供便利条件 性能调优:表示 ...

  10. How to trigger a Kubernetes cronjob manually-手动触发一个cronjob

    What should you do when you’ve developed and installed a cron job for your Kubernetes application, a ...