sort chunk

给一个数组,要求你尽可能多的切割这个数组,使得每一个小段分别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;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,483评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,998评论 0 23
  • 目前,我在外企工作,外企里分为两种人,外国人和中国人,而在中国人里又分为两种人,会说英语的和0.000000001...
    僞青年阅读 704评论 0 0
  • 雨落声,风吹叶。已是秋来,余署共情燃。绵绵妾意胜如絮,花落闺中秀,尽由儿郎思。 秋雨复,求雁归。倚栏顾盼,窃语莫伤...
    迦夜诗人阅读 248评论 0 0