1栈和队列--生成窗口最大数值
来源:互联网 发布:udp使用的端口 编辑:程序博客网 时间:2024/04/28 23:59
输入:整型数组arr,窗口大小w输出:长度为n-w+1数组
解析: 用双向队列(两头都可以操作),从队首到队尾,存的下标代表的数值依次减小,队首代表的总是最大的。当队首的下标等于当前下标减去w时,表示队首下标已经过期,移除!每次输出队首
public static int[] getMaxWindow(int[] arr, int w) { if (arr == null || w < 1 || arr.length < w) { return null; } LinkedList<Integer> qmax = new LinkedList<Integer>(); int[] res = new int[arr.length - w + 1]; int index = 0; for (int i = 0; i < arr.length; i++) { while (!qmax.isEmpty() && arr[qmax.peekLast()] <= arr[i]) { qmax.pollLast(); } qmax.addLast(i); if (qmax.peekFirst() == i - w) { qmax.pollFirst(); } if (i >= w - 1) { res[index++] = arr[qmax.peekFirst()]; } } return res; } // for test public static void printArray(int[] arr) { for (int i = 0; i != arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } public static void main(String[] args) { int[] arr = { 4, 3, 5, 4, 3, 3, 6, 7 }; int w = 3; printArray(getMaxWindow(arr, w)); }
0 0
- 1栈和队列--生成窗口最大数值
- 栈和队列 生成窗口最大数组
- 栈和队列---生成窗口最大值数组
- 程序员面试指南之生成窗口数值最大数组
- 生成窗口最大数值(每日一道算法题)
- 栈和队列之生成窗口最大值数组
- 栈和队列(7)-- 生成窗口最大值数组
- 栈和队列——生成窗口最大值数组
- 生成窗口最大数组问题
- 生成窗口最大值数组 + 双端队列
- 栈和队列:滑动窗口和数组变树问题
- 栈和队列--求子数组的最大和
- 最大疯狂队列的和
- 栈和队列---求最大子矩阵的大小
- 滑动窗口1:最大窗口子串
- 1、栈和队列
- 1、栈和队列
- 队列和栈(1)
- Leetcode 311. Sparse Matrix Multiplication (Medium) (cpp)
- React Native 简介
- 浅谈C++中指针和引用的区别
- Android利用android:indeterminateDrawable来实现ProgressBar三种方式
- UiAutomator自定义一个一键清理后台的方法
- 1栈和队列--生成窗口最大数值
- WinPE的制作 - 进WinPE后自动运行程序
- 相似图片搜索原理介绍
- CLR|CIL|CTS|CLS|CLI|BCL|FCL
- C\C++ 内存对齐
- [C++]什么时候使用引用行参
- Hadoop对RPC的支持
- 解决在eclipse中导入项目名称已存在的问题
- 如何自己编写Makefile