New Year Chaos

Sort by

recency

|

292 Discussions

|

  • + 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);
        }
    
  • + 0 comments
        if curr_person - (i+1) > 2: # 原本 curr_person 在 curr_person-1 位置上,計算 數值與位置的差是否大於2
    
        start = 0 if curr_person - 2 < 0 else curr_person - 2 # 確保curr_person-2大於0
    

    `

  • + 0 comments

    It seems as though this problem operates under the assumption that people execute their bribes from the front to the back of their initial position.

  • + 0 comments

    Python solution:

    def minimumBribes(arr: list) -> None:
        """Count bribes from the perspective of the person being overtaken."""
        bribes = 0
        for i, sticker in enumerate(arr): 
            pos = i + 1     # queue position is 1-indexed
            # check for 2+ bribes:
            if sticker - pos > 2:
                print("Too chaotic")
                return
            # for each person, count no. of overtakes
            # only have to check previous two positions:
            for j in range(max(0, sticker - 2), i):    # not i - 2
                if arr[j] > sticker:
                    bribes += 1                    
        print(bribes)