forked from yunshouhu/InterviewQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
面试题65之滑动窗口的最大值_MaxInSlidingWindow.cpp
191 lines (142 loc) · 4.49 KB
/
面试题65之滑动窗口的最大值_MaxInSlidingWindow.cpp
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
180
181
182
183
184
185
186
187
188
189
190
/*
* Question Description:
* (Question 93 in <Coding Intervies>) Given an array of numumbers and a sliding window size,
* how do you get the maximum numbers in all sliding windows?
*/
#include <stdio.h>
#include <vector>
#include <deque>
using namespace std;
vector<int> maxInWindows(const vector<int>& num, unsigned int size)
{
vector<int> maxInWindows;
if(num.size() >= size && size >= 1)
{
deque<int> index;
for(unsigned int i = 0; i < size; ++i)
{
while(!index.empty() && num[i] >= num[index.back()])
index.pop_back();
index.push_back(i);
}
for(unsigned int i = size; i < num.size(); ++i)
{
maxInWindows.push_back(num[index.front()]);
while(!index.empty() && num[i] >= num[index.back()])
index.pop_back();
if(!index.empty() && index.front() <= (int)(i - size))
index.pop_front();
index.push_back(i);
}
maxInWindows.push_back(num[index.front()]);
}
return maxInWindows;
}
// ==================== Test Code ====================
void Test(char* testName, const vector<int>& num, unsigned int size, const vector<int>& expected)
{
if(testName != NULL)
printf("%s begins: ", testName);
vector<int> result = maxInWindows(num, size);
vector<int>::const_iterator iterResult = result.begin();
vector<int>::const_iterator iterExpected = expected.begin();
while(iterResult < result.end() && iterExpected < expected.end())
{
if(*iterResult != *iterExpected)
break;
++iterResult;
++iterExpected;
}
if(iterResult == result.end() && iterExpected == expected.end())
printf("Passed.\n");
else
printf("FAILED.\n");
}
void Test1()
{
int num[] = {2, 3, 4, 2, 6, 2, 5, 1};
vector<int> vecNumbers(num, num + sizeof(num) / sizeof(int));
int expected[] = {4, 4, 6, 6, 6, 5};
vector<int> vecExpected(expected, expected + sizeof(expected) / sizeof(int));
unsigned int size = 3;
Test("Test1", vecNumbers, size, vecExpected);
}
void Test2()
{
int num[] = {1, 3, -1, -3, 5, 3, 6, 7};
vector<int> vecNumbers(num, num + sizeof(num) / sizeof(int));
int expected[] = {3, 3, 5, 5, 6, 7};
vector<int> vecExpected(expected, expected + sizeof(expected) / sizeof(int));
unsigned int size = 3;
Test("Test2", vecNumbers, size, vecExpected);
}
// increasingly sorted
void Test3()
{
int num[] = {1, 3, 5, 7, 9, 11, 13, 15};
vector<int> vecNumbers(num, num + sizeof(num) / sizeof(int));
int expected[] = {7, 9, 11, 13, 15};
vector<int> vecExpected(expected, expected + sizeof(expected) / sizeof(int));
unsigned int size = 4;
Test("Test3", vecNumbers, size, vecExpected);
}
// decreasingly sorted
void Test4()
{
int num[] = {16, 14, 12, 10, 8, 6, 4};
vector<int> vecNumbers(num, num + sizeof(num) / sizeof(int));
int expected[] = {16, 14, 12};
vector<int> vecExpected(expected, expected + sizeof(expected) / sizeof(int));
unsigned int size = 5;
Test("Test4", vecNumbers, size, vecExpected);
}
// size of sliding windows is 1
void Test5()
{
int num[] = {10, 14, 12, 11};
vector<int> vecNumbers(num, num + sizeof(num) / sizeof(int));
int expected[] = {10, 14, 12, 11};
vector<int> vecExpected(expected, expected + sizeof(expected) / sizeof(int));
unsigned int size = 1;
Test("Test5", vecNumbers, size, vecExpected);
}
// size of sliding windows is same as the array length
void Test6()
{
int num[] = {10, 14, 12, 11};
vector<int> vecNumbers(num, num + sizeof(num) / sizeof(int));
int expected[] = {14};
vector<int> vecExpected(expected, expected + sizeof(expected) / sizeof(int));
unsigned int size = 4;
Test("Test6", vecNumbers, size, vecExpected);
}
// size of sliding windows is 0
void Test7()
{
int num[] = {10, 14, 12, 11};
vector<int> vecNumbers(num, num + sizeof(num) / sizeof(int));
vector<int> vecExpected;
unsigned int size = 0;
Test("Test7", vecNumbers, size, vecExpected);
}
// size of sliding windows is greater than the array length
void Test8()
{
int num[] = {10, 14, 12, 11};
vector<int> vecNumbers(num, num + sizeof(num) / sizeof(int));
vector<int> vecExpected;
unsigned int size = 5;
Test("Test8", vecNumbers, size, vecExpected);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
return 0;
}