美文网首页
sort chunk

sort chunk

作者: 浮安樊 | 来源:发表于2018-06-08 04:02 被阅读0次

给一个数组,要求你尽可能多的切割这个数组,使得每一个小段分别sort之后,整个数组就sort了

if minValue of Latter chunk is larger than the max value of former chunk, there can be a slice.

traverse the array from end to beginning

class Solution {
    public int getChunks (int[] nums) {
        List<Integer> list = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return -1;
        }

        int min = nums[nums.length - 1];
        for (int i = nums.length - 1; i >= 0; i--) {
            if (nums[i]  > min) {
                continue;
            }

            list.add(i + 1);
            min = nums[i];
        } // end of for

        return list.size() + 1;
    }
}

相关文章

网友评论

      本文标题:sort chunk

      本文链接:https://www.haomeiwen.com/subject/ptcosftx.html