javascriptalgorithmsortingdata-structurestowers-of-hanoi

Print the Minimum number of steps needed to sort the pile


I'm trying to solve this problem:

Playing Disks:

DJ Varun works in a club and plays cool songs. He has N disks of different albums (each of equal radius). Every disk has a distinct number out of 1 to N associated with it.

Disks are placed one over the other in a single pile. Varun wants to sort this pile of disks in an increasing order i.e. top to bottom. But he has a very special method of doing this.

In a single step he can only choose one disk out of the pile and he can only put it at the top. So the task here is that Varun wants to sort his pile of disks in minimum number of possible steps. What is the minimum number of possible steps to sort the pile so that that Varun can check whether he is doing his work right or wrong?

Input Format: First line contains integer N, the size of the array followed by, an array of size N containing integers 1 to N in any random order which shows the position of disks top to bottom.

Constraints: 0<=N<=1000

Output Format: Print the Minimum number of steps needed to sort the pile. If it can't be sorted then return output as -1.

Sample TestCase 1

Input:

3
1
3
2

Output:

2

My solution so far gets only 20%, and I need at least 60%:

var input_stdin_array = ['3' , '1' , '3' , '2'];

//convert into number
input_stdin_array = input_stdin_array.map(i=>Number(i))

//remove lenght
input_stdin_array.splice(0,1);

var Total = input_stdin_array.length;
var noOfSwap = Total - 1;

var noOfSeq = 0;
var lastIndex = 0;
var currentValue = 0;
var perviousValue = 0;
for(var i=0; i< input_stdin_array.length;i++)
{

var maxNum = Math.max(...input_stdin_array);
var indexOfMax = input_stdin_array.indexOf(maxNum);
if(i == 0)
{
    currentValue = maxNum;
}
    
if(indexOfMax != 0 )
{
    if(i == 0)
        lastIndex = indexOfMax - 1;
    else
        lastIndex-=1;

    console.log('Last index',lastIndex);

    if(lastIndex >= 0)
    {
        perviousValue = input_stdin_array[lastIndex];
        var checkValue = currentValue -1;
        currentValue = checkValue;
        console.log('Previous ',perviousValue, ' Checkvalue ',checkValue);
        if(i != 0)
        {
            if(perviousValue == checkValue)
            {
                noOfSeq++;
            }
        }
        
    }
}
}
// noOfSeq +=1;
console.log("NO of seq ",noOfSeq);
noOfSwap = noOfSwap - noOfSeq;
noOfSwap = noOfSwap == 0 ? -1 : noOfSwap;
console.log("no of swp ",noOfSwap);

Solution

  • We can make some observations:

    That leads to this quite simple algorithm:

    function numSteps(arr) {
        let find = arr.length;
        for (let i = arr.length - 1; i >= 0; i--) {
            if (arr[i] == find) find--;
        }
        return find;
    }
    
    // I/O handling
    
    let output = document.querySelector("span");
    let input = document.querySelector("textarea");
    
    input.addEventListener("input", refresh);
    
    function refresh() {
        let arr = input.value.match(/\d+/g).slice(1).map(Number);
        let result = numSteps(arr);
        output.textContent = result;
    }
    
    refresh();
    div { float: left; margin: 5px }
    <div>Input:<br>
    <textarea rows=10 cols=5>3
    1
    3
    2
    </textarea></div>
    <div>Output:<br>
    <span></span></div>