-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily18_hard_google.hpp
More file actions
60 lines (44 loc) · 1.21 KB
/
daily18_hard_google.hpp
File metadata and controls
60 lines (44 loc) · 1.21 KB
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
/*
This problem was asked by Google.
Given an array of integers and a number k, where 1 <= k <= length of the array, compute the maximum values of each subarray of length k.
For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 8, 8], since:
10 = max(10, 5, 2)
7 = max(5, 2, 7)
8 = max(2, 7, 8)
8 = max(7, 8, 7)
*/
#include <iostream>
#include <limits>
#include <vector>
void solution(int *data, int size, int k){
int max = std::numeric_limits<int>::min();
int secondMax = std::numeric_limits<int>::min();
for(int i=0; i<size;++i){
if(data[i] > max){
secondMax = max;
max = data[i];
}else if(data[i] > secondMax){
secondMax = data[i];
}
if(k==1){
max=data[i];
std::cout<<"Max:"<<max<<std::endl;
}else if(i >= k-1){
if(max == data[i-k]){
max = secondMax;
secondMax = data[i];
}
std::cout<<"Max:"<<max<<std::endl;
}
}
}
int main() {
int data1[]={10,5,2,7,8,7};
solution(data1,6,3);
std::cout<<std::endl<<"====="<<std::endl;
int data2[]={10,5,2,7,8,7};
solution(data2,6,1);
std::cout<<std::endl<<"====="<<std::endl;
int data3[]={-1,-2,-3,-4,12,5,2,7,8,7,12};
solution(data3,11,4);
}