Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimize Matrix Path Sum Queries

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Optimize Matrix Path Sum Queries. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given a 2D grid of non-negative integers grid with m rows and n columns, and a list of query cells queries, return the minimum path sum from the top-left cell (0, 0) to each queried cell. A path may move only right or down. The goal is to optimize performance when many queries are asked on the same grid.

Constraints

  • 1 <= m, n <= 500
  • 0 <= grid[r][c] <= 10^4
  • 1 <= queries.length <= 10^5
  • 0 <= queries[i][0] < m
  • 0 <= queries[i][1] < n

Function Signature

def min_path_sum_queries(grid, queries):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output