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.
- New Year Chaos
- Discussions
New Year Chaos
New Year Chaos
Sort by
recency
|
62 Discussions
|
Please Login in order to post a comment
C++
}
Simple approach in php:
The simple solution to this problem is to take each element of the array, and check how many element to the right of that element has a smaller ID. But, this will be too slow to get a 100%.
To solve this problem in a linear fashion, I instead calculate the offset the current element has to its starting position. I also work backwards through the array, and store the smallest value I've found so far. This leaves me with 4 cases for each element: 1. The offset is more than 2, so I print "Too chaotic" as more than 2 bribes have been used, and call return. 2. If the current element is smaller than the smallests value, I set it as the new smallest value. 3. If nothing else I add the offset to the number of bribes. 4. Finally the most tricky one, if an element has already been skipped by 1 or 2 elements, and that has left one element between those elements and the current element, and then the current element bribes the element in front of it, you get an offset of either 0 or -1, but its element is larger than the smallest value so far, so it should count as 1 bribe. It can't be 2 because then it would revert the bribe of one of the elements that bribed it initially, and the problem is to count the minimum number of bribes need to construct the current situation. Example of this is the number 6 in:
Here's my solution (C++):
public static void minimumBribes(List q) { // Write your code here LinkedList maineQ = new LinkedList<>(q);
}