滑动窗口-Moving Stones Until Consecutive II

2020-02-20 16:34:16

问题描述:

问题求解:

    public int[] numMovesStonesII(int[] stones) {
        int n = stones.length;
        Arrays.sort(stones);
        int min = n;
        int start = 0;
        for (int end = 0; end < n; end++) {
            while (stones[end] - stones[start] + 1 > n) start += 1;
            int curr = end - start + 1;
            if (curr == n - 1 && stones[end] - stones[start] + 1 == n - 1) 
                curr = Math.min(min, 2);
            else 
                min = Math.min(min, n - curr);
        }
        return new int[]{min, Math.max(stones[n - 1] - stones[1] + 2 - n, stones[n - 2] - stones[0] + 2 - n)};
    }

  

 

posted @ 2020-02-20 16:35  hyserendipity  阅读(186)  评论(0编辑  收藏  举报