forked from markhary/codility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTieRopes.cpp
More file actions
33 lines (26 loc) · 717 Bytes
/
TieRopes.cpp
File metadata and controls
33 lines (26 loc) · 717 Bytes
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
// https://app.codility.com/programmers/lessons/16-greedy_algorithms/tie_ropes/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected time complexity: O(N)
#include <vector>
using namespace std;
int solution(int K, vector<int> &A) {
const int N = A.size();
int ropes = 0;
int length = 0;
for (int i=0; i<N; i++) {
// Add rope to the "stack". Tie it as soon as
// it exceeds or equals K. Assume this is
// optimal. This is O(N)
length += A[i];
if ( length >= K ) {
ropes++;
// Ropes have to be adjacent, so "cut" the rope
// as soon as we tie one off
length = 0;
}
}
return ropes;
}