Skip to content

Commit

Permalink
Add Explanations
Browse files Browse the repository at this point in the history
  • Loading branch information
Superklok committed Dec 20, 2023
1 parent d9657b3 commit c4f30ad
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
24 changes: 24 additions & 0 deletions JavaScriptFlippingAnImage.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 61,28 @@ To invert an image means that each `0` is replaced by `1`, and each `1` is repla
<br/>
`};`
<br/>
<br/>

## Explanation:

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.
<br/>

The function begins by iterating through each row of the image using a `for` loop.
<br/>

Inside the loop, the current row of the image is reversed using the `reverse()` method. This reverses the order of the elements in the row, effectively flipping the image horizontally.
<br/>

Then, the `map()` method is used to iterate through each element in the reversed row. For each element, an arrow function `(x => 1 - x)` is applied. This function subtracts the element from 1, effectively inverting the color. Assuming the elements in the image are either 0 or 1, this operation will switch the value from 0 to 1 or from 1 to 0.
<br/>

The modified row is then assigned back to the `image[row]` to update the image with the flipped and inverted row.
<br/>

After the loop finishes, the modified image is returned as the output of the function.
<br/>

In summary, this function takes an image as input and performs two operations on each row: it reverses the order of the elements to flip the image horizontally, and it inverts the colors of each pixel. The modified image is then returned as the output of the function.
<br/>
<br/>
33 changes: 33 additions & 0 deletions JavaScriptNumberOfIslands.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 105,37 @@ An island is surrounded by water and is formed by connecting adjacent lands hori
<br/>
`};`
<br/>
<br/>

## Explanation:

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

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.
<br/>

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.
<br/>

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.
<br/>

Within the `dfs` function, there is an initial check to see if the current row or column indices are out of bounds of the grid. If they are, the function returns.
<br/>

Next, the value of the current cell is retrieved from the grid.
<br/>

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.
<br/>

This recursive exploration continues until all connected cells of the current island are visited.
<br/>

After the DFS traversal is complete, the `numIslands` function returns the final count of islands.
<br/>

In summary, I've utilized a depth-first search algorithm to traverse the grid and count the number of islands. The `numIslands` function iterates through each cell, marking visited cells and exploring adjacent cells using the `dfs` function. The `dfs` function recursively explores connected cells until all islands are visited. The final count of islands is returned as the output of the `numIslands` function.
<br/>
<br/>
24 changes: 24 additions & 0 deletions JavaScriptSortArrayByParity.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 50,28 @@ Return any array that satisfies this condition.
<br/>
`};`
<br/>
<br/>

## Explanation:

I've coded a function called `sortArrayByParity` that takes an array of numbers, `nums`, as input. The purpose of this function is to rearrange the elements of the input array such that all even numbers appear before odd numbers, while maintaining the relative order of even and odd numbers within their respective groups.
<br/>

The function begins by initializing a variable called `oddIndex` with a value of `0`. This variable will keep track of the index where the next odd number should be placed in the array.
<br/>

Next, a `for` loop is used to iterate through each element of the input array. Within the loop, an `if` statement checks if the current number `(nums[i])` is even by checking if the remainder of dividing it by `2` is `0`.
<br/>

If the number is even, a destructuring assignment is used to swap the current number with the number at the `oddIndex`. This effectively moves the even number to its correct position in the array, ensuring that all even numbers appear before odd numbers.
<br/>

After the swap, the `oddIndex` is incremented by `1` to prepare for the next odd number that will be encountered.
<br/>

Once the loop finishes, the modified array is returned as the output of the function.
<br/>

In summary, this function rearranges the elements of the input array such that all even numbers come before odd numbers, while preserving the relative order of even and odd numbers within their respective groups.
<br/>
<br/>
33 changes: 33 additions & 0 deletions JavaScriptTransposeMatrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 58,37 @@ The transpose of a matrix is the matrix flipped over its main diagonal, switchin
<br/>
`};`
<br/>
<br/>

## Explanation:

I've defined a function called `transpose` that takes a matrix, represented by a 2-dimensional array, as input. The purpose of this function is to transpose the matrix, which means to swap the rows and columns, and return the transposed matrix.
<br/>

Inside the function, an empty array called `result` is created to store the transposed matrix.
<br/>

A `for` loop is used to iterate over the columns of the input matrix. This loop runs for the length of the first row of the matrix, assuming all rows have the same length.
<br/>

Inside the loop, a new empty array called `currentColumn` is created to store the current column of the transposed matrix.
<br/>

Another nested `for` loop is used to iterate over the rows of the input matrix. This loop runs for the length of the matrix.
<br/>

Inside the nested loop, the value at the current row and column of the input matrix is pushed into the `currentColumn` array.
<br/>

After the nested loop completes, the `currentColumn` array containing the transposed column is pushed into the `result` array.
<br/>

The outer loop continues until all columns of the input matrix are processed.
<br/>

Finally, the `result` array containing the transposed matrix is returned as the output of the function.
<br/>

In summary, this function transposes a given matrix by swapping the rows and columns. It achieves this by iterating over the columns and rows of the input matrix and constructing a new array representing the transposed matrix. The resulting transposed matrix is then returned as the output of the function.
<br/>
<br/>
27 changes: 27 additions & 0 deletions JavaScriptTwoSum.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 68,31 @@ Only one valid answer exists.
<br/>
`};`
<br/>
<br/>

## Explanation:

I've built a function called `twoSum` that takes in an array of numbers (`nums`) and a target number. The purpose of this function is to find a pair of numbers in the array that add up to the target and return their indices in an array. If no such pair is found, it returns `[-1, -1]`.
<br/>

Inside the function, an empty object called `numsObject` is initialized.
<br/>

A `for` loop is used to iterate through each element in the `nums` array. Within the loop, the current element is assigned to a variable `n`, and the difference between the target and `n` is calculated and assigned to a variable `compliment`.
<br/>

An `if` statement checks if the `numsObject` object has a property with the value of `compliment`. If such a property exists, it means that a pair of numbers that add up to the target has been found.
<br/>

In this case, the function returns an array containing the current index ( `i` ) and the index of the compliment in the `numsObject` object.
<br/>

If the compliment property does not exist, it means that the current number has not been encountered before. In that case, the current number (`n`) is added as a property to the `numsObject` object, with the value of the current index (`i`).
<br/>

After the loop finishes, if no pair of numbers that add up to the target is found, the function returns `[-1, -1]`.
<br/>

In summary, this function searches for a pair of numbers in the input array that add up to the target. It uses an object to store previously encountered numbers and their indices. The function iterates through the array, checking if the compliment of each number exists in the object. If a pair is found, their indices are returned. If no pair is found, `[-1, -1]` is returned.
<br/>
<br/>

0 comments on commit c4f30ad

Please sign in to comment.