My C code, 4ms, any improvement?


  • 0
    G

    int minimumTotal(int **triangle, int numRows)

    {

        int i, j;
    
        int min;
    
        int *ptr = malloc(sizeof(int) * numRows);
    
        if(numRows == 1)
    
                return **triangle;
    
        for(i = 0; i < numRows; i++)
    
                ptr[i] = triangle[numRows-1][i];
    
        for(i = numRows - 2; i >= 0; i--)
    
                for(j = 0; j <= i; j++) {
    
                        if(ptr[j] > ptr[j+1])
    
                                ptr[j] = triangle[i][j] + ptr[j+1];
    
                        else
    
                                ptr[j] = triangle[i][j] + ptr[j];
    
                }
    
        min = ptr[0];
    
        free(ptr);
    
        return min;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.