New Year Chaos

Sort by

recency

|

291 Discussions

|

  • + 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)
    
  • + 0 comments

    This approach is mostly about the fact that we only need to count all people in front of us (who could bribe the current person and not more than two bigger than us) def minimumBribes(q): bribes = 0

    for i in range(len(q)):
        if q[i] - (i + 1) > 2:  
            print("Too chaotic")
            return
    
        for j in range(max(0, q[i] - 2), i):
            if q[j] > q[i]:  
                bribes += 1
    
    print(bribes)