-
Notifications
You must be signed in to change notification settings - Fork 161
/
sliding-window-maximum.md
24 lines (20 loc) · 1.11 KB
/
sliding-window-maximum.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<p>Given an array <em>nums</em>, there is a sliding window of size <em>k</em> which is moving from the very left of the array to the very right. You can only see the <em>k</em> numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong> <em>nums</em> = <code>[1,3,-1,-3,5,3,6,7]</code>, and <em>k</em> = 3
<strong>Output: </strong><code>[3,3,5,5,6,7]
<strong>Explanation:
</strong></code>
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 <strong>3</strong>
1 [3 -1 -3] 5 3 6 7 <strong>3</strong>
1 3 [-1 -3 5] 3 6 7 <strong> 5</strong>
1 3 -1 [-3 5 3] 6 7 <strong>5</strong>
1 3 -1 -3 [5 3 6] 7 <strong>6</strong>
1 3 -1 -3 5 [3 6 7] <strong>7</strong>
</pre>
<p><strong>Note: </strong><br />
You may assume <em>k</em> is always valid, 1 ≤ k ≤ input array's size for non-empty array.</p>
<p><strong>Follow up:</strong><br />
Could you solve it in linear time?</p>