题解 | #滑动窗口的最大值#
滑动窗口的最大值
http://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
class Solution {
public:
priority_queue<pair<int, int>, vector<pair<int, int>>, less<>> qu;
vector<int> maxInWindows(const vector<int>& num, unsigned int size) {
vector<int> ans;
if(size == 0 || size > num.size())
return ans;
for (int i = 0; i < size; ++i)
qu.push(pair<int, int>(num[i], i));
ans.push_back(qu.top().first);
for (int i = 1, j = size; i <= num.size() - size && j < num.size(); j++, i++) {
qu.push(pair<int, int>(num[j], j));
while (!qu.empty()) {
pair<int, int> cur = qu.top();
if (cur.first >= num[j] && i <= cur.second && cur.second <= j) {
ans.push_back(cur.first);
break;
}
else if(!(i <= cur.second && cur.second <= j))
qu.pop();
}
}
return ans;
}
};