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.
There is a pretty simple solution using only a single for-loop.
// Complete the minimumBribes function below.voidminimumBribes(vector<int>q){inttotalBribes=0;intexpectedFirst=1;intexpectedSecond=2;intexpectedThird=3;for(unsignedinti=0;i<q.size();++i){if(q[i]==expectedFirst){expectedFirst=expectedSecond;expectedSecond=expectedThird;++expectedThird;}elseif(q[i]==expectedSecond){++totalBribes;expectedSecond=expectedThird;++expectedThird;}elseif(q[i]==expectedThird){totalBribes+=2;++expectedThird;}else{cout<<"Too chaotic"<<endl;return;}}cout<<totalBribes<<endl;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
There is a pretty simple solution using only a single for-loop.