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 chunks we could have made?

Example 1:

Input: arr = [4,3,2,1,0]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn't sorted.

Example 2:

Input: arr = [1,0,2,3,4]
Output: 4
Explanation:
We can split into two chunks, such as [1, 0], [2, 3, 4].
However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.

Note:

  • arr will have length in range [1, 10].
  • arr[i] will be a permutation of [0, 1, ..., arr.length - 1].

Approach #1: Array. [Java]

class Solution {
public int maxChunksToSorted(int[] arr) {
int n = arr.length;
int max = 0, ans = 0;
for (int i = 0; i < n; ++i) {
max = Math.max(max, arr[i]);
if (max == i) ans++;
}
return ans;
}
}

  

Approach #2: Array. [Python]

class Solution(object):
def maxChunksToSorted(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
m = 0
ans = 0
for i, n in enumerate(arr):
if n > m: m = n
if m == i: ans += 1
return ans

  

Analysis:

Track max so far

if max == index: ans += 1

Reference:

https://zxi.mytechroad.com/blog/difficulty/medium/leetcode-769-max-chunks-to-make-sorted/

769. Max Chunks To Make Sorted的更多相关文章

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

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

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

  4. 【LeetCode】769. Max Chunks To Make Sorted 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

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

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

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

  8. [LeetCode] 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 ...

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

随机推荐

  1. C++的MFC 与 HTML 双向通讯

    C++中嵌入ie浏览器总结(1) - ie边框 及上下文菜单 最近项目中用html 来做界面,也就折腾了一下在wxwidget中嵌入浏览器的若干细节工作,mfc也基本是类似的,由于wxwidget中已 ...

  2. spring框架之依赖注入(DI)

    1. IOC和DI的概念 * IOC -- Inverse of Control,控制反转,将对象的创建权反转给Spring!! * DI -- Dependency Injection,依赖注入,在 ...

  3. kafka 报Failed to load class "org.slf4j.impl.StaticLoggerBinder".[z]

    转:http://blog.chinaunix.net/uid-25135004-id-4172954.html 测试kafka    producer发送消息 和  consumer 接受消息报错 ...

  4. hg 添加用户

    .hg目录下hgrc文件 [ui] username = lyd

  5. process概念

    multiprocess: multiprocess.cpu_count():统计cpu核数 multiprocess.active_chirdren():获取所有的子进程 multiprocess. ...

  6. 什么是tcp协议?

    这是世界上最顶尖的tcp讲解技术...

  7. python panda库自动去重

    http://blog.csdn.net/xinxing__8185/article/details/48022401

  8. Perl的调试模式熟悉和应用

    perl -d file.pl perl -c file.pl DB<1> hList/search source lines:               Control script ...

  9. 详解Windows Service Wrapper(winsw.exe)及应用场景

    winsw.exe可以帮助nginx作为windows服务自启动,不需要每次都输入命令,很方便,使用到目前为止这种做法的效果很完美.你得到了 Windows 服务的支持,而且在服务重启时没有遗留孤立的 ...

  10. phoneGap,angularJs,onSen的一些备忘

    1.ng-click="funcName";这里的funcName需要再控制器里的$scope.funcName=function(){}进行定义 2.ng-controller= ...