Find the Cheapest Right-and-Down Grid Path

Quick Overview

Find the minimum total cell cost of a grid path that advances right or down, including the starting and ending cell costs.

Find the Cheapest Right-and-Down Grid Path

Company: Infosys

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each entry in the rectangular grid `costs` is a nonnegative cost. A path starts in the upper-left cell, finishes in the lower-right cell, and advances one cell at a time to the right or downward. Return the smallest total cost of any such path. Add the costs of every visited cell, including both endpoints. ### Examples **Example 1** ```text Input: costs = [[2,8,1],[3,1,1]] Output: 7 ``` Moving down, right, and right visits costs `2`, `3`, `1`, and `1`, for a total of `7`. Every other permitted path costs more. **Example 2** ```text Input: costs = [[5],[0],[4]] Output: 9 ``` There is only one column, so the path visits all three cells. ### Constraints - The grid has between `1` and `200` rows and between `1` and `200` columns. - All rows have equal length. - `0 <= costs[i][j] <= 200`

Overview: Find the minimum total cell cost of a grid path that advances right or down, including the starting and ending cell costs.

Infosys logo
Infosys
Oct 14, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each entry in the rectangular grid costs is a nonnegative cost. A path starts in the upper-left cell, finishes in the lower-right cell, and advances one cell at a time to the right or downward.

Return the smallest total cost of any such path. Add the costs of every visited cell, including both endpoints.

Examples

Example 1

Input: costs = [[2,8,1],[3,1,1]]
Output: 7

Moving down, right, and right visits costs 2, 3, 1, and 1, for a total of 7. Every other permitted path costs more.

Example 2

Input: costs = [[5],[0],[4]]
Output: 9

There is only one column, so the path visits all three cells.

Constraints

  • The grid has between 1 and 200 rows and between 1 and 200 columns.
  • All rows have equal length.
  • 0 <= costs[i][j] <= 200