---
# System prepended metadata

title: 63. Unique Paths II
tags: [Leetcode]

---


###### tags: `Leetcode`

# 239. Sliding Window Maximum

## 題目
###### Link : https://leetcode.com/problems/sliding-window-maximum/

## 程式碼

```cpp=
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        const int n = nums.size();
        vector<int> ans(n - k + 1);
        int ans_i = 0, cur_i = 0;
        priority_queue<pair<int, int>> pq;
        while(cur_i < k){
            pq.push({nums[cur_i], cur_i});
            ++cur_i;
        }
        ans[ans_i++] = pq.top().first;
        for(;cur_i < n;++cur_i){
            pq.push({nums[cur_i], cur_i});
            while(pq.top().second <= cur_i - k){
                pq.pop();
            }
            ans[ans_i++] = pq.top().first;
        }
        return ans;
    }
};
```