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. Windows平台下SVN安装配置及使用

    原文链接:https://www.cnblogs.com/snake-hand/archive/2013/06/09/3130022.html,等有空了玩一玩吧,现在没空.

  2. GMA Round 1 奇怪的数列

    传送门 奇怪的数列 已知数列{$a_n$},$a_1=1$,$a_{n+1}=a_n+\frac{1}{a_n}$,现在需要你估计$a_{233333}$的值,求出它的整数部分即可. 将原等式两边平方 ...

  3. 12、Java函数接口

    注:新版本接口中Iterable已换成Iterator    

  4. IIS安装、配置 发布网站 报错解决方案

    错误一:   HTTP 错误 500.19- Internal Server Error法请求该页面配置,因为页面的相关配置数据无效####   HTTP 错误 500.21 - Internal S ...

  5. n2n的编译和运行、配置

    交叉编译: cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/CMakeToolchainFileMingw32.cmake -build ./ ../ 1.n2n  基于p ...

  6. html5使用canvas实现毫秒级画心电图

  7. html5学习笔记3——高级特性

    一:Web存储 数据以 键/值 对存在, web网页的数据只允许该网页访问使用. web存储有两种: localStorage - 没有时间限制的数据存储,存于浏览器缓存 sessionStorage ...

  8. [转]恕我直言,在座的各位根本不会写 Java!

    导语 自 2013 年毕业后,今年已经是我工作的第 4 个年头了,总在做 Java 相关的工作,终于有时间坐下来,写一篇关于 Java 写法的一篇文章,来探讨一下如果你真的是一个 Java 程序员,那 ...

  9. Laravel: 基础篇

    一.安装 1)采用一键安装包 http://laravelacademy.org/resources-download 2)Mac 上安装 ----------在Mac上安装composer----- ...

  10. (原)Max Area of Island(即连通域标记)

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/10493114.html 1. 问题 Given a non-empty 2D array grid ...