Journey of LeetCode|DAY 11
Preface
This is a new day to continue my stack and queue journey.
Learn something new and keep reviewing what I learnt before.
1. Sliding Window Maximum
LeetCode Link: 239. Sliding Window Maximum
You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
Return the max sliding window.
Analysis and Solution
Sliding Window
LeetCode C++ as followings Sliding Window
class Solution {
private:
class MyQueue { //monotonous queue (from less to large)
public:
deque<int> que; // double enden queue realize monotonous queue
void pop(int value) {
if (!que.empty() && value == que.front()) {//compare value need to be poped with value in the exit of the queue. pop former if former is equal to latter; queue should not be empty.
que.pop_front();
}
}
void push(int value) {
while (!que.empty() && value > que.back()) {//value to be pushed is larger than value in the entrance of the queue. pop back all the values in the back of the queue until value to be pushed is less than or equal to value in the entrance of the queue.
que.pop_back();
}
que.push_back(value);//repush the suitable value
}
int front() {//the max avlue is the value in the front of the queue
return que.front();
}
};
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
MyQueue que;
vector<int> result;
for (int i = 0; i < k; i++) { // traverse first K elements, import them into queue
que.push(nums[i]);
}
result.push_back(que.front()); // result records the max value of the first K elements
for (int i = k; i < nums.size(); i++) {
que.pop(nums[i - k]); // sliding window pops the front element
que.push(nums[i]); // slidingwindow pushes the value in the back
result.push_back(que.front()); // record the max value
}
return result;
}
};
2.Top K Frequent Elements
LeetCode Link: 347. Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Analysis and Solution
Priority Queue
LeetCode C++ as followings Priority Queue
class Solution {
public:
// heap
class mycomparison {//self define function
public:
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) {
// count frequency of the elements
unordered_map<int, int> map; // map<nums[i],is equal to frequency>
for (int i = 0; i < nums.size(); i++) {//traverse nums
map[nums[i]]++;
}
// sorting for frequency
// define a heap , size is k
priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;
// use the heap to scan the value of the frequency
for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {
pri_que.push(*it);
if (pri_que.size() > k) { // pop if the size of heap is larger than K; gurante the size is K
pri_que.pop();
}
}
// find the first K frequent elements; print value in reverse order
vector<int> result(k);
for (int i = k - 1; i >= 0; i--) {//
result[i] = pri_que.top().first;
pri_que.pop();
}
return result;
}
};