Created
March 2, 2019 22:24
-
-
Save hmenn/06db4fb4b0bcf67ea5df8c8dfa70a2b7 to your computer and use it in GitHub Desktop.
hackerrank - interview kit - arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void minimumBribes(vector<int> q) { | |
| const int maxbribe=2; | |
| int step=0; | |
| bool invalid=false; | |
| int personsize = q.size() - 1; | |
| while(true){ | |
| bool bribed = false; | |
| int bribeCount = 0; | |
| for (int j = 0; j < personsize; ++j) { | |
| if (q[j] > q[j + 1]) { | |
| ++bribeCount; | |
| if(bribeCount > maxbribe){ | |
| invalid=true; | |
| break; | |
| } | |
| int temp = q[j]; | |
| q[j] = q[j + 1]; | |
| q[j + 1] = temp; | |
| bribed = true; | |
| ++step; | |
| }else if(bribed){ | |
| bribeCount=0; | |
| } | |
| } | |
| if(invalid || !bribed){ | |
| break; | |
| } | |
| } | |
| if (invalid) { | |
| std::cout << "Too chaotic"<<std::endl; | |
| }else{ | |
| std::cout <<step<<std::endl; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment