New Year Chaos

Sort by

recency

|

278 Discussions

|

  • + 0 comments

    public static void minimumBribes(List q) { // Write your code here HashMap seen = new HashMap<>(); //To store the seen values

    int min=q.size()+1;//This stores the min val found so far
    int max = 0;//This stores the max val found so far
    
    int numOfBribe = 0;// Total bribes
    
    for(int i=q.size()-1;i>=0;i--){
        //We travel from end to start and we are checking the number of
        // integers less than the current number.
    
        if(q.get(i)>max) { //If current number is greater than max then all the
                           //we have seen so far bribed.
            if(seen.size()>2){ //If the person bribed more than 2   
                System.out.println("Too chaotic");
                return;
            }
            else numOfBribe+=seen.size(); //If the person not bribed more than 2
        }
        else if(q.get(i)>min){ //If the current number is between min and max.
            int count = 0;
            for(int j=min;j<max;j++){
                if(j<q.get(i)){
                    if(seen.containsKey(j))//we can count it as bribed if the number
                        count++;           //is greater than min and present in seen
                    if(count>2){ //If the person bribed more than 2   
                        System.out.println("Too chaotic");
                        return;
                    }
                }
                else{ //No more integers present in seen which are less than the 
                    break;  //current number
                }
            }
            numOfBribe+=count; //Then we add the count to total number of bribes.
        }
        seen.put(q.get(i), 1);    //Update map,min, and max
        min = Math.min(q.get(i),min);
        max = Math.max(q.get(i), max);
    }
    System.out.println(numOfBribe);
    
    }
    
  • + 0 comments

    public static void minimumBribes(List q) { int bribes = 0;

    for (int i = q.size() - 1; i >= 0; i--) {
        // Check if the person at position i has moved more than 2 positions forward
        if (q.get(i) - (i + 1) > 2) {
            System.out.println("Too chaotic");
            return;
        }
    
        // Count how many people have overtaken the person in position i
        for (int j = Math.max(0, q.get(i) - 2); j < i; j++) {
            if (q.get(j) > q.get(i)) {
                bribes++;
            }
        }
    }
    System.out.println(bribes);
    

    }

  • + 0 comments

    My code runs normally in Jupyter but gives different results on this website. Could any please help? Thanks!

    def minimumBribes(q):
        # Write your code here
        n = len(q)
        bribe_count= {j:0 for j in range(n)}
        if n == 1: 
            return 0
        for i in range(1,n):
            for j in range(i):
                if q[j]>q[i]:
                    bribe_count[j]+=1
        sum_count = 0
        for person, count in bribe_count.items():
            if count>2:
                print('Too chaotic')
                return
            sum_count += count
        return sum_count
    
  • + 0 comments

    Hi folks, anyone able to suggest why this wouldn't be working? It passes all the sample tests, and tests 0, 1, and 11, but not the others. Have tried taking the test inputs and checking a few parts of them and they seem to work, so I don't know where the issue is.

    It's not failing on time

    function minimumBribes(q) {
        // Write your code here
        let bribes = 0;
        const reference = [...q];
        reference.sort((a, b)=> {
            return a-b
        })
        for(let i=0; i<q.length; i++){
            if(q[i] > reference[i]){
                let difference  = q[i] - (reference[i])
                if(difference > 2){
                    console.log('Too chaotic')
                    return
                } else {
                    bribes += difference
                }
            } else if(q[i] < reference[i] && q[i+1] < q[i]){
                bribes ++
            } else {
                null
            }  
        }
        console.log(bribes)
    }
    
  • + 0 comments

    can someone explain what is wrong with this code-

    def minimumBribes(q):
        # Write your code here
        res=0
        for i in range(len(q)):
            if (i+1)<q[i]:
                r=q[i]-(i+1)
                if r>2:
                    print("Too chaotic")
                    return
                res+=r
        print(res)
        return