leetcode

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

0931.cpp (759B)


      1 class Solution {
      2     int n;
      3 
      4     bool valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < n; }
      5 
      6   public:
      7     int minFallingPathSum(vector<vector<int>> &matrix) {
      8         n = matrix.size();
      9 
     10         for (int i = n - 2; i >= 0; i--) {
     11             for (int j = 0; j < n; j++) {
     12                 int mini = INT_MAX;
     13                 for (int k = -1; k <= 1; k++) {
     14                     int x = i + 1;
     15                     int y = j + k;
     16                     if (!valid(x, y)) continue;
     17                     mini = min(mini, matrix[x][y]);
     18                 }
     19                 matrix[i][j] += mini;
     20             }
     21         }
     22 
     23         int mini = INT_MAX;
     24         for (int i = 0; i < n; i++)
     25             mini = min(mini, matrix[0][i]);
     26         return mini;
     27     }
     28 };