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

题意:

在一个长度无限的数轴上,第 i 颗石子的位置为 stones[i]。如果一颗石子的位置最小/最大,那么该石子被称作端点石子。
 
每个回合,你可以将一颗端点石子拿起并移动到一个未占用的位置,使得该石子不再是一颗端点石子。
 
值得注意的是,如果石子像 stones = [1,2,5] 这样,你将无法移动位于位置 5 的端点石子,因为无论将它移动到任何位置(例如 0 或 3),该石子都仍然会是端点石子。
 
当你无法进行任何移动时,即,这些石子的位置连续时,游戏结束。
 
要使游戏结束,你可以执行的最小和最大移动次数分别是多少? 以长度为 2 的数组形式返回答案:answer = [minimum_moves, maximum_moves] 。
 
 
 
示例 1:
 
输入:[7,4,9]
输出:[1,2]
解释:
我们可以移动一次,4 -> 8,游戏结束。
或者,我们可以移动两次 9 -> 5,4 -> 6,游戏结束。
示例 2:
 
输入:[6,5,4,3,10]
输出:[2,3]
解释:
我们可以移动 3 -> 8,接着是 10 -> 7,游戏结束。
或者,我们可以移动 3 -> 7, 4 -> 8, 5 -> 9,游戏结束。
注意,我们无法进行 10 -> 2 这样的移动来结束游戏,因为这是不合要求的移动。
示例 3:
 
输入:[100,101,104,102,103]
输出:[0,0]

思路:

首先要理清具体操作,本质上就是把数轴上的点用最小的移动次数转为一段连续序列,由于最长移动次数可以直接求出:每次移动都从外到内把所有空隙都移动一次,可以刚开始把最左的移到最右的空隙或最右的移到最左的,这取决于哪个空隙最长,所以最长移动次数就是:总空隙-min(最左空隙,最右空隙);最短移动次数,可以用滑动窗口,一个窗口大小就是n长度的区间,刚开始递增j直到长度接近n,然后移动次数就是不在窗口里的点的个数。注意这里遇到一个连续窗口以及窗口外点只有一个时需要两步。

 1 class Solution {
 2 public:
 3     vector<int> numMovesStonesII(vector<int>& stones) {
 4         int mi=0x3f3f3f3f,ma=0,n=stones.size();
 5         sort(stones.begin(),stones.end());
 6         ma=stones[n-1]-stones[0]-min(stones[n-1]-stones[n-2],stones[1]-stones[0])-n+2;
 7         for(int i=0,j=0;i<n;i++){
 8             while(stones[i]-stones[j]+1>n)j++;
 9             if(i-j==stones[i]-stones[j]&&n-(i-j+1)==1)mi=min(mi,2);
10             else mi=min(mi,n-(i-j+1));
11         }
12         vector<int>v;
13         v.push_back(mi); v.push_back(ma);
14         return v;
15     }
16 };

原文地址:https://www.cnblogs.com/ljy08163268/p/11735512.html

时间: 2024-07-30 13:43:13

leetcode 1040. 移动石子直到连续 II(滑动窗口)的相关文章

leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)

用滑动窗口的思想来做.用一个unordered_map来查询之前的char有没有在现在的窗口中. class Solution { public: int lengthOfLongestSubstring(string s) { unordered_map<char,int>mp; int ans=0,now=0;//now is the window's instant length int b=0,e=0;//the window's begin and end int len=s.len

[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]Remove Duplicates from Sorted List II @ Python

原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4-&g

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次

LeetCode: Remove Duplicates from Sorted List II [083]

[题目] Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2-

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if

[LeetCode] Search in Rotated Sorted Array II [36]

题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 原题链接(点我) 解题思路 这题和Search in Rotated Sorted