Skip to content

Instantly share code, notes, and snippets.

@JonathanLalou
Created June 25, 2021 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanLalou/edaf294a3b905e580b7bf6565fcc5d8c to your computer and use it in GitHub Desktop.
Save JonathanLalou/edaf294a3b905e580b7bf6565fcc5d8c to your computer and use it in GitHub Desktop.
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'minimumBribes' function below.
*
* The function accepts INTEGER_ARRAY q as parameter.
*/
public static void minimumBribes(List<Integer> target) {
var counter = 0;
final int size = target.size();
for (int i = 0; i < size; i++) {
final Integer item = target.get(i);
if (item - (i + 1) > 2) {
System.out.println("Too chaotic");
return;
}
for (int j = Math.max(0, item - 2); j<i; j++){
if(target.get(j) > item){
counter++;
}
}
}
System.out.println(counter);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t).forEach(tItr -> {
try {
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> q = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
Result.minimumBribes(q);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
}
}
@JonathanLalou
Copy link
Author

many many many hours!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment