leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2304.cpp (559B)


      1 class Solution {
      2   public:
      3     int minPathCost(vector<vector<int>> &grid, const vector<vector<int>> &moveCost) {
      4         const int n = grid.size(), m = grid[0].size();
      5 
      6         for (int i = n - 2; i >= 0; i--) {
      7             for (int j = 0; j < m; j++) {
      8                 int res = INT_MAX;
      9                 for (int k = 0; k < m; k++) {
     10                     res = min(res, grid[i + 1][k] + moveCost[grid[i][j]][k]);
     11                 }
     12                 grid[i][j] += res;
     13             }
     14         }
     15 
     16         return *min_element(begin(grid[0]), end(grid[0]));
     17     }
     18 };