-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path239.sliding-window-maximum-1.go
179 lines (161 loc) · 5.5 KB
/
239.sliding-window-maximum-1.go
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* @lc app=leetcode id=239 lang=golang
*
* [239] Sliding Window Maximum
*
* https://leetcode.com/problems/sliding-window-maximum/description/
*
* algorithms
* Hard (38.02%)
* Likes: 1652
* Dislikes: 95
* Total Accepted: 157.5K
* Total Submissions: 413.2K
* Testcase Example: '[1,3,-1,-3,5,3,6,7]\n3'
*
* Given an array 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.
*
* Example:
*
*
* Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
* Output: [3,3,5,5,6,7]
* Explanation:
*
* Window position Max
* --------------- -----
* [1 3 -1] -3 5 3 6 7 3
* 1 [3 -1 -3] 5 3 6 7 3
* 1 3 [-1 -3 5] 3 6 7 5
* 1 3 -1 [-3 5 3] 6 7 5
* 1 3 -1 -3 [5 3 6] 7 6
* 1 3 -1 -3 5 [3 6 7] 7
*
*
* Note:
* You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty
* array.
*
* Follow up:
* Could you solve it in linear time?
*/
type Deque []int
func NewDeque() *Deque {
return &Deque{}
}
func (d Deque) Empty() bool {
if len(d) == 0 {
return true
}
return false
}
func (d Deque) Size() int {
return len(d)
}
func (d Deque) Front() int {
if d.Empty() {
return 0
}
return d[0]
}
func (d *Deque) PushFront(x int) {
if d.Empty() {
return
}
*d = append([]int{x}, *d...)
}
func (d *Deque) PopFront() {
if d.Empty() {
return
}
*d = (*d)[1:]
}
func (d Deque) Back() int {
if d.Empty() {
return 0
}
return d[d.Size()-1]
}
func (d *Deque) PushBack(x int) {
*d = append((*d), x)
}
func (d *Deque) PopBack() {
*d = (*d)[:d.Size()-1]
}
// To find out the maximum number in sliding window, we can use 'queue' to keep
// the 'index' of numbers.
// (The reason why we have to keep the 'index' of numbers instead of the
// 'value' of numbers is because there might be duplicate numbers in array.
// If we just keep the values of numbers, we are not able to determine whether
// the number at the queue's front is the slide out number or not.)
//
// We have to make sure the maximum number's index in sliding window is always at
// the front of queue. In order to ensure this:
// * When inserting a new number's index:
// => We have to make sure there're no numbers which is smaller than
// the inserted number. If there're any numbers which are smaller than
// the inserted number, since they won't be the maximum values in
// sliding window, pop all of them from queue's back.
// * When the maximum number's index (i.e. Queue's front) is going to slide out
// from sliding window:
// => We have to remove it from queue, too.
//
// ex: nums: [2, 3, 4, 2, 6, 2, 5, 1], sliding window size: 3
// (Note: Queue here shows the value of number instead of index.)
//
// *
// +-------+-----------------+----------------+---------+---------------+
// | Steps | Inserted nubmer | Sliding window | Queue | Maximum value |
// +-------+-----------------+----------------+---------+---------------+
// | 1 | 2 | [2] | [2] | N/A |
// | 2 | 3 | [2, 3] | [3] | N/A |
// | 3 | 4 | [2, 3, 4] | [4] | 4 |
// | 4 | 2 | [3, 4, 2] | [4, 2] | 4 |
// | 5 | 6 | [4, 2, 6] | [6] | 6 |
// | 6 | 2 | [2, 6, 2] | [6, 2] | 6 |
// | 7 | 5 | [6, 2, 5] | [6, 5] | 6 |
// | 8 | 1 | [2, 5, 1] | [5, 1] | 5 |
// +-------+-----------------+----------------+---------+---------------+
//
// Time Complexity: O(n)
// Space Complexity: O(k)
func maxSlidingWindow(nums []int, k int) []int {
maxWindows := []int{}
if len(nums) >= k && k >= 1 {
// Deque which keeps the 'index' of numbers.
deque := NewDeque()
// Fill sliding window first.
// The maximum number's index in the sliding window will
// at the front of queue.
for i := 0; i < k; i++ {
// Pop all numbers from queue's back which are smaller than
// the inserted number.
for !deque.Empty() && nums[i] > nums[deque.Back()] {
deque.PopBack()
}
// Push the inserted number to queue's back.
deque.PushBack(i)
}
for i := k; i < len(nums); i++ {
maxWindows = append(maxWindows, nums[deque.Front()])
// Pop all numbers from queue's back which are smaller than
// the inserted number.
for !deque.Empty() && nums[i] >= nums[deque.Back()] {
deque.PopBack()
}
// If the maximum number (i.e. Queue's front) is going to slide out
// from sliding window, remove it from queue.
if !deque.Empty() && deque.Front() <= i - k {
deque.PopFront()
}
// Push the inserted number to queue's back.
deque.PushBack(i)
}
// Add the last maximum number to answer.
maxWindows = append(maxWindows, nums[deque.Front()])
}
return maxWindows
}