鱼C论坛

 找回密码
 立即注册
查看: 2259|回复: 0

[学习笔记] leetcode 239. Sliding Window Maximum

[复制链接]
发表于 2019-9-30 08:41:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. 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 window.

  2. Example:

  3. Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
  4. Output: [3,3,5,5,6,7]
  5. Explanation:

  6. Window position                Max
  7. ---------------               -----
  8. [1  3  -1] -3  5  3  6  7       3
  9. 1 [3  -1  -3] 5  3  6  7       3
  10. 1  3 [-1  -3  5] 3  6  7       5
  11. 1  3  -1 [-3  5  3] 6  7       5
  12. 1  3  -1  -3 [5  3  6] 7       6
  13. 1  3  -1  -3  5 [3  6  7]      7
  14. Note:
  15. You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty array.

  16. Follow up:
  17. Could you solve it in linear time?
复制代码

  1. class Solution {
  2.     public int[] maxSlidingWindow(int[] nums, int k) {
  3.         int i = 0, j;
  4.         int n = nums.length;
  5.         List <Integer> list = new ArrayList<>();
  6.         List <Integer> max = new ArrayList<>();
  7.         for(j = 0; j < n ; j++){
  8.             max.add(nums[j]);
  9.             
  10.             if(j - i + 1>= k){
  11.                 list.add(Collections.max(max));
  12.                 max.remove(i);
  13.             }
  14.         }
  15.         
  16.         int[] ret = list.stream().mapToInt(x ->x).toArray();
  17.         return ret;
  18.     }
  19. }
复制代码

  1. class Solution {
  2.     public int[] maxSlidingWindow(int[] nums, int k) {
  3.         if(nums.length == 0 || k == 0) return nums;
  4.         int j = 0, n = nums.length;
  5.         int[] arr = new int[n-k+1];
  6.         for(int i = 0; i< n ; i++){
  7.             int max = Integer.MIN_VALUE;
  8.             if(i - j +1 >= k){
  9.                 for(int m = j; m <= i; m++){
  10.                     max = Math.max(max, nums[m]);
  11.                 }
  12.                 arr[i-k+1] = max;
  13.                 j++;
  14.                
  15.             }
  16.         }
  17.         
  18.         return arr;
  19.     }
  20. }
复制代码


monotonic queue!

  1. class Solution {
  2.     public int[] maxSlidingWindow(int[] nums, int k) {
  3.         if(nums.length == 0) return nums;
  4.         int n = nums.length;
  5.         int[] res = new int[n - k +1];
  6.         ArrayDeque<Integer> index = new ArrayDeque<>();
  7.         for(int i =0; i< n; i++){
  8.             while(!index.isEmpty() && nums[i] > nums[index.peekLast()]){
  9.                 index.pollLast();
  10.             }
  11.             index.offerLast(i);
  12.             if(i - k + 1 >= 0){
  13.                 res[i - k+1] = nums[index.peekFirst()];
  14.             }
  15.             if(i - k + 1>= index.peekFirst()) index.pop();
  16.         }
  17.         
  18.         return res;
  19.     }
  20. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 21:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表