239滑动窗口最大值
返回滑动窗口中的最大值
暴力破解加优化:遍历滑动窗口查找最大值,标记该最大值的下标。
窗口移动:判断该最大值是否移出窗口,如果移出则重新遍历查找。如果未移出,则比较最大值和新加入的值即可。
结果勉强通过,优化后刚好没有超出时间限制
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int[] result = new int[nums.length-k+1];
int n = -1;
for (int i = 0; i <= nums.length-k; i++) {
if (n<i){
int temp = -10000;
for (int j = 0; j < k; j++) {
if (temp<=nums[i+j]){
n = i+j;
temp = nums[i+j];
}
}
result[i] = temp;
}else {
if (nums[i+k-1]>=result[i-1]){
n = i+k-1;
result[i] = nums[i+k-1];
}else {
result[i] = result[i-1];
}
}
}
return result;
}
}
看了题解,使用队列解决!
队列需要一直维护成单调队列,即队顶元素是最大的,队尾元素是最小的。
使用队列需要解决两个问题:单调维护,滑动窗口范围
维护方法:放入新元素时,跟队尾元素比较,如果新元素比较大,就弹出队尾元素,直到队列为空或队尾元素比新元素大。
滑动窗口范围:在移动时,如果队首元素=窗口移出的这个元素,则弹出队顶元素。就可以保证,队列中的元素都属于窗口中。
结合起来就是,滑动窗口移动循环中,先判断滑动窗口范围,再维护单调队列,最后得到的队顶元素就是滑动窗口中的最大值。
需要弹出队顶和队尾元素,因此需要使用双向队列:Deque
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
Deque<Integer> queue = new LinkedList<>();
int[] result = new int[nums.length-k+1];
for (int i = 0; i < k; i++) {
while (!queue.isEmpty()&&queue.peekLast()<nums[i]){
queue.pollLast();
}
queue.add(nums[i]);
}
result[0] = queue.peek();
for (int i = k; i < nums.length; i++) {
if (queue.peek()==nums[i-k]){
queue.pop();
}
while (!queue.isEmpty()&&queue.peekLast()<nums[i]){
queue.pollLast();
}
queue.add(nums[i]);
result[i-k+1] = queue.peek();
}
return result;
}
}
347前 K 个高频元素
返回其中出现频率前 k
高的元素
我的题解:
使用容量为k的数组存储结果集合,同时需要容量为k的数组存储频率,两个数组一一对应,按照频率大小排序。
先将数组排序。遍历数组,每出现一个新元素则从后往前查找插入,遍历结束后数组存储结果集合即为最终结果。
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Arrays.sort(nums);
if (nums.length<=1){
return new int[]{nums[0]};
}
int[] result = new int[k];
int[] count = new int[k];
int top = -1;
int n = 1;
for (int i = 1; i <= nums.length; i++) {
if (i == nums.length ||nums[i]!=nums[i-1]){
int j;
for (j = top; j >= 0; j--) {
if (count[j]<n){
if (j+1<k){
count[j+1] = count[j];
result[j+1] = result[j];
}
}else {
break;
}
}
if (j+1<k){
count[j+1] = n;
result[j+1] = nums[i-1];
if (top<k-1){
top++;
}
}
n = 1;
}else {
n++;
}
}
return result;
}
}
看了题解:使用优先级队列实现
// lambda 表达式设置优先级队列从大到小存储 o1 - o2 为从小到大,o2 - o1 反之
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]);
其实原理基本相同,使用Map存储值和频率,再遍历Map集合,存入优先级队列。
如果大小超过k则弹出队头元素-频率最低元素。这就是小根堆算法,相反则是大根堆算法
class Solution {
public int[] topKFrequent(int[] nums, int k) {
// 优先级队列,为了避免复杂 api 操作,pq 存储数组
// lambda 表达式设置优先级队列从大到小存储 o1 - o2 为从小到大,o2 - o1 反之
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]);
int[] res = new int[k]; // 答案数组为 k 个元素
Map<Integer, Integer> map = new HashMap<>(); // 记录元素出现次数
for(int num : nums) map.put(num, map.getOrDefault(num, 0) + 1);
for(var x : map.entrySet()) { // entrySet 获取 k-v Set 集合
// 将 kv 转化成数组
int[] tmp = new int[2];
tmp[0] = x.getKey();
tmp[1] = x.getValue();
pq.offer(tmp);
// 下面的代码是根据小根堆实现的,我只保留优先队列的最后的k个,只要超出了k我就将最小的弹出,剩余的k个就是答案
if(pq.size() > k) {
pq.poll();
}
}
for(int i = 0; i < k; i ++) {
res[i] = pq.poll()[0]; // 获取优先队列里的元素
}
return res;
}
}
收获
使用队列来解决滑动窗口问题很有效
使用队列来解决固定容量解的问题
如果一端弹出则可以使用数组来代替队列,还可以使用优先级队列。
双端弹出则使用双向队列-Deque