Minimum Swaps 2

Sort by

recency

|

2470 Discussions

|

  • + 0 comments

    I noticed that in Julia the code raises error for STDIN written with capital letters. Once I changed it to stdin, everything went smooth. So, I guess you should update the current version (unless I am missing something).

  • + 0 comments

    Python code.

    Final ordered array should look like [1, 2, 3, ...], so we can iterate through the array and check if current value matches index+1. If they do not match, find the index of the value that matches index+1. Then swap the values at the current index and the other index and record the swap.

    Note: this code wouldn't work if the array isn't consecutive but it seems like all the test case arrays are consecutive now.

    def minimumSwaps(arr):
        temp = 0
        swap = 0
        for x in range(len(arr) - 1):
            if arr[x] != x + 1:
                min_index = arr.index(x + 1)
                temp = arr[x]
                arr[x] = x + 1
                arr[min_index] = temp
                swap += 1
        return swap
    
  • + 0 comments

    I though might be helpfull to someone..

    int minimumSwaps(int arr_count, int* arr) {
    
        int index = 0;
        int swaps = 0;
        int elem = 0;
        int tmp = 0;
    
        while (true)
        {
            elem = arr[index];
            if (elem != index +1) 
            {
                tmp = arr[index];
                arr[index] = arr[elem - 1];
                arr[elem - 1] = tmp;
                swaps++;
            }
            if (index + 1 == arr[index])
            {
                // move on to the next element
                index++;
            }
            if (arr_count == index + 1)
            {
                // we are done
                break;
            }
        }
        return swaps;
    }
    
  • + 0 comments

    My JavaScript (JS/node.js) solution. This assumes the first number may not start at 1.

    // Complete the minimumSwaps function below.
    function minimumSwaps(arr) {
        let swaps = 0; // Counter for minimum number of swaps
        // bubble sort
        
        let index = 0;
        
        // Find the lowest value in the array:
        const minimum = Math.min(...arr);
    
        while (index < arr.length) {
            // Subtract 1 from the element to determin it's correct position in the array
            const elementAsIndex = arr[index] - 1;
            // Because the numbers are consecutive, but may not start at 1,
            // we can determin what the correct number is for our current index by...
            // adding the minimum value and adding the index. We subtract 1 to get the position.
            const locationInArray = index + minimum - 1;
    
    
            // Check if element is greater or less than it's position in the array
            if(elementAsIndex !== locationInArray) {
                swaps += 1;
                // Swap the elements
                [ arr[elementAsIndex], arr[locationInArray] ] = [ arr[locationInArray], arr[elementAsIndex] ];
            } else {
                index += 1;
            }   
        }
        // key element is consecutive... does it always start at 1?
        
        return swaps;
    
    }
    
  • + 1 comment
    // Complete the minimumSwaps function below.
    static int minimumSwaps(int[] arr) {
        int count = 0;
        int diff = 0;
        int temp = 0;
        for(int i=0; i<arr.Length; i++){
            if(arr[i] != i+1){
                diff = arr[i] - 1;
                temp = arr[diff];
                arr[diff] = arr[i];
                arr[i] = temp;
                count++;
                i--;
            }
        }
    
        return count;
    }