On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:
"G": go straight 1 unit;
"L": turn 90 degrees to the left; "R": turn 90 degrees to the right.
The robot performs the instructions given in order, and repeats them forever.
Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.
Example 1:
Input: instructions = “GGLLGG” Output: true Explanation: The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0). When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.
Example 2:
Input: instructions = “GG” Output: false Explanation: The robot moves north indefinitely.
You are given an array of integers 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.
注意程序顺序,先递减栈模板,在计算结果,最后移出越界元素。如例子[876], i = 2, 窗口大小满足了,计算结果为8,8在下一轮会越界,所以移除。
结果数小于数组大小,注意line 7的条件
Python代码:
1 2 3 4 5 6 7 8 9 10 11
defmaxSlidingWindow(self, nums: List[int], k: int) -> List[int]: queue, res = deque(), [] for i inrange(len(nums)): while queue and nums[i] > nums[queue[-1]]: queue.pop() queue.append(i) if i >= k - 1: res.append(nums[queue[0]])#attn queue[0] not -1 if i - queue[0] + 1 >= k: #attn queue[0] not -1 queue.popleft() return res