Skip to content

Commit 6fc4358

Browse files
authored
batch of tests (#50)
1 parent f3a9fd0 commit 6fc4358

3,552 files changed

Lines changed: 332832 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/1001-1500/1001. grid-illumination/manifest.yaml

Lines changed: 1040 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
#include <unordered_set>
4+
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
vector<int> gridIllumination(int n, vector<vector<int>>& lamps, vector<vector<int>>& queries) {
10+
unordered_map<int, int> rowCount, colCount, diag1, diag2;
11+
unordered_set<long long> lampSet;
12+
vector<int> ans(queries.size());
13+
int dirs[9][2] = {{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,0}, {0,1}, {1,-1}, {1,0}, {1,1}};
14+
15+
for (auto& lamp : lamps) {
16+
int r = lamp[0], c = lamp[1];
17+
long long key = ((long long)r << 32) | (c & 0xFFFFFFFFLL);
18+
if (!lampSet.count(key)) {
19+
lampSet.insert(key);
20+
rowCount[r]++;
21+
colCount[c]++;
22+
diag1[r - c]++;
23+
diag2[r + c]++;
24+
}
25+
}
26+
27+
for (int i = 0; i < queries.size(); i++) {
28+
int r = queries[i][0], c = queries[i][1];
29+
if (rowCount[r] > 0 || colCount[c] > 0 || diag1[r - c] > 0 || diag2[r + c] > 0) {
30+
ans[i] = 1;
31+
for (auto& d : dirs) {
32+
int nr = r + d[0], nc = c + d[1];
33+
long long key = ((long long)nr << 32) | (nc & 0xFFFFFFFFLL);
34+
if (lampSet.count(key)) {
35+
lampSet.erase(key);
36+
rowCount[nr]--;
37+
colCount[nc]--;
38+
diag1[nr - nc]--;
39+
diag2[nr + nc]--;
40+
}
41+
}
42+
} else {
43+
ans[i] = 0;
44+
}
45+
}
46+
return ans;
47+
}
48+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public class Solution {
5+
public int[] GridIllumination(int n, int[][] lamps, int[][] queries) {
6+
Dictionary<int, int> rowCount = new Dictionary<int, int>();
7+
Dictionary<int, int> colCount = new Dictionary<int, int>();
8+
Dictionary<int, int> diag1 = new Dictionary<int, int>();
9+
Dictionary<int, int> diag2 = new Dictionary<int, int>();
10+
HashSet<(int, int)> lampSet = new HashSet<(int, int)>();
11+
int[] ans = new int[queries.Length];
12+
int[][] dirs = new int[][] {
13+
new int[] {-1,-1}, new int[] {-1,0}, new int[] {-1,1},
14+
new int[] {0,-1}, new int[] {0,0}, new int[] {0,1},
15+
new int[] {1,-1}, new int[] {1,0}, new int[] {1,1}
16+
};
17+
18+
foreach (var lamp in lamps) {
19+
int r = lamp[0], c = lamp[1];
20+
var key = (r, c);
21+
if (!lampSet.Contains(key)) {
22+
lampSet.Add(key);
23+
rowCount[r] = rowCount.GetValueOrDefault(r, 0) + 1;
24+
colCount[c] = colCount.GetValueOrDefault(c, 0) + 1;
25+
diag1[r - c] = diag1.GetValueOrDefault(r - c, 0) + 1;
26+
diag2[r + c] = diag2.GetValueOrDefault(r + c, 0) + 1;
27+
}
28+
}
29+
30+
for (int i = 0; i < queries.Length; i++) {
31+
int r = queries[i][0], c = queries[i][1];
32+
if (rowCount.GetValueOrDefault(r, 0) > 0 || colCount.GetValueOrDefault(c, 0) > 0 ||
33+
diag1.GetValueOrDefault(r - c, 0) > 0 || diag2.GetValueOrDefault(r + c, 0) > 0) {
34+
ans[i] = 1;
35+
foreach (var d in dirs) {
36+
int nr = r + d[0], nc = c + d[1];
37+
var key = (nr, nc);
38+
if (lampSet.Contains(key)) {
39+
lampSet.Remove(key);
40+
rowCount[nr]--;
41+
colCount[nc]--;
42+
diag1[nr - nc]--;
43+
diag2[nr + nc]--;
44+
}
45+
}
46+
} else {
47+
ans[i] = 0;
48+
}
49+
}
50+
return ans;
51+
}
52+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'dart:collection';
2+
3+
class Solution {
4+
List<int> gridIllumination(int n, List<List<int>> lamps, List<List<int>> queries) {
5+
Map<int, int> rowCount = HashMap<int, int>();
6+
Map<int, int> colCount = HashMap<int, int>();
7+
Map<int, int> diag1 = HashMap<int, int>();
8+
Map<int, int> diag2 = HashMap<int, int>();
9+
Set<String> lampSet = HashSet<String>();
10+
List<int> ans = List<int>.filled(queries.length, 0);
11+
List<List<int>> dirs = [
12+
[-1,-1], [-1,0], [-1,1],
13+
[0,-1], [0,0], [0,1],
14+
[1,-1], [1,0], [1,1]
15+
];
16+
17+
for (var lamp in lamps) {
18+
int r = lamp[0], c = lamp[1];
19+
var key = '$r,$c';
20+
if (!lampSet.contains(key)) {
21+
lampSet.add(key);
22+
rowCount[r] = (rowCount[r] ?? 0) + 1;
23+
colCount[c] = (colCount[c] ?? 0) + 1;
24+
diag1[r - c] = (diag1[r - c] ?? 0) + 1;
25+
diag2[r + c] = (diag2[r + c] ?? 0) + 1;
26+
}
27+
}
28+
29+
for (int i = 0; i < queries.length; i++) {
30+
int r = queries[i][0], c = queries[i][1];
31+
if ((rowCount[r] ?? 0) > 0 || (colCount[c] ?? 0) > 0 ||
32+
(diag1[r - c] ?? 0) > 0 || (diag2[r + c] ?? 0) > 0) {
33+
ans[i] = 1;
34+
for (var d in dirs) {
35+
int nr = r + d[0], nc = c + d[1];
36+
var key = '$nr,$nc';
37+
if (lampSet.contains(key)) {
38+
lampSet.remove(key);
39+
rowCount[nr] = rowCount[nr]! - 1;
40+
colCount[nc] = colCount[nc]! - 1;
41+
diag1[nr - nc] = diag1[nr - nc]! - 1;
42+
diag2[nr + nc] = diag2[nr + nc]! - 1;
43+
}
44+
}
45+
} else {
46+
ans[i] = 0;
47+
}
48+
}
49+
return ans;
50+
}
51+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
func gridIllumination(n int, lamps [][]int, queries [][]int) []int {
2+
rowCount := make(map[int]int)
3+
colCount := make(map[int]int)
4+
diag1 := make(map[int]int)
5+
diag2 := make(map[int]int)
6+
lampSet := make(map[[2]int]bool)
7+
ans := make([]int, len(queries))
8+
dirs := [][2]int{{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,0}, {0,1}, {1,-1}, {1,0}, {1,1}}
9+
10+
for _, lamp := range lamps {
11+
r, c := lamp[0], lamp[1]
12+
key := [2]int{r, c}
13+
if !lampSet[key] {
14+
lampSet[key] = true
15+
rowCount[r]++
16+
colCount[c]++
17+
diag1[r - c]++
18+
diag2[r + c]++
19+
}
20+
}
21+
22+
for i, q := range queries {
23+
r, c := q[0], q[1]
24+
if rowCount[r] > 0 || colCount[c] > 0 || diag1[r - c] > 0 || diag2[r + c] > 0 {
25+
ans[i] = 1
26+
for _, d := range dirs {
27+
nr, nc := r + d[0], c + d[1]
28+
key := [2]int{nr, nc}
29+
if lampSet[key] {
30+
delete(lampSet, key)
31+
rowCount[nr]--
32+
colCount[nc]--
33+
diag1[nr - nc]--
34+
diag2[nr + nc]--
35+
}
36+
}
37+
} else {
38+
ans[i] = 0
39+
}
40+
}
41+
return ans
42+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
5+
Map<Integer, Integer> rowCount = new HashMap<>();
6+
Map<Integer, Integer> colCount = new HashMap<>();
7+
Map<Integer, Integer> diag1 = new HashMap<>();
8+
Map<Integer, Integer> diag2 = new HashMap<>();
9+
Set<Long> lampSet = new HashSet<>();
10+
int[] ans = new int[queries.length];
11+
int[][] dirs = {{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,0}, {0,1}, {1,-1}, {1,0}, {1,1}};
12+
13+
for (int[] lamp : lamps) {
14+
int r = lamp[0], c = lamp[1];
15+
long key = (long)r << 32 | (c & 0xFFFFFFFFL);
16+
if (!lampSet.contains(key)) {
17+
lampSet.add(key);
18+
rowCount.merge(r, 1, Integer::sum);
19+
colCount.merge(c, 1, Integer::sum);
20+
diag1.merge(r - c, 1, Integer::sum);
21+
diag2.merge(r + c, 1, Integer::sum);
22+
}
23+
}
24+
25+
for (int i = 0; i < queries.length; i++) {
26+
int r = queries[i][0], c = queries[i][1];
27+
if (rowCount.getOrDefault(r, 0) > 0 ||
28+
colCount.getOrDefault(c, 0) > 0 ||
29+
diag1.getOrDefault(r - c, 0) > 0 ||
30+
diag2.getOrDefault(r + c, 0) > 0) {
31+
ans[i] = 1;
32+
for (int[] d : dirs) {
33+
int nr = r + d[0], nc = c + d[1];
34+
long key = (long)nr << 32 | (nc & 0xFFFFFFFFL);
35+
if (lampSet.contains(key)) {
36+
lampSet.remove(key);
37+
rowCount.merge(nr, -1, Integer::sum);
38+
colCount.merge(nc, -1, Integer::sum);
39+
diag1.merge(nr - nc, -1, Integer::sum);
40+
diag2.merge(nr + nc, -1, Integer::sum);
41+
}
42+
}
43+
} else {
44+
ans[i] = 0;
45+
}
46+
}
47+
return ans;
48+
}
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import kotlin.collections.HashMap
2+
import kotlin.collections.HashSet
3+
4+
class Solution {
5+
fun gridIllumination(n: Int, lamps: Array<IntArray>, queries: Array<IntArray>): IntArray {
6+
val rowCount = HashMap<Int, Int>()
7+
val colCount = HashMap<Int, Int>()
8+
val diag1 = HashMap<Int, Int>()
9+
val diag2 = HashMap<Int, Int>()
10+
val lampSet = HashSet<Long>()
11+
val ans = IntArray(queries.size)
12+
val dirs = arrayOf(intArrayOf(-1,-1), intArrayOf(-1,0), intArrayOf(-1,1),
13+
intArrayOf(0,-1), intArrayOf(0,0), intArrayOf(0,1),
14+
intArrayOf(1,-1), intArrayOf(1,0), intArrayOf(1,1))
15+
16+
for (lamp in lamps) {
17+
val r = lamp[0]; val c = lamp[1]
18+
val key = (r.toLong() shl 32) or (c.toLong() and 0xFFFFFFFFL)
19+
if (!lampSet.contains(key)) {
20+
lampSet.add(key)
21+
rowCount[r] = rowCount.getOrDefault(r, 0) + 1
22+
colCount[c] = colCount.getOrDefault(c, 0) + 1
23+
diag1[r - c] = diag1.getOrDefault(r - c, 0) + 1
24+
diag2[r + c] = diag2.getOrDefault(r + c, 0) + 1
25+
}
26+
}
27+
28+
for (i in queries.indices) {
29+
val r = queries[i][0]; val c = queries[i][1]
30+
if ((rowCount[r] ?: 0) > 0 || (colCount[c] ?: 0) > 0 ||
31+
(diag1[r - c] ?: 0) > 0 || (diag2[r + c] ?: 0) > 0) {
32+
ans[i] = 1
33+
for (d in dirs) {
34+
val nr = r + d[0]; val nc = c + d[1]
35+
val key = (nr.toLong() shl 32) or (nc.toLong() and 0xFFFFFFFFL)
36+
if (lampSet.contains(key)) {
37+
lampSet.remove(key)
38+
rowCount[nr] = rowCount[nr]!! - 1
39+
colCount[nc] = colCount[nc]!! - 1
40+
diag1[nr - nc] = diag1[nr - nc]!! - 1
41+
diag2[nr + nc] = diag2[nr + nc]!! - 1
42+
}
43+
}
44+
} else {
45+
ans[i] = 0
46+
}
47+
}
48+
return ans
49+
}
50+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from collections import defaultdict
2+
from typing import List
3+
4+
5+
class Solution(object):
6+
def gridIllumination(self, n, lamps, queries):
7+
# Hash maps to track the number of active lamps illuminating each line
8+
row_count = defaultdict(int)
9+
col_count = defaultdict(int)
10+
diagonal1 = defaultdict(int) # Major diagonals (row - col)
11+
diagonal2 = defaultdict(int) # Minor diagonals (row + col)
12+
13+
# Hash set to quickly verify the exact coordinate presence of an active lamp
14+
lamp_set = set()
15+
ans = []
16+
17+
# Relative coordinates defining a 3x3 window centered around a query cell
18+
directions = {
19+
(-1, -1),
20+
(-1, 0),
21+
(-1, 1),
22+
(0, -1),
23+
(0, 0),
24+
(0, 1),
25+
(1, -1),
26+
(1, 0),
27+
(1, 1),
28+
}
29+
30+
# Phase 1: Record all lamp placements and light up corresponding paths
31+
for row, col in lamps:
32+
if (row, col) not in lamp_set:
33+
lamp_set.add((row, col))
34+
row_count[row] += 1
35+
col_count[col] += 1
36+
diagonal1[row - col] += 1
37+
diagonal2[row + col] += 1
38+
39+
# Phase 2: Process illumination queries sequentially
40+
for row, col in queries:
41+
# Check if any active line counter covers this position
42+
if (
43+
row_count[row]
44+
or col_count[col]
45+
or diagonal1[row - col]
46+
or diagonal2[row + col]
47+
):
48+
ans.append(1)
49+
50+
# Turn off lamps located in the surrounding 3x3 area
51+
for c_row, c_col in directions:
52+
n_row, n_col = row + c_row, col + c_col
53+
if (n_row, n_col) in lamp_set:
54+
lamp_set.remove((n_row, n_col))
55+
row_count[n_row] -= 1
56+
col_count[n_col] -= 1
57+
diagonal1[n_row - n_col] -= 1
58+
diagonal2[n_row + n_col] -= 1
59+
else:
60+
ans.append(0)
61+
62+
return ans

0 commit comments

Comments
 (0)