Skip to content

ORIGINAL CODING QUESTION

Minimum route charge

Move right/down from top-left to bottom-right. Return the minimum total charge including both endpoints.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

rows cols, then rows×cols cell values in row-major order. Print one integer.

Constraints

1 ≤ rows,cols ≤ 12; 0 ≤ cell charge ≤ 100.

Example 1

Input

2 2 1 9 2 3

Output

6

The route 1→2→3 costs 6.

Example 2

Input

1 3 2 0 4

Output

6

The only route costs 6.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Choose the cheaper predecessor.

Hint 2

Boundary cells have only one predecessor.

Hint 3

A rolling row can store the current best costs.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →