-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
# LeetCodes | ||
|
||
- LeetCodes | ||
- Data Structure and Algorithms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
interface GraphNode { | ||
value: number; | ||
neighbors: GraphNode[]; | ||
} | ||
|
||
const node1: GraphNode = { value: 1, neighbors: [] }; | ||
const node2: GraphNode = { value: 2, neighbors: [] }; | ||
const node3: GraphNode = { value: 3, neighbors: [] }; | ||
|
||
node1.neighbors.push(node2, node3); | ||
node2.neighbors.push(node1); | ||
node3.neighbors.push(node1); | ||
|
||
/** Adapting DFS for Graphs: */ | ||
function depthFirstSearchGraph( | ||
node: GraphNode, | ||
visited: Set<GraphNode> = new Set(), | ||
) { | ||
if (!node || visited.has(node)) return; | ||
|
||
console.log('value => ', node.value); | ||
visited.add(node); | ||
|
||
for (const neighbor of node.neighbors) { | ||
depthFirstSearchGraph(neighbor, visited); | ||
} | ||
} | ||
|
||
depthFirstSearchGraph(node2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Here’s a simple TypeScript example of DFS used to solve a maze: | ||
|
||
type Cell = { | ||
x: number; | ||
y: number; | ||
}; | ||
|
||
/** Maze Representation: A 2D array where 1 represents passable cells and 0 represents walls. */ | ||
const maze: number[][] = [ | ||
[1, 1, 0, 1, 0], | ||
[1, 0, 1, 0, 1], | ||
[1, 0, 1, 0, 1], | ||
[1, 1, 1, 1, 1], | ||
[0, 0, 0, 0, 1], | ||
]; | ||
|
||
const directions: Cell[] = [ | ||
{ x: 0, y: 1 }, // right | ||
{ x: 1, y: 0 }, // down | ||
{ x: 0, y: -1 }, // left | ||
{ x: -1, y: 0 }, // up | ||
]; | ||
|
||
function isValidMove(x: number, y: number): boolean { | ||
return ( | ||
x >= 0 && | ||
x < maze.length && | ||
y >= 0 && | ||
y < maze[0].length && | ||
maze[x][y] === 1 | ||
); | ||
} | ||
|
||
function depthFirstSearchMaze( | ||
start: Cell, | ||
end: Cell, | ||
path: Cell[] = [], | ||
): boolean { | ||
const { x, y } = start; | ||
|
||
if (!isValidMove(x, y) || path.some((cell) => cell.x === x && cell.y === y)) { | ||
return false; | ||
} | ||
|
||
path.push(start); | ||
|
||
if (x === end.x && y === end.y) return true; | ||
|
||
for (const direction of directions) { | ||
const newX = x + direction.x; | ||
const newY = y + direction.y; | ||
|
||
if (depthFirstSearchMaze({ x: newX, y: newY }, end, path)) { | ||
return true; | ||
} | ||
} | ||
|
||
path.pop(); | ||
return false; | ||
} | ||
|
||
const start: Cell = { x: 0, y: 0 }; | ||
const end: Cell = { x: 4, y: 4 }; | ||
const path: Cell[] = []; | ||
|
||
if (depthFirstSearchMaze(start, end, path)) { | ||
console.log('Path found:', path); | ||
} else { | ||
console.log('No path found.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// DFS is typically used to explore nodes and edges of a graph. | ||
// In this example, we'll implement DFS for a tree structure, which can be easily adapted for graphs. | ||
|
||
interface TreeNode { | ||
value: number; | ||
children: TreeNode[]; | ||
} | ||
|
||
const root: TreeNode = { | ||
value: 1, | ||
children: [ | ||
{ | ||
value: 2, | ||
children: [ | ||
{ value: 4, children: [] }, | ||
{ value: 5, children: [] }, | ||
], | ||
}, | ||
{ | ||
value: 3, | ||
children: [ | ||
{ value: 6, children: [] }, | ||
{ value: 7, children: [] }, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
/** Depth-First Search */ | ||
function depthFirstSearch(node: TreeNode) { | ||
if (!node) return; | ||
|
||
console.log('node.value', node.value); | ||
|
||
for (const child of node.children) { | ||
depthFirstSearch(child); | ||
} | ||
} | ||
|
||
depthFirstSearch(root); |
Empty file.
30 changes: 30 additions & 0 deletions
30
node-concepts/src/leetcodes/src/number-of-islands/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export abstract class NumberOfIslands { | ||
/** solution one */ | ||
public static solutionOne(grid: number[][]): number { | ||
const visited = grid.map((row) => row.map((cell) => false)); | ||
|
||
let islandCount = 0; | ||
|
||
for (let i = 0; i < grid.length; i++) { | ||
for (let j = 0; j < grid[i].length; j++) { | ||
if (this.dFS(i, j, grid, visited)) islandCount++; | ||
} | ||
} | ||
|
||
return islandCount; | ||
} | ||
|
||
/** depth first search | ||
* @private | ||
* @example | ||
* - if returns true, means, we have an island | ||
*/ | ||
private static dFS( | ||
i: number, | ||
j: number, | ||
grid: number[][], | ||
visited: boolean[][], | ||
): boolean { | ||
return true; | ||
} | ||
} |