-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodilityStacksFish.cpp
65 lines (52 loc) · 1.68 KB
/
CodilityStacksFish.cpp
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
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
//if the below header file does not work then use above includes
#include<bits/stdc++.h>
int solution(vector<int> &A, vector<int> &B) {
// write your code in C++14 (g++ 6.2.0)
stack <int> downStream;
int count=0;
for(int i=0;(size_t)i<A.size();++i)//auto i:A)
{
int curFish = A[i];
int curFlow = B[i];
if(curFlow ==1)//downstream
{
downStream.push(A[i]);
}
if(!downStream.empty() && curFlow ==0)//upstream
{
//std::cout<<"Stack top: "<<downStream.top()<<endl;
//std::cout<<"Cur fish: "<<curFish<<endl;
while(!downStream.empty() && curFish > downStream.top())
{
//std::cout<<"Popped element: "<<downStream.top()<<endl;
downStream.pop();//eat the fish
}
}
if(downStream.empty() && curFlow ==0)//nothing to eat
{
count++;
}
}
//std::cout<<"Stack size: "<<downStream.size()<<std::endl;
return count+downStream.size();
}
int main()
{
std::cout << "Fish Problem Codility - C++ !" << endl;
vector<int> A = {4,3,2,1,5};
vector<int> B = {0,1,0,0,0};
//vector<int> A = {1,5,8,3,2,9};//{4,3,2,1,5};
//vector<int> B = {1,0,1,1,1,0};//{0,1,0,0,0};
int fishAlive = solution(A,B);
std::cout<<"No of fish Alive: "<<fishAlive;
return 0;
}