leetcode

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

1765.cpp (1100B)


      1 class Solution {
      2     typedef tuple<int, int> record;
      3 
      4   public:
      5     vector<vector<int>> highestPeak(vector<vector<int>> &isWater) {
      6         static const int offset[] = {-1, 0, 1, 0, -1};
      7         const int n = isWater.size(), m = isWater[0].size();
      8 
      9         queue<record> q;
     10         for (int i = 0; i < n; i++) {
     11             for (int j = 0; j < m; j++) {
     12                 if (isWater[i][j])
     13                     isWater[i][j] = 0, q.push({i, j});
     14                 else
     15                     isWater[i][j] = INT_MAX;
     16             }
     17         }
     18 
     19         while (!q.empty()) {
     20             for (int k = q.size(); k > 0; k--) {
     21                 const auto [i, j] = q.front();
     22                 q.pop();
     23                 for (int k = 0; k < 4; k++) {
     24                     const int x = i + offset[k], y = j + offset[k + 1];
     25                     if (x < 0 || x >= n || y < 0 || y >= m) continue;
     26                     if (isWater[x][y] != INT_MAX) continue;
     27                     isWater[x][y] = isWater[i][j] + 1;
     28                     q.push({x, y});
     29                 }
     30             }
     31         }
     32 
     33         return isWater;
     34     }
     35 };