当前位置:网站首页>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 !
边栏推荐
- Shell process control
- The origin of usb-if Association and various interfaces
- Data analysis methodology and previous experience summary [notes dry goods]
- USB-IF协会与各种接口的由来
- 起床困难综合症(按位贪心)
- Flow control statement of SQL data analysis [if, case... When detailed]
- Leetcode skimming: stack and queue 02 (realizing stack with queue)
- 九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建
- 二叉搜索树的创建,查找,添加,删除操作
- 2022拼多多详情/拼多多商品详情/拼多多sku详情
猜你喜欢
Linux centos7 installation Oracle11g super perfect novice tutorial
Linux CentOS7安装Oracle11g的超完美新手教程
Selectively inhibiting learning bias for active sampling
时间复杂度与空间复杂度
Leetcode 96 différents arbres de recherche binaires
实例讲解将Graph Explorer搬上JupyterLab
Difficult to get up syndrome (bit by bit greed)
The new version of graphic network PDF will be released soon
LDR6035智能蓝牙音响可对手机设备持续充放电方案
SQL Server Installation Guide
随机推荐
Shell custom function
Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
Node——Egg 实现上传文件接口
下载在线视频 m3u8使用教程
Which app is better and more secure for stock mobile account opening
heketi 记录
智能运维实战:银行业务流程及单笔交易追踪
B tree and b+tree of MySQL
LeetCode中等题题分享(5)
Flow control statement of SQL data analysis [if, case... When detailed]
Cmake engineering related
Selectively inhibiting learning bias for active sampling
449-原码、补码、反码
SQL Server 安裝指南
Is it safe to buy funds in a securities account? Where can I buy funds
Database -- sqlserver details
实例讲解将Graph Explorer搬上JupyterLab
Difficult to get up syndrome (bit by bit greed)
Windows installation WSL (II)
Node——添加压缩文件