滑动窗口-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)};
    }

  

原文地址:https://www.cnblogs.com/hyserendipity/p/12336324.html

时间: 2024-08-27 15:11:00

滑动窗口-Moving Stones Until Consecutive II的相关文章

[滑动窗口] leetcode 1004 Max Consecutive Ones III

problem:https://leetcode.com/problems/max-consecutive-ones-iii/ 维护最多包含k个0的滑动窗口,一旦超过了k个0,把队首的0 pop出来.不断更新当前滑动窗口中的数据个数,并取最大值返回即可. class Solution { public: int longestOnes(vector<int>& A, int K) { int count = 0; int index = -1; deque<int> zer

[Swift]LeetCode1033. 边框着色 | Moving Stones Until Consecutive

Given a 2-dimensional grid of integers, each value in the grid represents the color of the grid square at that location. Two squares belong to the same connected component if and only if they have the same color and are next to each other in any of t

LeetCode.1033-移动石头直到连续(Moving Stones Until Consecutive)

这是小川的第386次更新,第414篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第247题(顺位题号是1033).在a,b和c位置的数字线上有三块石头.每次,你在一个终点(即最低或最高位置的石头)上拾取一块石头,然后将它移动到这些终点之间的空置位置. 形式上,假设石头当前位于x,y,z位置,x <y <z.你在x位置或z位置拾取石头,然后将石头移动到整数位置k,x <k <z且k!= y. 当你不能做任何移动时,游戏结束.例如,当石头处于连续的位置时.

[leetcode]346. Moving Average from Data Stream滑动窗口平均值

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAverage m = new MovingAverage(3); m.next(1) = 1 m.next(10) = (1 + 10) / 2 m.next(3) = (1 + 10 + 3) / 3 m.next(5) = (1

leetcode 1040. 移动石子直到连续 II(滑动窗口)

题意: 在一个长度无限的数轴上,第 i 颗石子的位置为 stones[i].如果一颗石子的位置最小/最大,那么该石子被称作端点石子. 每个回合,你可以将一颗端点石子拿起并移动到一个未占用的位置,使得该石子不再是一颗端点石子. 值得注意的是,如果石子像 stones = [1,2,5] 这样,你将无法移动位于位置 5 的端点石子,因为无论将它移动到任何位置(例如 0 或 3),该石子都仍然会是端点石子. 当你无法进行任何移动时,即,这些石子的位置连续时,游戏结束. 要使游戏结束,你可以执行的最小和

详解--单调队列 经典滑动窗口问题

单调队列,即单调的队列.使用频率不高,但在有些程序中会有非同寻常的作用. 动态规划·单调队列的理解 做动态规划时常常会见到形如这样的转移方程: f[x] = max or min{g(k) | b[x] <= k < x} + w[x] (其中b[x]随x单调不降,即b[1]<=b[2]<=b[3]<=...<=b[n]) (g[k]表示一个和k或f[k]有关的函数,w[x]表示一个和x有关的函数) 这个方程怎样求解呢?我们注意到这样一个性质:如果存在两个数j, k,使

单调队列(滑动窗口问题)

Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards b

[leetcode]239. Sliding Window Maximum滑动窗口最大值

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding

[Swift]LeetCode480. 滑动窗口中位数 | Sliding Window Median

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Given an a