-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSWS.cpp
More file actions
78 lines (68 loc) · 1.51 KB
/
Copy pathSWS.cpp
File metadata and controls
78 lines (68 loc) · 1.51 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <vector>
#include <math.h>
#include <limits.h>
template <typename T>
void display(std::vector<T> &arr, std::string end = "\n")
{
for (auto &&i : arr)
{
std::cout << i << end;
}
}
// Naive Approach
// int maxSum(std::vector<int> &arr, int k)
// {
// /*
// Get the max sum of the k elements from the arr
// */
// int sum = INT_MIN;
// int n = arr.size();
// for (size_t i = 0; i <= n - k; i++)
// {
// int currentSum = 0;
// for (size_t j = 0; j < k; j++)
// {
// currentSum += arr[i + j];
// }
// sum = std::max(sum, currentSum);
// }
// return sum;
// }
// Sliding Window Approach
int maxSum(std::vector<int> &arr, int k)
{
/*
Get the max sum of the k elements from the arr
*/
int n = arr.size();
if (k > n)
{
return -1;
}
int max_sum = 0;
int window_sum = 0;
// finding the first window
for (size_t i = 0; i < k; i++)
{
window_sum += arr[i];
}
max_sum = window_sum;
for (size_t i = k; i < n; i++)
{
std::cout << i-k << std::endl;
window_sum += arr[i] - arr[i - k];
max_sum = std::max(max_sum, window_sum);
}
return max_sum;
}
int main(int argc, char const *argv[])
{
std::vector<int> arr = {5, 2, -1, 0, 3, 4, 5};
int k = 3;
std::cout << "STATIC SLIDING WINDOW\n";
display(arr, " ");
std::cout << "\n"
<< maxSum(arr, k) << "\n";
return 0;
}