Sort by

recency

|

1935 Discussions

|

  • + 0 comments

    'Python 3.9' Solution:

     # Making a list compare_arr to incorporate the changes in the initial array and compare the units of displacement
    
     n=len(q)
    compare_arr=[i+1 for i in range(n)]
    count=0
    chaotic=0
    for i in range(n-1):
        if q[i]==compare_arr[i]:
            continue
        elif q[i]==compare_arr[i+1]:
            compare_arr[i], compare_arr[i+1]= compare_arr[i+1], compare_arr[i]
            count+=1
        elif q[i]==compare_arr[i+2]:
            compare_arr[i], compare_arr[i+2]= compare_arr[i+2], compare_arr[i]
            compare_arr[i+2], compare_arr[i+1]= compare_arr[i+1], compare_arr[i+2]
            count+=2
        else:
            chaotic=1
            break
    if chaotic==1:
        print('Too chaotic')
    else:
        print(count)  
    
  • + 0 comments

    this is an awful problem

  • + 0 comments

    My C Solution:

    void minimumBribes(int q_count, int* q) {

    unsigned int total = 0;
    unsigned int prev;
    unsigned int temp;
    int arr[q_count];
    for(int i = 1; i <= q_count; i++)
        arr[i-1] = i;
    for(int i = 0; i < q_count; i++){
        if(q[i] == arr[i])
            continue;
        else if(q[i] == arr[i+1]){
            temp = arr[i+1];
            arr[i+1] = arr[i];
            arr[i] = temp;
            total += 1;
        }
        else if(q[i] == arr[i+2]){
            temp = arr[i+2];
            arr[i + 2] = arr[i + 1];
            arr[i+1] = arr[i];
            arr[i] = temp;
            total += 2;
        }
        else {
            printf("Too chaotic\n");
            return;
        }
    }
    
    printf("%d\n", total);
    

    }

  • + 0 comments

    My Kotlin solution

    fun minimumBribes(q: Array<Int>): Unit {
        var result = 0
        var list = q.toMutableList()
        for (index in (q.lastIndex) downTo 0){
            val sortedValue = (index +1)
            if (list[index] == sortedValue){
                // make the list smaller so that next iteration would take less time to find the lastIndexOf
                list.removeAt(index)
                continue
            }
            
            // search from tail of list gets the index quicker in this case
            val unSortedIndex = list.lastIndexOf(sortedValue)
            val shift = (index - unSortedIndex)
            if(shift > 2){
                println("Too chaotic")
                return
            }
            
            list.removeAt(unSortedIndex) 
            result += shift
        }
        println(result)    
    }
    
  • + 0 comments

    This is the solution for Java

        public static void minimumBribes(List<Integer> q) {
            int n = q.size();
            int bribes = 0;
    
            for(int i = n; i >= 1; i--) {
                // quick exit if number is in the right place
                if (q.get(i - 1) == i) {
                    continue;
                }
    
                // case where expected element is only one
                // places ahead
                if(q.get(i - 2) == i) {
                    bribes++;
                    Collections.swap(q, i-1, i-2);
                }
    
                // case where expected element has moved two
                // places away
                if(q.get(i - 3) == (i)) {
                    bribes += 2;
                    Collections.rotate(q.subList(i - 3, i), -1);
                }
    
                // case where expected element has moved ahead
                // further places
                else {
                    System.out.println("Too chaotic");
                    return;
                }
            }
            System.out.printf("%d\n",bribes);
        }
            }
            System.out.printf("%d\n",bribes);
        }