We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I use this method and find some problems, but I've got them fixed :). Here is my C++ solution, which has passed all the test cases.
intn;cin>>n;int*p=newint[n];for(inti=0;i<n;i++)cin>>p[i];stack<int>st;int*killer=newint[n];//which killed iint*daycount=newint[n];//how long i can existint*killcount=newint[n];//how many i killedfor(inti=0;i<n;i++){daycount[i]=0;killcount[i]=0;}for(inti=0;i<n;i++){while(!st.empty()&&p[i]<=p[st.top()])st.pop();if(st.empty()){killer[i]=-1;daycount[i]=-1;}else{killer[i]=st.top();killcount[killer[i]]++;daycount[i]+=killcount[killer[i]];daycount[st.top()]--;if(killer[st.top()]>=0&&daycount[st.top()]<=0)st.pop();}st.push(i);}intmax=-1;for(inti=0;i<n;i++)if(max<killcount[i])max=killcount[i];cout<<max<<endl;return0;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Poisonous Plants
You are viewing a single comment's thread. Return to all comments →
I use this method and find some problems, but I've got them fixed :). Here is my C++ solution, which has passed all the test cases.