239. 滑动窗口最大值
题目链接
题意
给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
返回 滑动窗口中的最大值 。
示例 1:
输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置 最大值
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
示例 2:
输入:nums = [1], k = 1
输出:[1]
提示:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length
解
解1
用栈实现单调栈, 由于需要拷贝, 超时了
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct stack{
int top;
int array[100005];
};
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
struct stack *st = (struct stack *)malloc(sizeof(struct stack));
int *ans = (int *)malloc(sizeof(int) * 100005);
int idx = 0;
memset(ans, 0, sizeof(int) * 100005);
memset(st, 0, sizeof(*st));
st->top = -1;
for (int i = 0; i < k; i++) {
while (st->top != -1 && st->array[st->top] < nums[i]) {
st->top--;
}
st->array[++st->top] = nums[i];
}
ans[idx++] = st->array[0];
for (int i = k; i < numsSize; i++) {
while (st->top != -1 && st->array[st->top] < nums[i]) st->top--;
st->array[++st->top] = nums[i];
if (st->array[0] == nums[i-k]) {
int j = 0;
while (j < st->top) {
st->array[j] = st->array[j+1];
j++;
}
st->top--;
}
ans[idx++] = st->array[0];
}
*returnSize = idx;
return ans;
}
解2: 队列实现单调栈
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
const int max = 1e5+10;
struct queue{
int front, back;
int array[100005];
};
int empty(struct queue *queue) {
if (queue->front == queue->back) {
return 1;
}
return 0;
}
void push(struct queue *que, int n) {
while (!empty(que) && que->array[que->back] < n) {
que->back--;
}
que->array[++que->back] = n;
}
void pop(struct queue *que, int n) {
if (que->array[que->front+1] == n) {
que->front++;
}
}
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
struct queue *que = (struct queue *)malloc(sizeof(struct queue));
int *ans = (int *)malloc(sizeof(int) * max);
int idx = 0;
memset(que, 0, sizeof(*que));
que->front = que->back = -1;
for (int i = 0; i < k; i++) {
push(que, nums[i]);
}
ans[idx++] = que->array[que->front+1];
for (int i = k; i < numsSize; i++) {
pop(que, nums[i-k]);
push(que, nums[i]);
ans[idx++] = que->array[que->front+1];
}
*returnSize = idx;
return ans;
}
347. 前 K 个高频元素
题目链接
题意
给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。
示例 1:
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
示例 2:
输入: nums = [1], k = 1
输出: [1]
提示:
1 <= nums.length <= 105
k 的取值范围是 [1, 数组中不相同的元素的个数]
题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的
进阶:你所设计算法的时间复杂度 必须 优于 O(n log n) ,其中 n 是数组大小。
解
小顶堆
class Solution {
public:
struct cmp_pair {
bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs.second > rhs.second;
}
};
vector<int> topKFrequent(vector<int>& nums, int k) {
vector<int> ans(k);
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
map[nums[i]]++;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp_pair> pri_que;
for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {
pri_que.push(*it);
if (pri_que.size() > k) {
pri_que.pop();
}
}
for (int i = k-1; i >= 0; i--) {
ans[i] = pri_que.top().first;
pri_que.pop();
}
return ans;
}
};