BackeasyStringsGoogleAmazon

Network Protocol Tracker 23 Solution

Problem Statement

You are tasked with analyzing a 2D grid of network packet metrics to determine the optimal transmission path. The grid represents a network topology where each cell contains a non-negative integer indicating the signal strength or cost associated with that node. Your objective is to find the minimum cumulative cost to traverse from the top-left corner (0,0) to the bottom-right corner (rows-1, cols-1). Movement is restricted to only two directions: right (to the adjacent cell in the same row) or down (to the adjacent cell in the next row). You must compute the sum of the values along the path that yields the lowest total cost. If multiple paths yield the same minimum cost, return that minimum value. The solution must efficiently handle large grids by leveraging dynamic programming principles to avoid redundant calculations.

Example 1
Input
grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]]
Output
7

Explanation: The optimal path is: Start at (0,0) with value 1. Move right to (0,1) with value 3 (cumulative 4). Move right to (0,2) with value 1 (cumulative 5). Move down to (1,2) with value 1 (cumulative 6). Move down to (2,2) with value 1 (cumulative 7). Alternative path: (0,0)->(1,0)->(1,1)->(1,2)->(2,2) gives 1+1+5+1+1=9. Another: (0,0)->(0,1)->(1,1)->(2,1)->(2,2) gives 1+3+5+2+1=12. The minimum is 7.

Example 2
Input
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output
21

Explanation: The optimal path is: Start at (0,0) with value 1. Move right to (0,1) with value 2 (cumulative 3). Move right to (0,2) with value 3 (cumulative 6). Move down to (1,2) with value 6 (cumulative 12). Move down to (2,2) with value 9 (cumulative 21). Alternative path: (0,0)->(1,0)->(2,0)->(2,1)->(2,2) gives 1+4+7+8+9=29. Another: (0,0)->(0,1)->(1,1)->(2,1)->(2,2) gives 1+2+5+8+9=25. The minimum is 21.

Example 3
Input
grid = [[5]]
Output
5

Explanation: The grid is a single cell. The only path is to start and end at (0,0). The cost is simply the value of the cell, which is 5.

Example 4
Input
grid = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
Output
5

Explanation: All cells have value 1. The path length from (0,0) to (2,2) requires exactly 4 moves (2 right, 2 down), visiting 5 cells in total. Since each cell contributes 1 to the sum, the total cost is 5 * 1 = 5. Any valid path will have the same cost.

Constraints

  • 1 <= grid.length <= 200
  • 1 <= grid[0].length <= 200
  • 0 <= grid[i][j] <= 100
  • The answer is guaranteed to fit in a 32-bit integer.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Network Protocol Tracker 23 — Problem Statement & Solution Guide

StringsEasy2D Grid DP
TimeO(m*n)
|
SpaceO(m*n) or O(n) with row compression

Problem Description

You are tasked with analyzing a 2D grid of network packet metrics to determine the optimal transmission path. The grid represents a network topology where each cell contains a non-negative integer indicating the signal strength or cost associated with that node. Your objective is to find the minimum cumulative cost to traverse from the top-left corner (0,0) to the bottom-right corner (rows-1, cols-1). Movement is restricted to only two directions: right (to the adjacent cell in the same row) or down (to the adjacent cell in the next row). You must compute the sum of the values along the path that yields the lowest total cost. If multiple paths yield the same minimum cost, return that minimum value. The solution must efficiently handle large grids by leveraging dynamic programming principles to avoid redundant calculations.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Network Protocol Tracker 23"

easy

WHY DOES IT MATTER?

DP transforms exponential path enumeration into a tractable linear scan.

OPTIMIZATION CHALLENGE

The key is collapsing overlapping subproblems to achieve O(m·n) time.

REAL-WORLD CONNECTION

It mirrors routing algorithms that compute cheapest paths in mesh networks.

Prefer in‑place DP on the input grid when mutation is allowed to save memory.

COMPLEXITY AT A GLANCE

⏱ Time:O(m*n)
💾 Space:O(m*n) or O(n) with row compression

Core Theory — Why This Approach?

The problem is a classic dynamic‑programming (DP) scenario where the optimal substructure is that the minimum cost to reach any cell (i, j) equals the cell's own cost plus the minimum of the costs to reach the cell directly above (i‑1, j) or to the left (i, j‑1). A naive recursive solution explores all possible right‑down paths, leading to exponential time O(2^{m+n}) and massive recomputation, which quickly becomes infeasible for grids larger than a few dozen rows or columns. By storing intermediate results in a DP table, each cell is computed exactly once, collapsing the exponential search space to a linear scan of the grid. This yields an optimal O(m·n) time algorithm with O(m·n) or O(n) auxiliary space, making it suitable for the large inputs typical in interview settings.

Interview Questions on This Problem

Q1Why is a simple recursion without memoization unsuitable for this problem?

Recursion explores every possible path, causing exponential blow‑up. Memoization eliminates repeated work, turning it into linear DP.

Q2Can you reduce the space complexity below O(m·n) and how?

Yes, by keeping only the previous row (or column) of DP values because each cell depends only on its top and left neighbors. This reduces space to O(min(m, n)).

Q3How would you adapt the solution if diagonal moves were also allowed?

Include the top‑left diagonal cell in the recurrence: dp[i][j] = grid[i][j] + min(dp[i‑1][j], dp[i][j‑1], dp[i‑1][j‑1]). The DP framework stays the same, only the transition expands.

Examples

Example 1

Input

grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]]

Output

7

Explanation: The optimal path is: Start at (0,0) with value 1. Move right to (0,1) with value 3 (cumulative 4). Move right to (0,2) with value 1 (cumulative 5). Move down to (1,2) with value 1 (cumulative 6). Move down to (2,2) with value 1 (cumulative 7). Alternative path: (0,0)->(1,0)->(1,1)->(1,2)->(2,2) gives 1+1+5+1+1=9. Another: (0,0)->(0,1)->(1,1)->(2,1)->(2,2) gives 1+3+5+2+1=12. The minimum is 7.

Example 2

Input

grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Output

21

Explanation: The optimal path is: Start at (0,0) with value 1. Move right to (0,1) with value 2 (cumulative 3). Move right to (0,2) with value 3 (cumulative 6). Move down to (1,2) with value 6 (cumulative 12). Move down to (2,2) with value 9 (cumulative 21). Alternative path: (0,0)->(1,0)->(2,0)->(2,1)->(2,2) gives 1+4+7+8+9=29. Another: (0,0)->(0,1)->(1,1)->(2,1)->(2,2) gives 1+2+5+8+9=25. The minimum is 21.

Example 3

Input

grid = [[5]]

Output

5

Explanation: The grid is a single cell. The only path is to start and end at (0,0). The cost is simply the value of the cell, which is 5.

Example 4

Input

grid = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

Output

5

Explanation: All cells have value 1. The path length from (0,0) to (2,2) requires exactly 4 moves (2 right, 2 down), visiting 5 cells in total. Since each cell contributes 1 to the sum, the total cost is 5 * 1 = 5. Any valid path will have the same cost.

Constraints

  • 1 <= grid.length <= 200
  • 1 <= grid[0].length <= 200
  • 0 <= grid[i][j] <= 100
  • The answer is guaranteed to fit in a 32-bit integer.

Optimal Approach & Strategy

Iteratively fill a DP matrix (or reuse the input grid) where each cell holds its own cost plus the minimum of the top and left neighbor costs.

Brute Force Approach

Recursively try every right‑down path and keep the minimum sum, which is exponential in grid dimensions.

Verified Code Solutions

JavaScript Solution
Time: O(m*n)
function solution(nums, target) {
   let sum = 0;
   for (let num of nums) {
       if (num <= target) {
           sum += num;
       }
   }
   return sum;
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.