Skip to content

Commit 494de77

Browse files
committed
Add Formatting
1 parent c4f30ad commit 494de77

5 files changed

+251
-276
lines changed

JavaScriptFlippingAnImage.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# JavaScript Flipping An Image
2-
3-
## Challenge:
1+
# JavaScript Flipping an Image
2+
<br/>
43

4+
## Challenge
55
Given an `n x n` binary matrix `image`, flip the image horizontally, then invert it, and return the resulting image.
66

77
To flip an image horizontally means that each row of the image is reversed.
@@ -14,56 +14,56 @@ To invert an image means that each `0` is replaced by `1`, and each `1` is repla
1414

1515
* For example, inverting `[0,1,1]` results in `[1,0,0]`.
1616

17-
### 1<sup>st</sup> Example:
18-
19-
`Input: image = [[1,1,0],[1,0,1],[0,0,0]]`
2017
<br/>
21-
`Output: [[1,0,0],[0,1,0],[1,1,1]]`
22-
<br/>
23-
`Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].`
24-
<br/>
25-
`Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]`
2618

27-
### 2<sup>nd</sup> Example:
19+
### 1<sup>st</sup> Example
2820

29-
`Input: image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]`
30-
<br/>
31-
`Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]`
32-
<br/>
33-
`Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].`
34-
<br/>
35-
`Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]`
21+
```JavaScript
22+
Input: image = [[1,1,0],[1,0,1],[0,0,0]]
23+
Output: [[1,0,0],[0,1,0],[1,1,1]]
24+
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]]
25+
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
26+
```
3627

37-
### Constraints:
28+
### 2<sup>nd</sup> Example
29+
30+
```JavaScript
31+
Input: image = [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
32+
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
33+
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]]
34+
Then, invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
35+
```
3836

39-
`n == image.length`
40-
<br/>
41-
`n == image[i].length`
42-
<br/>
43-
`1 <= n <= 20`
4437
<br/>
45-
`images[i][j]` is either `0` or `1`.
4638

47-
## Solution:
39+
### Constraints
40+
41+
```JavaScript
42+
n == image.length
43+
n == image[i].length
44+
1 <= n <= 20
45+
```
46+
47+
- `images[i][j]` is either `0` or `1`.
4848

49-
`const flipAndInvertImage = (image) => {`
50-
<br/>
51-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`for(let row in image) {`
52-
<br/>
53-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`image[row] = image[row].reverse();`
54-
<br/>
55-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`image[row] = image[row].map(x=>1-x);`
56-
<br/>
57-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`}`
58-
<br/>
59-
<br/>
60-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`return image;`
61-
<br/>
62-
`};`
6349
<br/>
50+
51+
## Solution
52+
53+
```JavaScript
54+
const flipAndInvertImage = (image) => {
55+
for(let row in image) {
56+
image[row] = image[row].reverse();
57+
image[row] = image[row].map(x=>1-x);
58+
}
59+
60+
return image;
61+
};
62+
```
63+
6464
<br/>
6565

66-
## Explanation:
66+
## Explanation
6767

6868
I've created a function called `flipAndInvertImage` that takes an image as input and returns the modified image. The purpose of this function is to flip the image horizontally and invert the colors of each pixel.
6969
<br/>

JavaScriptNumberOfIslands.md

Lines changed: 73 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,97 @@
1-
# JavaScript Number Of Islands
2-
3-
## Challenge:
1+
# JavaScript Number of Islands
2+
<br/>
43

4+
## Challenge
55
Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return the number of islands.
66

77
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
8-
9-
### 1<sup>st</sup> Example:
10-
11-
`Input: grid = [`
12-
<br/>
13-
`["1","1","1","1","0"],`
14-
<br/>
15-
`["1","1","0","1","0"],`
16-
<br/>
17-
`["1","1","0","0","0"],`
188
<br/>
19-
`["0","0","0","0","0"]`
209
<br/>
21-
`]`
22-
<br/>
23-
`Output: 1`
2410

25-
### 2<sup>nd</sup> Example:
11+
### 1<sup>st</sup> Example
2612

27-
`Input: grid = [`
28-
<br/>
29-
`["1","1","0","0","0"],`
30-
<br/>
31-
`["1","1","0","0","0"],`
32-
<br/>
33-
`["0","0","1","0","0"],`
34-
<br/>
35-
`["0","0","0","1","1"]`
36-
<br/>
37-
`]`
38-
<br/>
39-
`Output: 3`
13+
```JavaScript
14+
Input: grid = [
15+
['1','1','1','1','0'],
16+
['1','1','0','1','0'],
17+
['1','1','0','0','0'],
18+
['0','0','0','0','0']
19+
]
20+
Output: 1
21+
```
4022

41-
### Constraints:
23+
### 2<sup>nd</sup> Example
24+
25+
```JavaScript
26+
Input: grid = [
27+
['1','1','0','0','0'],
28+
['1','1','0','0','0'],
29+
['0','0','1','0','0'],
30+
['0','0','0','1','1']
31+
]
32+
Output: 3
33+
```
4234

43-
`m == grid.length`
44-
<br/>
45-
`n == grid[i].length`
46-
<br/>
47-
`1 <= m, n <= 300`
4835
<br/>
49-
`grid[i][j]` is `'0'` or `'1'`.
5036

51-
## Solution:
37+
### Constraints
38+
39+
```JavaScript
40+
m == grid.length
41+
n == grid[i].length
42+
1 <= m, n <= 300
43+
```
44+
45+
- `grid[i][j]` is `'0'` or `'1'`.
5246

53-
`const numIslands = (grid) => {`
54-
<br/>
55-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`let count = 0;`
56-
<br/>
57-
<br/>
58-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`for (let i = 0; i < grid.length; i++) {`
59-
<br/>
60-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`for (let j = 0; j < grid[0].length; j++) {`
61-
<br/>
62-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`if (grid[i][j] === '1') {`
63-
<br/>
64-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`count++;`
65-
<br/>
66-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`dfs(grid, i,j);`
67-
<br/>
68-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`}`
69-
<br/>
70-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`}`
71-
<br/>
72-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`}`
73-
<br/>
74-
<br/>
75-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`return count;`
76-
<br/>
77-
`};`
78-
<br/>
79-
<br/>
80-
`const dfs = (grid, row, col) => {`
81-
<br/>
82-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) {`
83-
<br/>
84-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`return;`
85-
<br/>
86-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`}`
87-
<br/>
88-
<br/>
89-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`const value = grid[row][col];`
90-
<br/>
91-
<br/>
92-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`if (value === '1') {`
93-
<br/>
94-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`grid[row][col] = '#';`
95-
<br/>
96-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`dfs(grid, row + 1, col);`
97-
<br/>
98-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`dfs(grid, row - 1, col);`
99-
<br/>
100-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`dfs(grid, row, col + 1);`
101-
<br/>
102-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`dfs(grid, row, col - 1);`
103-
<br/>
104-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`}`
105-
<br/>
106-
`};`
10747
<br/>
48+
49+
## Solution
50+
51+
```JavaScript
52+
const numIslands = (grid) => {
53+
let count = 0;
54+
55+
for (let i = 0; i < grid.length; i++) {
56+
for (let j = 0; j < grid[0].length; j++) {
57+
if (grid[i][j] === '1') {
58+
count++;
59+
dfs(grid, i,j);
60+
}
61+
}
62+
}
63+
64+
return count;
65+
};
66+
67+
const dfs = (grid, row, col) => {
68+
if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) {
69+
return;
70+
}
71+
72+
const value = grid[row][col];
73+
74+
if (value === '1') {
75+
grid[row][col] = '#';
76+
dfs(grid, row + 1, col);
77+
dfs(grid, row - 1, col);
78+
dfs(grid, row, col + 1);
79+
dfs(grid, row, col - 1);
80+
}
81+
};
82+
```
83+
10884
<br/>
10985

110-
## Explanation:
86+
## Explanation
11187

11288
I've defined two functions: `numIslands` and `dfs`.
11389
<br/>
11490

115-
The `numIslands` function takes a grid as input and returns the number of islands present in the grid. It initializes a variable `count` to 0.
91+
The `numIslands` function takes a grid as input and returns the number of islands present in the grid. It initializes a variable `count` to `0`.
11692
<br/>
11793

118-
Inside the function, there is a nested loop that iterates over each cell in the grid. If the current cell has a value of '1', indicating it is part of an island, the `count` is incremented and the `dfs` function is called with the current grid and the indices of the current cell.
94+
Inside the function, there is a nested loop that iterates over each cell in the grid. If the current cell has a value of `'1'`, indicating it is part of an island, the `count` is incremented and the `dfs` function is called with the current grid and the indices of the current cell.
11995
<br/>
12096

12197
The `dfs` function is a recursive function that performs a depth-first search on the grid. It takes the grid, row index, and column index as input.
@@ -127,7 +103,7 @@ Within the `dfs` function, there is an initial check to see if the current row o
127103
Next, the value of the current cell is retrieved from the grid.
128104
<br/>
129105

130-
If the value is '1', indicating an unvisited cell that is part of an island, the value of the cell is changed to '#' to mark it as visited. Then, the `dfs` function is called recursively for the adjacent cells: one cell below, one cell above, one cell to the right, and one cell to the left.
106+
If the value is `'1'`, indicating an unvisited cell that is part of an island, the value of the cell is changed to `'#'` to mark it as visited. Then, the `dfs` function is called recursively for the adjacent cells: one cell below, one cell above, one cell to the right, and one cell to the left.
131107
<br/>
132108

133109
This recursive exploration continues until all connected cells of the current island are visited.

0 commit comments

Comments
 (0)