Skip to content

Commit 99f9d7b

Browse files
committed
Additional questions
1 parent e0bf2de commit 99f9d7b

12 files changed

+205
-23
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.vscode/
22
temp.py
3-
tools/
3+
tools/
4+
5+
temp/

README.md

+26-22
Large diffs are not rendered by default.
File renamed without changes.
File renamed without changes.
File renamed without changes.

my-submissions/m1905.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
3+
def helper(r: int, c: int, voidIt: bool) -> bool :
4+
if not (0 <= r < len(grid1)) or not (0 <= c < len(grid1[0])) :
5+
return True
6+
7+
if not grid2[r][c] :
8+
return True
9+
10+
grid2[r][c] = 0
11+
12+
if not grid1[r][c] or voidIt :
13+
helper(r, c + 1, True)
14+
helper(r, c - 1, True)
15+
helper(r + 1, c, True)
16+
helper(r - 1, c, True)
17+
18+
return False
19+
20+
temp = helper(r, c + 1, False)
21+
temp &= helper(r, c - 1, False)
22+
temp &= helper(r + 1, c, False)
23+
temp &= helper(r - 1, c, False)
24+
return temp
25+
26+
counter = 0
27+
for r in range(len(grid1)) :
28+
for c in range(len(grid1[0])) :
29+
if grid2[r][c] and helper(r, c, False) :
30+
counter += 1
31+
32+
return counter

my-submissions/m1992.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public int[][] findFarmland(int[][] land) {
3+
ArrayList<int[]> output = new ArrayList<>();
4+
5+
for (int r = 0; r < land.length; r++) {
6+
for (int c = 0; c < land[0].length; c++) {
7+
if (land[r][c] == 1 // Corner found
8+
&& (r == 0 || land[r - 1][c] == 0)
9+
&& (c == 0 || land[r][c - 1] == 0)) {
10+
int[] group = new int[4];
11+
group[0] = r;
12+
group[1] = c;
13+
14+
for (int i = r + 1;; i++) {
15+
if (i >= land.length || land[i][c] == 0) {
16+
group[2] = i - 1;
17+
break;
18+
}
19+
}
20+
for (int i = c + 1;; i++) {
21+
if (i >= land[0].length || land[r][i] == 0) {
22+
group[3] = i - 1;
23+
break;
24+
}
25+
}
26+
27+
output.add(group);
28+
}
29+
}
30+
}
31+
32+
int[][] actualOutput = new int[output.size()][];
33+
for (int i = 0; i < output.size(); i++) {
34+
actualOutput[i] = output.get(i);
35+
}
36+
37+
return actualOutput;
38+
}
39+
}

my-submissions/m1992.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def findFarmland(self, land: List[List[int]]) -> List[List[int]]:
3+
output:List[List[int]] = []
4+
5+
for r in range(len(land)) :
6+
for c in range(len(land[0])) :
7+
if land[r][c] \
8+
and (r == 0 or not land[r - 1][c]) \
9+
and (c == 0 or not land[r][c - 1]) :
10+
output.append([r, c, r, c])
11+
12+
rRight, cRight = r, c
13+
while True :
14+
if rRight >= len(land) or land[rRight][c] == 0 :
15+
output[-1][2] = rRight - 1
16+
break
17+
rRight += 1
18+
while True :
19+
if cRight >= len(land[0]) or land[r][cRight] == 0 :
20+
output[-1][3] = cRight - 1
21+
break
22+
cRight += 1
23+
24+
return output
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def countBattleships(self, board: List[List[str]]) -> int:
3+
counter = 0
4+
5+
# find corners
6+
for r in range(len(board)) :
7+
for c in range(len(board[0])) :
8+
if board[r][c] == 'X' \
9+
and (c == 0 or board[r][c - 1] == '.') \
10+
and (r == 0 or board[r - 1][c] == '.'):
11+
counter += 1
12+
13+
return counter

my-submissions/m419 v2.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int countBattleships(char** board, int boardSize, int* boardColSize) {
2+
int counter = 0;
3+
4+
for (int r = 0; r < boardSize; r++) {
5+
for (int c = 0; c < boardColSize[0]; c++) {
6+
if (board[r][c] == 'X'
7+
&& (c == 0 || board[r][c - 1] == '.')
8+
&& (r == 0 || board[r - 1][c] == '.'))
9+
counter++;
10+
}
11+
}
12+
return counter;
13+
}
14+

my-submissions/m419.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
void removeShip(char** board, int boardSize, int* boardColSize, int x, int y) {
2+
if (x < 0 || x >= boardSize || y < 0 || y >= boardColSize[0] || board[x][y] == '.') {
3+
return;
4+
}
5+
6+
board[x][y] = '.';
7+
8+
removeShip(board, boardSize, boardColSize, x + 1, y);
9+
removeShip(board, boardSize, boardColSize, x - 1, y);
10+
removeShip(board, boardSize, boardColSize, x, y + 1);
11+
removeShip(board, boardSize, boardColSize, x, y - 1);
12+
}
13+
14+
int countBattleships(char** board, int boardSize, int* boardColSize) {
15+
int counter = 0;
16+
17+
for (int i = 0; i < boardSize; i++) {
18+
for (int j = 0; j < boardColSize[0]; j++) {
19+
if (board[i][j] == 'X') {
20+
counter++;
21+
removeShip(board, boardSize, boardColSize, i, j);
22+
}
23+
}
24+
}
25+
return counter;
26+
}

my-submissions/m695.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
int getAreaAndVoid(int** grid, int gridSize, int* gridColSize, int x, int y) {
2+
if (x < 0 || y < 0 || x >= gridSize || y >= gridColSize[0] || grid[x][y] == 0) {
3+
return 0;
4+
}
5+
6+
grid[x][y] = 0;
7+
8+
return 1 + getAreaAndVoid(grid, gridSize, gridColSize, x + 1, y)
9+
+ getAreaAndVoid(grid, gridSize, gridColSize, x - 1, y)
10+
+ getAreaAndVoid(grid, gridSize, gridColSize, x, y + 1)
11+
+ getAreaAndVoid(grid, gridSize, gridColSize, x, y - 1);
12+
}
13+
14+
int maxAreaOfIsland(int** grid, int gridSize, int* gridColSize) {
15+
int max = 0;
16+
17+
for (int i = 0; i < gridSize; i++) {
18+
for (int j = 0; j < gridColSize[0]; j++) {
19+
if (grid[i][j]) {
20+
int islandArea = getAreaAndVoid(grid, gridSize, gridColSize, i, j);
21+
max = (islandArea > max) ? islandArea : max;
22+
}
23+
}
24+
}
25+
return max;
26+
27+
28+
}

0 commit comments

Comments
 (0)