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.
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.
def min_path_sum_queries(grid, queries):