ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-06-25 01:31:06  阅读:188  来源: 互联网

标签:Training int minCost 2304 solution moveCost length cost grid



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[i][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:

Input: grid = [[5,3],[4,0],[2,1]], moveCost = [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]]
Output: 17
Explanation: The path with the minimum possible cost is the path 5 -> 0 -> 1.
- The sum of the values of cells visited is 5 + 0 + 1 = 6.
- The cost of moving from 5 to 0 is 3.
- The cost of moving from 0 to 1 is 8.
So the total cost of the path is 6 + 3 + 8 = 17.

Example 2:

Input: 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]]
Output: 6
Explanation: The path with the minimum possible cost is the path 2 -> 3.
- The sum of the values of cells visited is 2 + 3 = 5.
- The cost of moving from 2 to 3 is 1.
So the total cost of this path is 5 + 1 = 6.

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 2 <= m, n <= 50
  • grid consists of distinct integers from 0 to m * n - 1.
  • moveCost.length == m * n
  • moveCost[i].length == n
  • 1 <= moveCost[i][j] <= 100
 Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

A classic DP problem (Dynamic Programming) because it's either ask you a boolean yes or no questions, Or ask for extreme values, e.g., min, max
  • Build up a minCost 2 day array, each array element denotes by far the min cost to reach to that element
  • Make sure to initialize the values.

 

 

 


Solutions

 1 public int minPathCost(int[][] grid, int[][] moveCost) {
 2     if (grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0 || moveCost == null) {
 3         return -1; // or throw exception
 4     }
 5 
 6     int[][] minCost = new int[grid.length][grid[0].length];
 7     for (int i = 0; i < minCost[0].length; i++) {
 8         minCost[0][i] = grid[0][i];  // first row costs are the init values
 9     }
10 
11     for (int i = 1; i < minCost.length; i++) {
12         for (int j = 0; j < minCost[0].length; j++) {
13             int min = Integer.MAX_VALUE;
14             for (int k = 0; k < minCost[0].length; k++) {
15                 min = Math.min(min, minCost[i - 1][k] + moveCost[grid[i - 1][k]][j]);
16             }
17             minCost[i][j] = grid[i][j] + min;
18         }
19     }
20     int finalMin = Integer.MAX_VALUE;
21 
22     for (int i = 0; i < minCost[0].length; i++) {
23         finalMin = Math.min(minCost[minCost.length - 1][i], finalMin);
24     }
25     return finalMin;
26 }

 

 

  Time Complexity: O(N^3) since going through the 2-d array and another nested loop
Space Complexity: O(N^2) used the 2-d minCost array

References

  • None

标签:Training,int,minCost,2304,solution,moveCost,length,cost,grid
来源: https://www.cnblogs.com/baozitraining/p/16410625.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有