LeetCode Solutions

1001. Grid Illumination

Time:

Space:

			

class Solution {
 public:
  vector<int> gridIllumination(int N, vector<vector<int>>& lamps,
                               vector<vector<int>>& queries) {
    vector<int> ans;
    unordered_map<int, int> rows;
    unordered_map<int, int> cols;
    unordered_map<int, int> diag1;
    unordered_map<int, int> diag2;
    unordered_set<pair<int, int>, pairHash> lampsSet;

    for (vector<int>& lamp : lamps) {
      int i = lamp[0];
      int j = lamp[1];
      if (lampsSet.insert({i, j}).second) {
        ++rows[i];
        ++cols[j];
        ++diag1[i + j];
        ++diag2[i - j];
      }
    }

    for (const vector<int>& q : queries) {
      int i = q[0];
      int j = q[1];
      if (rows[i] || cols[j] || diag1[i + j] || diag2[i - j]) {
        ans.push_back(1);
        for (int y = max(0, i - 1); y < min(N, i + 2); ++y)
          for (int x = max(0, j - 1); x < min(N, j + 2); ++x)
            if (lampsSet.erase({y, x})) {
              --rows[y];
              --cols[x];
              --diag1[y + x];
              --diag2[y - x];
            }
      } else
        ans.push_back(0);
    }

    return ans;
  }

 private:
  struct pairHash {
    size_t operator()(const pair<int, int>& p) const {
      return p.first ^ p.second;
    }
  };
};
			

class Solution {
  public int[] gridIllumination(int N, int[][] lamps, int[][] queries) {
    List<Integer> ans = new ArrayList<>();
    Map<Integer, Integer> rows = new HashMap<>();
    Map<Integer, Integer> cols = new HashMap<>();
    Map<Integer, Integer> diag1 = new HashMap<>();
    Map<Integer, Integer> diag2 = new HashMap<>();
    Set<Long> lampsSet = new HashSet<>();

    for (int[] lamp : lamps) {
      int i = lamp[0];
      int j = lamp[1];
      if (lampsSet.add(hash(i, j))) {
        rows.put(i, rows.getOrDefault(i, 0) + 1);
        cols.put(j, cols.getOrDefault(j, 0) + 1);
        diag1.put(i + j, diag1.getOrDefault(i + j, 0) + 1);
        diag2.put(i - j, diag2.getOrDefault(i - j, 0) + 1);
      }
    }

    for (int[] q : queries) {
      int i = q[0];
      int j = q[1];
      if (rows.getOrDefault(i, 0) > 0 || cols.getOrDefault(j, 0) > 0 ||
          diag1.getOrDefault(i + j, 0) > 0 || diag2.getOrDefault(i - j, 0) > 0) {
        ans.add(1);
        for (int y = Math.max(0, i - 1); y < Math.min(N, i + 2); ++y)
          for (int x = Math.max(0, j - 1); x < Math.min(N, j + 2); ++x)
            if (lampsSet.remove(hash(y, x))) {
              rows.put(y, rows.getOrDefault(y, 0) - 1);
              cols.put(x, cols.getOrDefault(x, 0) - 1);
              diag1.put(y + x, diag1.getOrDefault(y + x, 0) - 1);
              diag2.put(y - x, diag2.getOrDefault(y - x, 0) - 1);
            }
      } else
        ans.add(0);
    }

    return ans.stream().mapToInt(Integer::intValue).toArray();
  }

  private long hash(int i, int j) {
    return ((long) i << 32) + j;
  }
}
			

class Solution:
  def gridIllumination(self, N: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
    ans = []
    rows = Counter()
    cols = Counter()
    diag1 = Counter()
    diag2 = Counter()
    lampsSet = set()

    for i, j in lamps:
      if (i, j) not in lampsSet:
        lampsSet.add((i, j))
        rows[i] += 1
        cols[j] += 1
        diag1[i + j] += 1
        diag2[i - j] += 1

    for i, j in queries:
      if rows[i] or cols[j] or diag1[i + j] or diag2[i - j]:
        ans.append(1)
        for y in range(max(0, i - 1), min(N, i + 2)):
          for x in range(max(0, j - 1), min(N, j + 2)):
            if (y, x) in lampsSet:
              lampsSet.remove((y, x))
              rows[y] -= 1
              cols[x] -= 1
              diag1[y + x] -= 1
              diag2[y - x] -= 1
      else:
        ans.append(0)

    return ans