New Year Chaos

Sort by

recency

|

295 Discussions

|

  • + 0 comments

    I get the correct answers except for the 100,000 inputs. It keeps timing out.

    Programming in Python 3...

    Anybody got it?

  • + 0 comments

    Passes all samples , but fails everything else. Any tips fellas ?

    function minimumBribes(q) {
        let bribes = 0
        for(let i = 0; i < q.length; i++){
            if((q[i]-1) != i && (q[i]-1) > i || q[i] > q[i + 1]){
                if(Math.abs((q[i]-1) - i) > 2){
                    console.log("Too chaotic")
                    bribes = 0
                    break;
                } else {
                    if(q[i] > q[i + 1] && i == q.length - 2){
                        bribes++
                    } else {
                        bribes += Math.abs((q[i]-1) - i)
                    }    
                }
            }
        }
        if(bribes > 0){
            console.log(bribes)
        }
    }
    
  • + 0 comments

    Basically it boils down to the idea that people who could have bribed me, would not be more than 2 position before my initial position to my current position.

  • + 0 comments

    C# submission

        public static void minimumBribes(List<int> q)
        {
            var numberOfPeople = q.Count;
            var numberOfBribes = 0;
            
            for (int i = 0; i < numberOfPeople; i++)
            {
                var positionsMoved = q[i] - (i + 1);
                
                if (positionsMoved > 2)
                {
                    Console.WriteLine("Too chaotic");
                    return;
                }
                
                var startingPoint = Math.Max(0, q[i] - 2);
                for (int j = startingPoint; j < i; j++)
                {
                    if (q[j] > q[i])
                    {
                        numberOfBribes += 1;
                    }
                }
            }
            Console.WriteLine(numberOfBribes);
        }
    
    }
    
  • + 0 comments

    Here is my solution in Java. I initially solved it by replacing the elements in list with its correct position and counting the bribes while doing that. That yielded result but few of the test cases failed due to performance. I tried a little in local IDE and took help online to arrive at this solution}

    public static void minimumBribes(List<Integer> inp) {
            int totalbribe = 0;
            int currentPerson = 0;
            int originalPosition = 0;
            int size = inp.size();
            
            //loop through all the elements
            for (int i = 0; i < size; i++){
                currentPerson = inp.get(i);
                originalPosition = currentPerson - 1;
                
                //If deviation of more than 2 position then return
                if(originalPosition - i > 2){
                    System.out.println("Too chaotic");
                    return;
                }
                
                //count the number of bribes current person has received.
                //currentPerson - 2 because if someone moved more than 2 position then it is chaotic 
                for (int j = Math.max(0, currentPerson - 2); j < i; j++){
                    if (inp.get(j) > currentPerson){
                        totalbribe++;
                    }
                }
            }
            
            System.out.println(totalbribe);
        }