leetcode

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

1289.cpp (762B)


      1 class Solution {
      2   public:
      3     int minFallingPathSum(const vector<vector<int>> &grid) const {
      4         const int n = size(grid), m = size(grid[0]);
      5         int first = 0, second = 0, pos = -1;
      6 
      7         for (auto i = 0; i < n; i++) {
      8             auto first2 = INT_MAX, second2 = INT_MAX, pos2 = -1;
      9             for (auto j = 0; j < m; j++) {
     10                 const int crnt = grid[i][j] + (j != pos ? first : second);
     11                 if (crnt < first2) {
     12                     second2 = first2;
     13                     first2 = crnt;
     14                     pos2 = j;
     15                 } else {
     16                     second2 = min(second2, crnt);
     17                 }
     18             }
     19             first = first2, second = second2, pos = pos2;
     20         }
     21 
     22         return first;
     23     }
     24 };