Rain water harvesting problem
it shows test case 1 and 3 give wrong ans, but when i download them and test locally they give the EXACT correct answer.
CODE GOES HERE:
#include<bits/stdc++.h>
using namespace std;
void coutvector(vector<vector> v)
{
cout<<endl;
for(int i=0;i<v.size();i++)
{
for(int j=0;j<v[0].size();j++)
{
cout<<v[i][j]<<" ";
}cout<<endl;
}
cout<<endl;
}
vector<vector> gen_filled_vector(vector<vector> q,vector v)
{
int n=v.size();
int max=q.size();
for( int j=0;j<n;j++)
{
for(int i=q.size()-1;i>=(max-v[j]);i–)
{
q[i][j]=1;
}
}
return q;
}
void clear_stack(stack &st)
{
while(!st.empty())
{
st.pop();
}
}
int stored_water(vector<vector> v)
{
int n=v[0].size(),max=v.size();
stack st;
int count=0,sum=0;
int flag_starting_1=0;
for(int i=max-1;i>=0;i–)
{
clear_stack(st);
count=0;
flag_starting_1=0;
for(int j=0;j<n;j++)
{
if(v[i][j]==1 && st.empty())
{
st.push(1);flag_starting_1=1;
}
else if(v[i][j]==0 && st.empty())
{
st.push(0);
}
if(v[i][j]==1 && st.top()==1 && !st.empty())
{
st.push(1);
flag_starting_1=1;
}
else if(v[i][j]==1 && st.top()==0 && !st.empty() && flag_starting_1==0)
{
flag_starting_1=1;
st.push(1);
}
else if(v[i][j]==1 && st.top()==0 && !st.empty() && flag_starting_1==1)
{
sum+=count;
st.push(1);
count=0;
}
else if(v[i][j]==0 && st.top()==1 && !st.empty() && flag_starting_1==1)
{
count++;
st.push(0);
}
else if(v[i][j]==0 && st.top()==0 && !st.empty() && flag_starting_1==1)
{
count++;
st.push(0);
}
}
}
return sum;
}
int main()
{
int n=0;
cin>>n;
vector v(n,0);
for(int i=0;i<n;i++)
{
cin>>v[i];
}
int max=INT_MIN;
for(int i=0;i<n;i++)
{
if(v[i]>max)
max=v[i];
}
vector<vector> q(max,vector (n,0));
vector<vector> c=gen_filled_vector(q,v);
int e=stored_water©;
cout<<e<<endl;
return 0;
}