-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrog_river_one.cpp
More file actions
41 lines (33 loc) · 869 Bytes
/
Copy pathfrog_river_one.cpp
File metadata and controls
41 lines (33 loc) · 869 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
34
35
36
37
38
39
40
41
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int solution(int X, vector<int> &A)
{
// If no leaves falling or less leaves then X, can't cross the river
if (A.empty() || A.size() < X) {
return -1;
}
std::set<int> jumps; // set with all required jumps, when we find X and this set is empty, frog can cross the river
for (int i = 1; i <= X; ++i) {
jumps.insert(i);
}
for (std::size_t i = 0; i < A.size(); ++i) {
auto element = jumps.find(A[i]);
if (element != jumps.end()) {
jumps.erase(element);
if (jumps.empty()) {
return i;
}
}
}
return -1;
}
int main()
{
std::vector<int> jumps = {1, 3, 1, 4, 2, 3, 5, 4};
std::cout << solution(5, jumps);
cout << endl;
return 0;
}