Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
1,040 changes: 1,040 additions & 0 deletions tests/1001-1500/1001. grid-illumination/manifest.yaml

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions tests/1001-1500/1001. grid-illumination/sol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <vector>
#include <unordered_map>
#include <unordered_set>

using namespace std;

class Solution {
public:
vector<int> gridIllumination(int n, vector<vector<int>>& lamps, vector<vector<int>>& queries) {
unordered_map<int, int> rowCount, colCount, diag1, diag2;
unordered_set<long long> lampSet;
vector<int> ans(queries.size());
int dirs[9][2] = {{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,0}, {0,1}, {1,-1}, {1,0}, {1,1}};

for (auto& lamp : lamps) {
int r = lamp[0], c = lamp[1];
long long key = ((long long)r << 32) | (c & 0xFFFFFFFFLL);
if (!lampSet.count(key)) {
lampSet.insert(key);
rowCount[r]++;
colCount[c]++;
diag1[r - c]++;
diag2[r + c]++;
}
}

for (int i = 0; i < queries.size(); i++) {
int r = queries[i][0], c = queries[i][1];
if (rowCount[r] > 0 || colCount[c] > 0 || diag1[r - c] > 0 || diag2[r + c] > 0) {
ans[i] = 1;
for (auto& d : dirs) {
int nr = r + d[0], nc = c + d[1];
long long key = ((long long)nr << 32) | (nc & 0xFFFFFFFFLL);
if (lampSet.count(key)) {
lampSet.erase(key);
rowCount[nr]--;
colCount[nc]--;
diag1[nr - nc]--;
diag2[nr + nc]--;
}
}
} else {
ans[i] = 0;
}
}
return ans;
}
};
52 changes: 52 additions & 0 deletions tests/1001-1500/1001. grid-illumination/sol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;

public class Solution {
public int[] GridIllumination(int n, int[][] lamps, int[][] queries) {
Dictionary<int, int> rowCount = new Dictionary<int, int>();
Dictionary<int, int> colCount = new Dictionary<int, int>();
Dictionary<int, int> diag1 = new Dictionary<int, int>();
Dictionary<int, int> diag2 = new Dictionary<int, int>();
HashSet<(int, int)> lampSet = new HashSet<(int, int)>();
int[] ans = new int[queries.Length];
int[][] dirs = new int[][] {
new int[] {-1,-1}, new int[] {-1,0}, new int[] {-1,1},
new int[] {0,-1}, new int[] {0,0}, new int[] {0,1},
new int[] {1,-1}, new int[] {1,0}, new int[] {1,1}
};

foreach (var lamp in lamps) {
int r = lamp[0], c = lamp[1];
var key = (r, c);
if (!lampSet.Contains(key)) {
lampSet.Add(key);
rowCount[r] = rowCount.GetValueOrDefault(r, 0) + 1;
colCount[c] = colCount.GetValueOrDefault(c, 0) + 1;
diag1[r - c] = diag1.GetValueOrDefault(r - c, 0) + 1;
diag2[r + c] = diag2.GetValueOrDefault(r + c, 0) + 1;
}
}

for (int i = 0; i < queries.Length; i++) {
int r = queries[i][0], c = queries[i][1];
if (rowCount.GetValueOrDefault(r, 0) > 0 || colCount.GetValueOrDefault(c, 0) > 0 ||
diag1.GetValueOrDefault(r - c, 0) > 0 || diag2.GetValueOrDefault(r + c, 0) > 0) {
ans[i] = 1;
foreach (var d in dirs) {
int nr = r + d[0], nc = c + d[1];
var key = (nr, nc);
if (lampSet.Contains(key)) {
lampSet.Remove(key);
rowCount[nr]--;
colCount[nc]--;
diag1[nr - nc]--;
diag2[nr + nc]--;
}
}
} else {
ans[i] = 0;
}
}
return ans;
}
}
51 changes: 51 additions & 0 deletions tests/1001-1500/1001. grid-illumination/sol.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'dart:collection';

class Solution {
List<int> gridIllumination(int n, List<List<int>> lamps, List<List<int>> queries) {
Map<int, int> rowCount = HashMap<int, int>();
Map<int, int> colCount = HashMap<int, int>();
Map<int, int> diag1 = HashMap<int, int>();
Map<int, int> diag2 = HashMap<int, int>();
Set<String> lampSet = HashSet<String>();
List<int> ans = List<int>.filled(queries.length, 0);
List<List<int>> dirs = [
[-1,-1], [-1,0], [-1,1],
[0,-1], [0,0], [0,1],
[1,-1], [1,0], [1,1]
];

for (var lamp in lamps) {
int r = lamp[0], c = lamp[1];
var key = '$r,$c';
if (!lampSet.contains(key)) {
lampSet.add(key);
rowCount[r] = (rowCount[r] ?? 0) + 1;
colCount[c] = (colCount[c] ?? 0) + 1;
diag1[r - c] = (diag1[r - c] ?? 0) + 1;
diag2[r + c] = (diag2[r + c] ?? 0) + 1;
}
}

for (int i = 0; i < queries.length; i++) {
int r = queries[i][0], c = queries[i][1];
if ((rowCount[r] ?? 0) > 0 || (colCount[c] ?? 0) > 0 ||
(diag1[r - c] ?? 0) > 0 || (diag2[r + c] ?? 0) > 0) {
ans[i] = 1;
for (var d in dirs) {
int nr = r + d[0], nc = c + d[1];
var key = '$nr,$nc';
if (lampSet.contains(key)) {
lampSet.remove(key);
rowCount[nr] = rowCount[nr]! - 1;
colCount[nc] = colCount[nc]! - 1;
diag1[nr - nc] = diag1[nr - nc]! - 1;
diag2[nr + nc] = diag2[nr + nc]! - 1;
}
}
} else {
ans[i] = 0;
}
}
return ans;
}
}
42 changes: 42 additions & 0 deletions tests/1001-1500/1001. grid-illumination/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
func gridIllumination(n int, lamps [][]int, queries [][]int) []int {
rowCount := make(map[int]int)
colCount := make(map[int]int)
diag1 := make(map[int]int)
diag2 := make(map[int]int)
lampSet := make(map[[2]int]bool)
ans := make([]int, len(queries))
dirs := [][2]int{{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,0}, {0,1}, {1,-1}, {1,0}, {1,1}}

for _, lamp := range lamps {
r, c := lamp[0], lamp[1]
key := [2]int{r, c}
if !lampSet[key] {
lampSet[key] = true
rowCount[r]++
colCount[c]++
diag1[r - c]++
diag2[r + c]++
}
}

for i, q := range queries {
r, c := q[0], q[1]
if rowCount[r] > 0 || colCount[c] > 0 || diag1[r - c] > 0 || diag2[r + c] > 0 {
ans[i] = 1
for _, d := range dirs {
nr, nc := r + d[0], c + d[1]
key := [2]int{nr, nc}
if lampSet[key] {
delete(lampSet, key)
rowCount[nr]--
colCount[nc]--
diag1[nr - nc]--
diag2[nr + nc]--
}
}
} else {
ans[i] = 0
}
}
return ans
}
49 changes: 49 additions & 0 deletions tests/1001-1500/1001. grid-illumination/sol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.*;

class Solution {
public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
Map<Integer, Integer> rowCount = new HashMap<>();
Map<Integer, Integer> colCount = new HashMap<>();
Map<Integer, Integer> diag1 = new HashMap<>();
Map<Integer, Integer> diag2 = new HashMap<>();
Set<Long> lampSet = new HashSet<>();
int[] ans = new int[queries.length];
int[][] dirs = {{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,0}, {0,1}, {1,-1}, {1,0}, {1,1}};

for (int[] lamp : lamps) {
int r = lamp[0], c = lamp[1];
long key = (long)r << 32 | (c & 0xFFFFFFFFL);
if (!lampSet.contains(key)) {
lampSet.add(key);
rowCount.merge(r, 1, Integer::sum);
colCount.merge(c, 1, Integer::sum);
diag1.merge(r - c, 1, Integer::sum);
diag2.merge(r + c, 1, Integer::sum);
}
}

for (int i = 0; i < queries.length; i++) {
int r = queries[i][0], c = queries[i][1];
if (rowCount.getOrDefault(r, 0) > 0 ||
colCount.getOrDefault(c, 0) > 0 ||
diag1.getOrDefault(r - c, 0) > 0 ||
diag2.getOrDefault(r + c, 0) > 0) {
ans[i] = 1;
for (int[] d : dirs) {
int nr = r + d[0], nc = c + d[1];
long key = (long)nr << 32 | (nc & 0xFFFFFFFFL);
if (lampSet.contains(key)) {
lampSet.remove(key);
rowCount.merge(nr, -1, Integer::sum);
colCount.merge(nc, -1, Integer::sum);
diag1.merge(nr - nc, -1, Integer::sum);
diag2.merge(nr + nc, -1, Integer::sum);
}
}
} else {
ans[i] = 0;
}
}
return ans;
}
}
50 changes: 50 additions & 0 deletions tests/1001-1500/1001. grid-illumination/sol.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import kotlin.collections.HashMap
import kotlin.collections.HashSet

class Solution {
fun gridIllumination(n: Int, lamps: Array<IntArray>, queries: Array<IntArray>): IntArray {
val rowCount = HashMap<Int, Int>()
val colCount = HashMap<Int, Int>()
val diag1 = HashMap<Int, Int>()
val diag2 = HashMap<Int, Int>()
val lampSet = HashSet<Long>()
val ans = IntArray(queries.size)
val dirs = arrayOf(intArrayOf(-1,-1), intArrayOf(-1,0), intArrayOf(-1,1),
intArrayOf(0,-1), intArrayOf(0,0), intArrayOf(0,1),
intArrayOf(1,-1), intArrayOf(1,0), intArrayOf(1,1))

for (lamp in lamps) {
val r = lamp[0]; val c = lamp[1]
val key = (r.toLong() shl 32) or (c.toLong() and 0xFFFFFFFFL)
if (!lampSet.contains(key)) {
lampSet.add(key)
rowCount[r] = rowCount.getOrDefault(r, 0) + 1
colCount[c] = colCount.getOrDefault(c, 0) + 1
diag1[r - c] = diag1.getOrDefault(r - c, 0) + 1
diag2[r + c] = diag2.getOrDefault(r + c, 0) + 1
}
}

for (i in queries.indices) {
val r = queries[i][0]; val c = queries[i][1]
if ((rowCount[r] ?: 0) > 0 || (colCount[c] ?: 0) > 0 ||
(diag1[r - c] ?: 0) > 0 || (diag2[r + c] ?: 0) > 0) {
ans[i] = 1
for (d in dirs) {
val nr = r + d[0]; val nc = c + d[1]
val key = (nr.toLong() shl 32) or (nc.toLong() and 0xFFFFFFFFL)
if (lampSet.contains(key)) {
lampSet.remove(key)
rowCount[nr] = rowCount[nr]!! - 1
colCount[nc] = colCount[nc]!! - 1
diag1[nr - nc] = diag1[nr - nc]!! - 1
diag2[nr + nc] = diag2[nr + nc]!! - 1
}
}
} else {
ans[i] = 0
}
}
return ans
}
}
62 changes: 62 additions & 0 deletions tests/1001-1500/1001. grid-illumination/sol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from collections import defaultdict
from typing import List


class Solution(object):
def gridIllumination(self, n, lamps, queries):
# Hash maps to track the number of active lamps illuminating each line
row_count = defaultdict(int)
col_count = defaultdict(int)
diagonal1 = defaultdict(int) # Major diagonals (row - col)
diagonal2 = defaultdict(int) # Minor diagonals (row + col)

# Hash set to quickly verify the exact coordinate presence of an active lamp
lamp_set = set()
ans = []

# Relative coordinates defining a 3x3 window centered around a query cell
directions = {
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 0),
(0, 1),
(1, -1),
(1, 0),
(1, 1),
}

# Phase 1: Record all lamp placements and light up corresponding paths
for row, col in lamps:
if (row, col) not in lamp_set:
lamp_set.add((row, col))
row_count[row] += 1
col_count[col] += 1
diagonal1[row - col] += 1
diagonal2[row + col] += 1

# Phase 2: Process illumination queries sequentially
for row, col in queries:
# Check if any active line counter covers this position
if (
row_count[row]
or col_count[col]
or diagonal1[row - col]
or diagonal2[row + col]
):
ans.append(1)

# Turn off lamps located in the surrounding 3x3 area
for c_row, c_col in directions:
n_row, n_col = row + c_row, col + c_col
if (n_row, n_col) in lamp_set:
lamp_set.remove((n_row, n_col))
row_count[n_row] -= 1
col_count[n_col] -= 1
diagonal1[n_row - n_col] -= 1
diagonal2[n_row + n_col] -= 1
else:
ans.append(0)

return ans
Loading
Loading