class Solution {
public int[] maxAltitude(int[] heights, int limit) {
if(heights.length 0)
return new int[0];
int[] ans = new int[heights.length – limit + 1];
List lower = new LinkedList<>();//一个非严格递减的队列
for(int i = 0; i < limit; i++){
while(!lower.isEmpty() && lower.getLast() < heights[i] )
lower.removeLast();
lower.addLast(heights[i]);
}
ans[0] = lower.getFirst();
int begin = 0;
int end = begin + limit - 1;
while(end < heights.length -1){
end++;
if(lower.getFirst() == heights[begin])
lower.removeFirst();
while(!lower.isEmpty() && lower.getLast() < heights[end] )//每次新元素入队,就把队列中小于他的所有元素移除。保证队列的递减
lower.removeLast();
lower.addLast(heights[end]);
begin++;
ans[begin] = lower.getFirst();
}
return ans;
}
class Solution {
public int[] maxAltitude(int[] heights, int limit) {
if(heights.length == 0 || limit == 0) return new int[0];
// if (N == 0) {
// return new int[0] ;
// }
int N = heights.length - limit + 1;
int[] resArr = new int[N];
Arrays.fill(resArr, Integer.MIN_VALUE);
for (int i = 0; i < limit; i++) {
resArr[0] = Math.max(resArr[0], heights[i]);
}
for (int i = limit , j = 1; i < heights.length; i++,j++) {
for (int k = i; k > i - limit; k--) {
resArr[j] = Math.max(resArr[j], heights[k]);
}
}
return resArr;
}
}
class Solution {
public int[] maxAltitude(int[] heights, int limit) {
if(heights.length==0){
return heights;
}
int l = 0;
int r = limit-1;
int[] ret = new int[heights.length-limit+1];
for(;rmax?nums[i]:max;
}
return max;
}
}
思路:固定滑动窗口的大小,每次只往后移动一格,并且随着移动获取滑动窗口内的最大值,这个题比较烦的是最后返回结果如果直接用list.toArray()的话,返回的是一个引用对象,不能直接返回,这点我开始时候不知道有没有别的接口可以简化,所以就自己遍历了一遍数组再返回。之后发现返回数组长度是可以确定的,所以直接创建ret数组而不是用list来存储最大值。
时间复杂度:因为滑动窗口每次只需要往后一个,时间复杂度应该是O(limit(n-limit))=O(n*limit),空间复杂度O(1)
共以下 13 个回答
用单调队列实现,队尾保持小队头保持大,队头始终是该滑动窗口的最大者
时间复杂度$O(n)$,空间复杂度$O(limit)$
class Solution { maxSlidingWindow(vector & nums, int k) { ();
public:
vector
if (nums.empty() || nums.size() < k) {
return vector
}
};
打卡
时间复杂度O(n),空间复杂度O(limit)
时间复杂度:O(n)
额外空间复杂的:O(k)
class Solution { lower = new LinkedList<>();//一个非严格递减的队列
public int[] maxAltitude(int[] heights, int limit) {
if(heights.length 0)
return new int[0];
int[] ans = new int[heights.length – limit + 1];
List
}
//空间复杂度On
//时间复杂度Olimit
打卡
暴力解决了,有空再看下滑动窗口吧~~~
时间复杂度:O(n)
空间复杂度:O(n)
本来用的是暴力法,然后看了视频之后用了时间复杂度O(n),空间复杂度O(n)的方法
“`C++ maxAltitude(vector & heights, int limit) { res; queue;
class Solution {
public:
vector
if(heights.empty() == true || heights.size() < limit) { return heights; } int length = heights.size(); vector
deque
//用队列记录最大值的序号,队列头部是最大值
for(int i = 0; i < length; i++) { //窗口有更大值则弹出尾部小值 while(queue.empty() == false && heights[queue.back()] <= heights[i]) { queue.pop_back(); }
};
“`
思路:单调队列
class Solution { maxAltitude(vector & heights, int limit) { res; q; //单调队列,存储标号
public:
vector
vector
if(heights.size() < limit || heights.size() 0) return res;
deque
for(int i = 0; i < heights.size() ; i ++) {
//维护单调递减
while(!q.empty() && heights[i] > heights[q.back()]) q.pop_back();
q.push_back(i);
if(i – q.front() >= limit) q.pop_front();
//output
if(i + 1 >= limit) {
res.push_back(heights[q.front()]);
}
}
return res;
}
};
时间复杂度O(N),空间复杂度O(limit)
时间O(n),空间:O(1)