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

    # coding:utf-8 import tornado.web import tornado.options import tornado.httpserver import tornado.io ...

  2. 记一次linux服务部署

    虽然很简单,但是还是想记录一下: 准备一台云主机并连接到云主机上 因为是 springboot 项目,采用了前后端分离部署.所以就没有使用 Tomcat.为应用程序创建文件夹. 添加启动脚本 star ...

  3. linux操作之逻辑分区与交换分区篇

    作业一: 1)   开启Linux系统前添加一块大小为15G的SCSI硬盘 2)   开启系统,右击桌面,打开终端 3)   为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划 ...

  4. 如何在MyBatis中优雅的使用枚举

    问题 在编码过程中,经常会遇到用某个数值来表示某种状态.类型或者阶段的情况,比如有这样一个枚举:   public enum ComputerState { OPEN(10), //开启 CLOSE( ...

  5. CDH下集成spark2.2.0与kafka(四十一):在spark+kafka流处理程序中抛出错误java.lang.NoSuchMethodError: org.apache.kafka.clients.consumer.KafkaConsumer.subscribe(Ljava/util/Collection;)V

    错误信息 19/01/15 19:36:40 WARN consumer.ConsumerConfig: The configuration max.poll.records = 1 was supp ...

  6. C# System.Threading.AutoResetEvent

    表示线程同步事件在一个等待线程释放后收到信号时自动重置. using System; using System.Threading; // Visual Studio: Replace the def ...

  7. 【打印】windows打印控件,Lodop.js介绍

    1.Lodop.js这插件很强大,目前仅支持windows系统 2.使用原生javascript编写 3.lodop支持客户端安装,c-lodop支持服务器端安装 4.无论客户端还是服务器端,都必须是 ...

  8. 【C++】C++中变量的声明与定义的区别

    声明(declaration):意味着告诉编译器关于变量名称.变量类型.变量大小.函数名称.结构名称.大小等等信息,并且在声明阶段不会给变量分配任何的内存. 定义(definition):定义就是在变 ...

  9. 【C#】解析C#中LING的使用

    LING提供了一种从数据源中获取数据的方式,不同的语言已经形成了很多种关联的数据源.LING(Language Integrated Query,语言集成查询)提供一种通用的从不同的数据源中获取数据的 ...

  10. Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)

    Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer   Advanced Installer :Free for 30 da ...