Skip to content

Commit 16bff8a

Browse files
committed
Added 3 solutions
1 parent c851bad commit 16bff8a

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int numEquivDominoPairs(int[][] dominoes) {
3+
Map<String, Integer> map = new HashMap <>();
4+
for (int i = 0; i < dominoes.length; i++) {
5+
String key = Math.min(dominoes[i][0], dominoes[i][1]) + "|" + Math.max(dominoes[i][0], dominoes[i][1]);
6+
map.put(key, map.getOrDefault(key, 0) + 1);
7+
}
8+
9+
int count = 0;
10+
for (int value : map.values()) {
11+
count += (value * (value - 1)) / 2;
12+
}
13+
14+
return count;
15+
}
16+
}

Easy/Rotting Oranges.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
3+
public int orangesRotting(int[][] grid) {
4+
Queue<Integer> queue = new LinkedList<>();
5+
Map<Integer, Integer> map = new HashMap<>();
6+
int numRows = grid.length;
7+
int numCols = grid[0].length;
8+
9+
for (int i = 0; i < numRows; i++) {
10+
for (int j = 0; j < numCols; j++) {
11+
if (grid[i][j] == 2) {
12+
int key = numCols * i + j;
13+
queue.add(key);
14+
map.put(key, 0);
15+
}
16+
}
17+
}
18+
19+
int ans = 0;
20+
while (!queue.isEmpty()) {
21+
int key = queue.remove();
22+
int x = key / numCols;
23+
int y = key % numCols;
24+
for (int[] dir : dirs) {
25+
int newX = x + dir[0];
26+
int newY = y + dir[1];
27+
if (newX < 0 || newX >= numRows || newY < 0 || newY >= numCols || grid[newX][newY] != 1) {
28+
continue;
29+
}
30+
grid[newX][newY] = 2;
31+
int newKey = newX * numCols + newY;
32+
queue.add(newKey);
33+
map.put(newKey, map.get(key) + 1);
34+
ans = map.get(newKey);
35+
}
36+
}
37+
38+
for (int i = 0; i < numRows; i++) {
39+
for (int j = 0; j < numCols; j++) {
40+
if (grid[i][j] == 1) {
41+
return -1;
42+
}
43+
}
44+
}
45+
46+
return ans;
47+
}
48+
}

Medium/Walls and Gates.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
3+
public void wallsAndGates(int[][] rooms) {
4+
if (rooms.length == 0 || rooms[0].length == 0) {
5+
return;
6+
}
7+
8+
Queue<Integer> queue = new LinkedList<>();
9+
int numRows = rooms.length;
10+
int numCols = rooms[0].length;
11+
12+
for (int i = 0; i < numRows; i++) {
13+
for (int j = 0; j < numCols; j++) {
14+
if (rooms[i][j] == 0) {
15+
int key = i * numCols + j;
16+
queue.add(key);
17+
}
18+
}
19+
}
20+
21+
while (!queue.isEmpty()) {
22+
int removed = queue.remove();
23+
int x = removed / numCols;
24+
int y = removed % numCols;
25+
26+
for (int[] dir : dirs) {
27+
int newX = x + dir[0];
28+
int newY = y + dir[1];
29+
30+
if (newX < 0 || newX >= numRows || newY < 0 || newY >= numCols || rooms[newX][newY] != 2147483647) {
31+
continue;
32+
}
33+
34+
rooms[newX][newY] = rooms[x][y] + 1;
35+
int newKey = newX * numCols + newY;
36+
queue.add(newKey);
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)