Skip to content

Instantly share code, notes, and snippets.

@psaitu
Created August 5, 2019 16:37
Show Gist options
  • Select an option

  • Save psaitu/ac004651071ab09d62c305342f95eaa3 to your computer and use it in GitHub Desktop.

Select an option

Save psaitu/ac004651071ab09d62c305342f95eaa3 to your computer and use it in GitHub Desktop.
public class Main {
// https://stackoverflow.com/questions/53543089/new-year-chaos-hackerrank-code-optimization
public static void minBribes(int[] q) {
int bribe = 0;
boolean chaotic = false;
int n = q.length;
for(int i = 0; i < n; i++) {
if(q[i] - (i + 1) > 2) {
chaotic = true;
break;
}
for(int j = Math.max(0, q[i] - 2); j < i; j ++) {
System.out.println(j);
if(q[j] > q[i]) {
bribe++;
}
}
}
if(chaotic) {
System.out.println("Too chaotic");
} else {
System.out.println(bribe);
}
}
public static void main(String[] args) {
/**
* Maximum Swaps Problem
* input = [3, 2, 1, 6, 5, 4]
* expected = [1, 2, 3, 4, 5, 6]
*
* 3
* expected = 3
* seen = 1
* bribes = 2
*
* expected = [3, 1, 2, 4, 5, 6]
*
* 2
* expected = 3
* seen = 1
* bribes = 1
*
* expected = [3, 2, 1, 4, 5, 6]
* 1
* expected 3
* seen 3
* bribes = 0
*
* expected = [3, 2, 1, 4, 5, 6]
*
* 6
* expected 6
* seen 4
* bribes = 2
*
* expected = [3, 2, 1, 6, 4, 5]
*
* 5
* expected 5
*
*
*
*/
// int[] queue = new int[]{2, 5, 1, 3, 4};
int[] queue = new int[]{2, 1, 5, 3, 4};
minBribes(queue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment