sort chunk

2018-06-08  本文已影响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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读