Leetcode每日一题 20230816

题目链接
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.

Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]

Method 1

每个window分别找最大
Time complexity: n - k + 1 windows, take O(k) to find the largest element from each window

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int loop = nums.length - k;
        int[] result = new int[loop+1];
        int windowSize;
        int index = 0;

        for (int i = 0; i <= loop; i++){
            windowSize = k;
            int min = Integer.MIN_VALUE;
            while (windowSize != 0){
                min = Math.max(nums[index], min);
                index++;
                windowSize--;
            }
            index = i + 1;
            result[i] = min;

        }
        return result;
    }
}

Method 2

回忆deque Method

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Summary of Deque methods
First Element (Head) Last Element (Tail)
Throws exceptionSpecial valueThrows exceptionSpecial value
InsertaddFirst(e)offerFirst(e)addLast(e)offerLast(e)
RemoveremoveFirst()pollFirst()removeLast()pollLast()
ExaminegetFirst()peekFirst()getLast()peekLast()
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Comparison of Queue and Deque methods
{@code Queue} Method Equivalent {@code Deque} Method
{@link #add(Object) add(e)}{@link #addLast(Object) addLast(e)}
{@link #offer(Object) offer(e)}{@link #offerLast(Object) offerLast(e)}
{@link #remove() remove()}{@link #removeFirst() removeFirst()}
{@link #poll() poll()}{@link #pollFirst() pollFirst()}
{@link #element() element()}{@link #getFirst() getFirst()}
{@link #peek() peek()}{@link #peekFirst() peekFirst()}
* * * * * * * * * * * * * * * * * * * * * *
Comparison of Stack and Deque methods
Stack Method Equivalent {@code Deque} Method
{@link #push(Object) push(e)}{@link #addFirst(Object) addFirst(e)}
{@link #pop() pop()}{@link #removeFirst() removeFirst()}
{@link #peek() peek()}{@link #getFirst() getFirst()}
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        Deque<Integer> dq = new ArrayDeque<>();
        List<Integer> list = new ArrayList<>();

        for (int i = 0; i < k; i++){
            while(!dq.isEmpty() && nums[i] >= nums[dq.peekLast()]){
                dq.pollLast();
            }
            dq.offerLast(i);
        }
        list.add(nums[dq.peekFirst()]);
        for (int i = k; i < nums.length; i++){
            if(dq.peekFirst() == i-k){
                dq.pollFirst();
            }
            while (!dq.isEmpty() && nums[i] >= nums[dq.peekLast()]) {
                dq.pollLast();
            }
            dq.offerLast(i);
            list.add(nums[dq.peekFirst()]);
        }
        return list.stream().mapToInt(i->i).toArray();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值