Skip to content

Instantly share code, notes, and snippets.

@lundstig
Created March 13, 2019 14:26
Show Gist options
  • Save lundstig/4bbe2d4f6647889652aba22b21a1338a to your computer and use it in GitHub Desktop.
Save lundstig/4bbe2d4f6647889652aba22b21a1338a to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> h(n);
for (int i = 0; i < n; ++i) {
cin >> h[i];
}
// Calculate initial sum
int sum = 0;
for (int i = 0; i < k; ++i) {
sum += h[i];
}
int smallest = 1000000000;
int smallestPos;
for (int i = 0; i + k <= n; ++i) {
// Is the the best place we'e seen so far?
if (sum < smallest) {
smallest = sum;
smallestPos = i;
}
// Slide the window 1 plank to the right, unless we're at the end
if (i + k < n) {
sum -= h[i];
sum += h[i + k];
}
}
cout << smallestPos + 1 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment