-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingEleFrogRiverOne.cpp
More file actions
111 lines (93 loc) · 4.27 KB
/
CountingEleFrogRiverOne.cpp
File metadata and controls
111 lines (93 loc) · 4.27 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// CountingEleFrogRiverOne.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int finder(vector<int> &A, vector<int> &B, int pos)
{
int destPosTime = -1;
bool found_at_least_once = false;
auto start_it = A.begin();
cout << "\n";
while (start_it != A.end()) {
start_it = std::find(start_it, A.end(), pos);
if (start_it != A.end()) {
auto const timePos = std::distance(A.begin(), start_it);
std::cout << "FOUND LEAF: "<<pos<<" AT : " << timePos << '\n';
if (destPosTime == -1)
{
destPosTime = timePos;
}
++start_it;
found_at_least_once = true;
}
}
std::cout << "Least AT : " << pos << " AT : " << destPosTime << '\n';
if (found_at_least_once == true)
{
//if()
return destPosTime;
}
if (!found_at_least_once) {
//std::cout << "NOT FOUND" << '\n';
return -1;
}
}
//int solution(int X, vector<int> &A)
//{
// int crossTime = -1;
// vector<int> B;
//
// for (int pos = 1; pos < X + 1; ++pos)
// {
// crossTime = finder(A,B,pos);
// }
// return crossTime;
//}
int solution(int X, vector<int> &A)
{
int pos = 1;
vector<int> B;
for (int i = 0; i < A.size(); ++i)
{
auto it = std::find(A.begin(), A.end(), pos);
if (it != A.end())
{
auto const timePos = std::distance(A.begin(), it);
cout << "B Value: ";
B.push_back(timePos);
cout << B[i] << endl;
if (pos < X)
{
pos++;
cout << "Pos Value: " << pos << endl;
}
else
{
break;
}
}
else
{
return -1; //did not find element cant cross river
}
}
cout << "B Size: " << B.size()<<endl;
if (B.size() == X) //found all elements
{
std::sort(B.begin(), B.end(), std::greater<int>());
cout << "B Value: " << B[0] << endl;
return B[0];
}
}
int main()
{
//vector<int> A = {1,3,1,4,2,3,5,4};
vector<int> A = { 5,3,1,4,2,3,5,4,1};
int crossTime = -1;
crossTime = solution(5, A);
cout <<"\n"<< "The time for the frog to reach: " << crossTime << " secs" << endl;
return 0;
}