• + 0 comments

    This Java 8 solution seems to work but fails two test cases on timed out, clearly I should find a solution with no nested loops

        public static void minimumBribes(List<Integer> q) {
        // Write your code here
            int bribes = 0;
                for(int i=0; i< q.size();i++){
                    if(q.get(i) > i+1){
                        int bribes_temp = q.get(i) - (i+1);
                        if (bribes_temp>2){
                            System.out.println("Too chaotic");
                            return;
                        }
                        bribes += bribes_temp;
                    }
                    else {
                        int j = i;
                        int bribes_temp = 0;
                        while(++j<q.size()){
                            if(q.get(i)>q.get(j)){
                                if(++bribes_temp>2){
                                    System.out.println("Too chaotic");
                                    return;
                                }
                            }                
                        }
                        bribes += bribes_temp;
                    }
                
                }
                System.out.println(bribes);
                return;
        }