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
New Year Chaos
Sort by
recency
|
1909 Discussions
|
Please Login in order to post a comment
C# O(n)
why should we start checking from the end of the queue while calculating the bribes. whats is wrong if we compare the array index +1 with the element calculate the difference from the starting of the queue?
what is logically wrong with the below approach? can someone help me understand? for i in range(len(q)): if i+1 < q[1]: calculate the difference between the index and the actual element; if the difference is above two, we return too chaotic and exit loop. elif the difference is 2 then add two to a counter, else add 1
JAVA public static void minimumBribes(List list) { int totalBribes = 0; for (int i = 0; i < list.size(); i++) { int bribes = list.get(i) - (i + 1); if (bribes > 2) { System.out.println("Too chaotic"); return; } // Count total bribes for the current person for (int j = Math.max(0, list.get(i) - 2); j < i; j++) { if (list.get(j) > list.get(i)) { totalBribes++; } } } System.out.println(totalBribes); }
JAVA public static void minimumBribes(List list) { int totalBribes = 0; for (int i = 0; i < list.size(); i++) { int bribes = list.get(i) - (i + 1); if (bribes > 2) { System.out.println("Too chaotic"); return; } // Count total bribes for the current person for (int j = Math.max(0, list.get(i) - 2); j < i; j++) { if (list.get(j) > list.get(i)) { totalBribes++; } } } System.out.println(totalBribes); }