当前位置:网站首页>Leetcode question brushing: stack and queue 07 (maximum value of sliding window)
Leetcode question brushing: stack and queue 07 (maximum value of sliding window)
2022-07-02 00:27:00 【Taotao can't learn English】
239. Maximum sliding window
Given an array nums, There is a size of k The sliding window of the array moves from the leftmost side to the rightmost side of the array . You can only see... In the sliding window k A digital . The sliding window moves only one bit to the right at a time .
Returns the maximum value in the sliding window .
Advanced :
Can you solve this problem in linear time complexity ?

Tips :
- 1 <= nums.length <= 10^5
- -10^4 <= nums[i] <= 10^4
- 1 <= k <= nums.length
All the way through the stumbling .
I got this question originally , It seems not difficult , It's still difficult ???
Write the code in a few lines
public int[] maxSlidingWindow(int[] nums, int k) {
/** * Input :nums = [1,3,-1,-3,5,3,6,7], k = 3 * Output :[3,3,5,5,6,7] * common 8 Number , Slide in groups of three , It can be made up of 8-3+1 Group , namely 6 Group */
if (nums.length < 2) {
return nums;
}
int[] result = new int[nums.length - k + 1];
for (int i = 0; i < nums.length - k + 1; i++) {
int max = -10000;
for (int j = i; j < i + k; j++) {
if (nums[j] > max) {
max = nums[j];
}
}
result[i] = max;
}
return result;
}

I can only say : What a long test case ...
Then improve your thinking , Change violent cracking to sliding . Divide a window into the following parts .
left Is the current maximum expiration
right Is the new value
mid Is the last maximum , Is the maximum of that old interval , As shown in the figure : At present mid=3
Every time the window moves back , Let's see if the previous maximum can still be used ( Whether the maximum value is equal to left The value of is the same , The same cannot be used , You have to traverse the window again to find the maximum value ); If you can use it, continue to use it .
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length < 2) {
return nums;
}
int[] result = new int[nums.length - k + 1];
/** * If the maximum value of the current range element is still the maximum value of the next range , Continue to use . * Here we need to judge whether the current position has passed */
int maxValue = -10000;
for (int j = 0; j < k; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
result[0] = maxValue;
// front k The two have been compared
int resIndex = 1;
// Abstract it out , Left boundary value , The maximum value of the middle part , Right boundary value
for (int i = k; i < nums.length; i++) {
// Just expired left boundary
int left = nums[i - k];
int mid = maxValue;
int right = nums[i];
if (left>mid-1) {
// If expired , You have to find the maximum again
maxValue = -10000;
for (int j = i - k + 1; j < i + 1; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
}
// If the new element is larger than the original
if (right >= mid) {
maxValue = right;
}
result[resIndex] = maxValue;
resIndex++;
}
return result;
}

emmmm There's progress , This pass 49 individual , There are only two left . After pondering hard , Finally let me see something different .
So many kinds . In order to improve efficiency , In addition to proving that the maximum value is and left The value of is the same , The update proves that the maximum value is just eliminated left The maximum of . So I changed a line of code . Passed .
public static int[] maxSlidingWindow2(int[] nums, int k) {
/** * Input :nums = [1,3,-1,-3,5,3,6,7], k = 3 * Output :[3,3,5,5,6,7] * common 8 Number , Slide in groups of three , It can be made up of 8-3+1 Group , namely 6 Group */
if (nums.length < 2) {
return nums;
}
int[] result = new int[nums.length - k + 1];
/** * If the maximum value of the current range element is still the maximum value of the next range , Continue to use . * Here we need to judge whether the current position has passed */
int maxValue = -10000;
for (int j = 0; j < k; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
result[0] = maxValue;
// front k The two have been compared
int resIndex = 1;
// Abstract it out , Left boundary value , The maximum value of the middle part , Right boundary value
for (int i = k; i < nums.length; i++) {
// Just expired left boundary
int left = nums[i - k];
int mid = maxValue;
int right = nums[i];
// Partial optimization , Prevent consecutive repeated numbers
// If left That's the maximum
if (left > mid - 1 && left != nums[i - k + 1]) {
// If expired , You have to find the maximum again
maxValue = -10000;
for (int j = i - k + 1; j < i + 1; j++) {
if (nums[j] > maxValue) {
maxValue = nums[j];
}
}
}
// If the new element is larger than the original
if (right >= mid) {
maxValue = right;
}
result[resIndex] = maxValue;
resIndex++;
}
return result;
}

perfect AC !
边栏推荐
- Use es to realize epidemic map or take out order function (including code and data)
- PWN attack and defense world cgpwn2
- Jielizhi, production line assembly link [chapter]
- ThreadLocal内存泄漏是什么,怎么解决
- What does open loop and closed loop mean?
- 毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
- Learn online case practice
- 2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
- How to improve data quality
- Review data desensitization system
猜你喜欢

Export default the exported object cannot be deconstructed, and module Differences between exports

EMC circuit protection device for surge and impulse current protection

Database -- sqlserver details

2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details

九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建

Practical calculation of the whole process of operational amplifier hysteresis comparator

How to improve data quality

Selectively inhibiting learning bias for active sampling

2023款雷克萨斯ES产品公布,这回进步很有感

BPR (Bayesian personalized sorting)
随机推荐
【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
Jielizhi, production line assembly link [chapter]
【QT】对于Qt MSVC 2017无法编译的问题解决
启牛商学院给的证券账户安不安全?哪里可以开户
启牛学院开户安全的吗?开户怎么开?
Jielizhi, production line assembly link [chapter]
Is it safe to choose mobile phone for stock trading account opening in Beijing?
Which securities company is safer to open a stock account
SQL数据分析之流程控制语句【if,case...when详解】
Qt5.12.9 migration tutorial based on Quanzhi H3
Kuberntes cloud native combat high availability deployment architecture
Intelligent operation and maintenance practice: banking business process and single transaction tracking
leetcode96不同的二叉搜索樹
cookie、session、tooken
SQL Server 安裝指南
Record the accidental success and failure of uploading large files
mysql之B tree 以及 B+tree
Jielizhi Bluetooth headset quality control and production skills [chapter]
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电



