239. Sliding Window Maximum

Source: Internet
Author: User

Topic itself: https://leetcode.com/problems/sliding-window-maximum/

Given an array nums, there is a sliding window of size K which are moving from the very left of the array To the very right. You can only see theK numbers in the window. Each of the sliding window moves right by one position.

For example,
Given nums = [1,3,-1,-3,5,3,6,7] , and k = 3.

Window position                Max---------------               -----[1  3  -1]-3  5  3  6  7       3 1 [3  -1  -3] 5  3  6  7       3 1  3 [-1  -3  5] 3  6  7       5 1  3  -1 [-3  5  3] 6  7       5 1  3  -1  -3 [5  3  6] 7       6 1  3  -1  -3  5 [3  6  7]      7

Therefore, return the max sliding window as [3,3,5,5,6,7] .

Note:
You may assume K are always valid, Ie:1≤k≤input array's size for non-empty array.

Follow up:
Could solve it in linear time?

Prepared before the Pocket gems, this time is actually smooth

Points:

1. Use a priorityqueue to automatically peek to the current maximum number each time

2. Save <index with a map, nums[index]> No, not in reverse, otherwise the duplicated number will be lost.

1      Public int[] Maxslidingwindow (int[] Nums,intk) {2         if(Nums.length = = 0) {3             return New int[0];4         }5Priorityqueue<integer> PQ =NewPriorityqueue<>(Collections.reverseorder ());6Map<integer, integer> map =NewHashmap<>();7         intFirstmax = Nums[0];8          for(inti = 0; I < K; i++) {9Firstmax =Math.max (Firstmax, nums[i]);Ten Pq.offer (Nums[i]); One Map.put (i, nums[i]); A         } -         intLen =nums.length; -         int[] res =New int[Len-k + 1]; theRes[0] =Firstmax; -          for(inti = k; i < Len; i++) { - Pq.offer (Nums[i]); - Map.put (i, nums[i]); +Pq.remove (Map.get (i-k)); -Map.Remove (I-k); +Res[i-k + 1] =Pq.peek (); A         } at         returnRes; -}

I think a bit of the point is that the topic emphasizes that K is valid, is greater than 0 and less than a non-empty array length, but the length of the input array may be 0, it feels very boring = =

Time complexity is O (n), I think it is O (n logk), because each time the PQ is given a number of times is the complexity of LOGK, but K supposed to is not very small (and number of team leader), so it is not very important

239. Sliding Window Maximum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.