DeepSeek LeetCode 769. Max Chunks To Make Sorted Java Implement

本文详解LeetCode 769题‘Max Chunks To Make Sorted’的Java实现,利用数组为[0,n-1]排列的特性,通过贪心策略维护运行最大值,当max==i时切分一个有效块。时间复杂度O(n),空间复杂度O(1)。核心在于理解排列性质如何保证前缀包含全部0..i值,从而支持独立排序。

LeetCode 769 — Max Chunks To Make Sorted (Java)

Approach: Greedy — track running maximum

The array is a permutation of [0, n-1]. A chunk [0…i] can be split off exactly when the maximum value seen so far equals the index i. At that point, all values 0…i are contained in the prefix, so it can be sorted independently and forms one valid chunk.

class Solution {
    public int maxChunksToSorted(int[] arr) {
        int chunks = 0;
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            max = Math.max(max, arr[i]);
            if (max == i) { // prefix [0..i] contains exactly values 0..i
                chunks++;
            }
        }
        return chunks;
    }
}

Complete Runnable Example

public class Main {
    public static void main(String[] args) {
        Solution sol = new Solution();

        System.out.println(sol.maxChunksToSorted(new int[]{4,3,2,1,0})); // 1
        System.out.println(sol.maxChunksToSorted(new int[]{1,0,2,3,4})); // 4
        System.out.println(sol.maxChunksToSorted(new int[]{0,1,2,3}));   // 4
        System.out.println(sol.maxChunksToSorted(new int[]{2,0,1}));     // 1
        System.out.println(sol.maxChunksToSorted(new int[]{0}));         // 1
    }
}

Walkthrough — arr = [1, 0, 2, 3, 4]

i arr[i] max max == i? chunks
0 1 1 no 0
1 0 1 yes 1
2 2 2 yes 2
3 3 3 yes 3
4 4 4 yes 4

Result: 4 ✓ (chunks: [1,0], [2], [3], [4])

Walkthrough — arr = [4,3,2,1,0]

i arr[i] max max == i? chunks
0 4 4 no 0
1 3 4 no 0
2 2 4 no 0
3 1 4 no 0
4 0 4 yes 1

Result: 1 ✓

Complexity

Metric Complexity
Time O(n) — single pass
Space O(1)

Key Points

  1. Permutation property is crucial — because arr contains exactly 0…n-1, the prefix [0…i] is independently sortable iff its maximum is i. All values 0…i are guaranteed to be present in that prefix.
  2. Greedy cut is optimal — cutting as early as possible (whenever max == i) maximizes the number of chunks.
  3. Contrast with 768 — in Max Chunks To Make Sorted II, elements may be duplicated or unsorted, so you’d need a prefix-max vs. sorted-prefix-min comparison (or monotonic stack). Here the permutation constraint reduces it to a single running max.
  4. Single pass, O(1) extra space — no sorting, no auxiliary arrays.

Why not just compare arr[i] == i?

Because a chunk may be larger than one element. [1, 0] has neither element equal to its index, but the whole chunk is valid. The running-max trick correctly handles that: at i = 1, max = 1 == i, so we cut.

Optional: (For reference only) version without the permutation assumption

If you ever need a variant that works on arbitrary arrays (i.e., LeetCode 768), the idea changes — but for this problem the above is the canonical, optimal solution.
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址