leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1289.cpp (762B)
0 class Solution { 1 public: 2 int minFallingPathSum(const vector<vector<int>> &grid) const { 3 const int n = size(grid), m = size(grid[0]); 4 int first = 0, second = 0, pos = -1; 5 6 for (auto i = 0; i < n; i++) { 7 auto first2 = INT_MAX, second2 = INT_MAX, pos2 = -1; 8 for (auto j = 0; j < m; j++) { 9 const int crnt = grid[i][j] + (j != pos ? first : second); 10 if (crnt < first2) { 11 second2 = first2; 12 first2 = crnt; 13 pos2 = j; 14 } else { 15 second2 = min(second2, crnt); 16 } 17 } 18 first = first2, second = second2, pos = pos2; 19 } 20 21 return first; 22 } 23 };