Interview Question Series | Season 2

Problem Clarification:

  • A car is at the top-left corner of the matrix and needs to move to the bottom-right corner.
  • The car can only move right or down.
  • The matrix contains the amount of gasoline consumed to reach each cell.
  • We need to find the path from the top-left to the bottom-right with the minimum gasoline consumption.

Dynamic Programming Approach:

We can solve this problem using dynamic programming by constructing a matrix dp where each element dp[i][j] represents the minimum gasoline consumption to reach cell (i, j) from (0, 0).

  • Base case: The gasoline consumption to reach the starting cell (0, 0) is simply MAP[0][0].
  • First row: Since the car can only move right along the first row, the minimum gasoline consumption at each cell (0, j) will be the sum of all previous cells in the first row.
  • First column: Since the car can only move down along the first column, the minimum gasoline consumption at each cell (i, 0) will be the sum of all previous cells in the first column.
  • Other cells: For each other cell (i, j), the car can either come from the left or from above. So, the value for dp[i][j] will be the minimum of the gasoline consumption from the cell to the left or the cell above, plus the current cell’s gasoline consumption (MAP[i][j]).

Corrected Code:

#include <stdio.h>

int MIN(int a, int b) {
    return a < b ? a : b;
}

int minGasolinePathSum(int MAP[3][3], int m, int n) {
    int Matrix[m][n];

    // Initialize the starting point
    Matrix[0][0] = MAP[0][0];

    // Initialize the first row (can only move right)
    for (int i = 1; i < n; i++) {
        Matrix[0][i] = Matrix[0][i - 1] + MAP[0][i];
    }

    // Initialize the first column (can only move down)
    for (int i = 1; i < m; i++) {
        Matrix[i][0] = Matrix[i - 1][0] + MAP[i][0];
    }

    // Fill the rest of the matrix
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            Matrix[i][j] = MAP[i][j] + MIN(Matrix[i - 1][j], Matrix[i][j - 1]);
        }
    }

    // The bottom-right corner has the minimum gasoline consumed to reach it
    return Matrix[m - 1][n - 1];
}

int main() {
    int MAP[3][3] = {
        {1, 3, 1},
        {1, 5, 1},
        {4, 2, 1}
    };
    int m = 3, n = 3;

    int result = minGasolinePathSum(MAP, m, n);
    printf("Minimum liters of gasoline: %d litersn", result);

    return 0;
}

Explanation of Code:

  1. Initialization: We start by initializing the top-left corner (Matrix[0][0]) with the value of MAP[0][0], because that’s where the car starts.

  2. First Row: We process the first row of the matrix. Since the car can only move to the right, each cell in the first row is the sum of the previous cell in the row and the corresponding value in MAP.

  3. First Column: Similarly, we process the first column. Since the car can only move down, each cell in the first column is the sum of the previous cell in the column and the corresponding value in MAP.

  4. Rest of the Matrix: For each other cell, we calculate the minimum gasoline consumed to reach that cell by considering the two possible directions (from left or from above). We add the current cell’s gasoline consumption (MAP[i][j]) to the minimum of the two.

  5. Result: The bottom-right corner of the matrix (Matrix[m-1][n-1]) will contain the minimum gasoline required to reach the destination.

Example Walkthrough:

Given the matrix:

MAP = {
    {1, 3, 1},
    {1, 5, 1},
    {4, 2, 1}
}
  • Start at (0, 0) with 1 gasoline.
  • First row: Matrix[0][1] = 1 + 3 = 4, Matrix[0][2] = 4 + 1 = 5.
  • First column: Matrix[1][0] = 1 + 1 = 2, Matrix[2][0] = 2 + 4 = 6.
  • For (1, 1): It’s the minimum of moving from left (4) and moving from above (2) plus the gasoline at that position: Matrix[1][1] = 2 + 5 = 7.
  • Similarly, calculate for the other cells.
  • The result will be in the bottom-right corner: Matrix[2][2] = 7.

Thus, the minimum gasoline required is 7.

Output:

Minimum liters of gasoline: 7 liters

Time Complexity:

  • Time Complexity: O(m * n), where m is the number of rows and n is the number of columns in the matrix. We iterate through every cell of the matrix once.
  • Space Complexity: O(m * n), for storing the dynamic programming matrix.
Scroll to Top