FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DataStructure/239. Sliding Window Maximum.cpp at main · Kunalgithub24/DataStructure · GitHub
Kunalgithub24
/
DataStructure
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
DataStructure
/
239. Sliding Window Maximum.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
25 lines (21 loc) · 679 Bytes
Breadcrumbs
DataStructure
/
239. Sliding Window Maximum.cpp
Copy path
File metadata and controls
25 lines (21 loc) · 679 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class
Solution
{
public:
vector<
int
>
maxSlidingWindow
(vector<
int
>& nums,
int
k) {
int
n = nums.
size
();
vector<
int
> ans;
deque<
int
> dq;
for
(
int
i =
0
; i < n; i++) {
//
Remove indices outside the window
if
(!dq.
empty
() && dq.
front
() <= i - k)
dq.
pop_front
();
//
Maintain decreasing order in deque
while
(!dq.
empty
() && nums[dq.
back
()] <= nums[i])
dq.
pop_back
();
dq.
push_back
(i);
//
Add maximum for the current window
if
(i >= k -
1
)
ans.
push_back
(nums[dq.
front
()]);
}
return
ans;
}
};
Back
|
FazBrowse Home
|
New Git URL