当前位置:网站首页>Update structure of maximum or minimum value in the window - maximum value in the window
Update structure of maximum or minimum value in the window - maximum value in the window
2022-07-24 22:13:00 【Harrison who likes to knock code】
subject
Suppose a fixed size is W The window of , Cross in turn arr, Returns the maximum value of each slide out condition
for example ,arr= [4,3,5,4,3,3,6,7], W= 3. return :[5,5,5,4,6,7].
Double ended queue meaning : When the windows shrink in turn , The number of positions will become the maximum value in the window in turn
package com.harrison.class13;
import java.util.LinkedList;
/** * @author Harrison * @create 2022-03-25-13:07 * @motto looking for him for thousand times in the crowd , Suddenly look back , The man was in the dim light . */
public class Code01_SlidingWindowMaxArray {
public static int[] right(int[] arr, int w) {
if (arr == null || arr.length < w || w < 1) {
return null;
}
int N = arr.length;
int[] res = new int[N - w + 1];
int L = 0;
int R = w - 1;
int index = 0;
while (R < N) {
int max = arr[L];
for (int i = L + 1; i <= R; i++) {
max = Math.max(max, arr[i]);
}
res[index++] = max;
L++;
R++;
}
return res;
}
public static int[] getMaxWindow(int[] arr, int w) {
if (arr == null || arr.length < w || w < 1) {
return null;
}
int N = arr.length;
int[] res = new int[N - w + 1];
int index = 0;
// Drop the mark
LinkedList<Integer> qmax = new LinkedList<>();
for (int R = 0; R < N; R++) {
while (!qmax.isEmpty() && arr[qmax.peekLast()] <= arr[R]) {
qmax.pollLast();
}
qmax.addLast(R);
// R-w: Window expiration subscript
if (qmax.peekFirst() == R - w) {
qmax.pollFirst();
}
// Whether a normal window is formed
if (R >= w - 1) {
res[index++]=arr[qmax.peekFirst()];
}
}
return res;
}
public static int[] generateRandomArray(int maxSize, int maxValue) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * (maxValue + 1));
}
return arr;
}
// for test
public static boolean isEqual(int[] arr1, int[] arr2) {
if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
return false;
}
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int testTime = 100000;
int maxSize = 100;
int maxValue = 100;
System.out.println("test begin");
for (int i = 0; i < testTime; i++) {
int[] arr = generateRandomArray(maxSize, maxValue);
int w = (int) (Math.random() * (arr.length + 1));
int[] ans1 = getMaxWindow(arr, w);
int[] ans2 = right(arr, w);
if (!isEqual(ans1, ans2)) {
System.out.println("Oops!");
}
}
System.out.println("test finish");
}
}
边栏推荐
- Principle of an automatic nine point calibration tool (including part of the source code)
- leetcode:不可能得到的最短骰子序列【思维题 + 分组思想】
- Integrated swagger learning
- 微机原理:CPU架构详解
- [icml2022] climate change and machine learning: opportunities, challenges and considerations, 121 ppt
- 一键编译安装redis6.2.4
- 基于深度学习的多任务人脸属性分析(基于飞桨PaddlePaddle)
- Function default parameter pit avoidance Guide
- C # use SQLite
- PCL点云处理之移动最小二乘拟合实验(六十二)
猜你喜欢

Dialogue with celebrities: where are the opportunities and challenges in the second half when brands gather at the shuzang track?

由斐波那契数列引述到矩阵快速幂技巧
![[postgraduate entrance examination English vocabulary training camp] day 11 - offer, form, maintain, critical](/img/49/360222c3528ee527b4ca659b0ec669.png)
[postgraduate entrance examination English vocabulary training camp] day 11 - offer, form, maintain, critical

RISC0:Towards a Unified Compilation Framework for Zero Knowledge

Maixll dock QR code recognition
![Cell special issue | application and future prediction of AI in protein structure, precision medicine, antibody therapy [review]](/img/2e/7f3cbae33c8a994b38e3bf4f9f13cb.png)
Cell special issue | application and future prediction of AI in protein structure, precision medicine, antibody therapy [review]

VScode默认输出到调试控制台如何调整到终端以及两者中的乱码问题

IndexTree2D

CAD text styles

Everything about database, database and table is here
随机推荐
PCL点云处理之CSF布料模拟滤波(五十九)
Ranking of engineering project management software
Dialogue with celebrities: where are the opportunities and challenges in the second half when brands gather at the shuzang track?
Helm —— 强大的 Kubernetes 应用的包管理工具
单调栈结构练习——子数组最小值的累加和
CAD text styles
窗口内最大值或最小值的更新结构——窗口内最大值
Circom 2.0: A Scalable Circuit Compiler
Web3安全 Go+Security
In the eyes of professionals in the robot industry, what is the current situation of the robot industry?
通讯异常判断之通讯心跳信号应用编程
【ICML2022】气候变化与机器学习:机遇、挑战与考虑,121页ppt
Feeding Program Source Code to ZK VMs
从暴力递归到动态规划,记忆化搜索
Shallow copy deep copy
PCL点云处理之pcd文件转txt文件(单个或多个批量转换)(六十三)
Leetcode: the shortest dice sequence impossible to get [thinking questions + grouping ideas]
一种兼容、更小、易用的WEB字体API
C # review the entrustment and event
运动控制卡应用开发教程之调用激光振镜控制