Baozi Training Leetcode solution 2304. Minimum Path Cost in a Grid

打印 上一主题 下一主题

主题 1878|帖子 1878|积分 5634

Problem Statement 

You are given a 0-indexed m x n integer matrix grid consisting of distinct integers from 0 to m * n - 1. You can move in this matrix from a cell to any other cell in the next row. That is, if you are in cell (x, y) such that x < m - 1, you can move to any of the cells (x + 1, 0), (x + 1, 1), ..., (x + 1, n - 1). Note that it is not possible to move from cells in the last row.
Each possible move has a cost given by a 0-indexed 2D array moveCost of size (m * n) x n, where moveCost[j] is the cost of moving from a cell with value i to a cell in column j of the next row. The cost of moving from cells in the last row of grid can be ignored.
The cost of a path in grid is the sum of all values of cells visited plus the sum of costs of all the moves made. Return the minimum cost of a path that starts from any cell in the first row and ends at any cell in the last row.
 
Example 1:
  1. <strong data-original-attrs="{"style":""}">Input:</strong> grid = [[5,3],[4,0],[2,1]], moveCost = [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]]
  2. <strong data-original-attrs="{"style":""}">Output:</strong> 17
  3. <strong data-original-attrs="{"style":""}">Explanation: </strong>The path with the minimum possible cost is the path 5 -> 0 -> 1.
  4. - The sum of the values of cells visited is 5 + 0 + 1 = 6.
  5. - The cost of moving from 5 to 0 is 3.
  6. - The cost of moving from 0 to 1 is 8.
  7. So the total cost of the path is 6 + 3 + 8 = 17.
复制代码
Example 2:
  1. <strong data-original-attrs="{"style":""}">Input:</strong> grid = [[5,1,2],[4,0,3]], moveCost = [[12,10,15],[20,23,8],[21,7,1],[8,1,13],[9,10,25],[5,3,2]]
  2. <strong data-original-attrs="{"style":""}">Output:</strong> 6
  3. <strong data-original-attrs="{"style":""}">Explanation:</strong> The path with the minimum possible cost is the path 2 -> 3.
  4. - The sum of the values of cells visited is 2 + 3 = 5.
  5. - The cost of moving from 2 to 3 is 1.
  6. So the total cost of this path is 5 + 1 = 6.
复制代码
 
Constraints:
<ul data-original-attrs="{"style":"-webkit-text-stroke-width: 0px; orphans: 2; widows: 2;"}"><li data-original-attrs="{"style":""}">m == grid.length<li data-original-attrs="{"style":""}">n == grid.length<li data-original-attrs="{"style":""}">2

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

吴旭华

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表