Running Time of Algorithms

Sort by

recency

|

275 Discussions

|

  • + 0 comments

    Did anyone try BASH solution it keep getting me Time limit exceeded?

  • + 0 comments

    Here is problem solution in python java c++ c and Javascript - https://programmingoneonone.com/hackerrank-running-time-of-algorithms-problem-solution.html

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/pAWOIEQemtc

    int runningTime(vector<int> arr) {
        int i,j;
        int value;
        int result = 0;
        for(i=1;i<arr.size();i++)
        {
            value=arr[i];
            j=i-1;
            while(j>=0 && value<arr[j])
            {
                arr[j+1]=arr[j];
                j=j-1;
                result ++;
            }
            arr[j+1]=value;
        }
        return result;
    }
    
  • + 0 comments

    Here's my PHP solution:

    function runningTime($arr) {
        // Write your code here
        $total = 0;
        
        for ($i=0; $i < count($arr)-1; $i++) {
            $temp = 0;
            for ($j=$i+1; $j < count($arr); $j++) {
                if ($arr[$j] < $arr[$i]) {
                    $temp = $arr[$j];
                    // delete array $arr[$j]
                    unset($arr[$j]);
                    
                    // insert in front of array
                    array_splice( $arr, $i, 0, $temp );
                    
                    // count step
                    $total = $total + ($j - $i);
                    
                    // re-index array
                    $arr = array_values($arr);
                }
            }
        } 
        return $total;
    }
    
  • + 0 comments

    solution from my side

    count = 0
        for i in range(1, len(arr)):
            key = arr[i]
            j = i - 1
    
            while j >= 0 and key < arr[j]:
                count +=1
                arr[j + 1] = arr[j]
                j -= 1
            arr[j + 1] = key
        return count