Skip to content

Commit 1788627

Browse files
committed
Added javascript tasks
1 parent b6e972b commit 1788627

File tree

42 files changed

+1480
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1480
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class TreeNode { // NOSONAR
2+
constructor(val = 0, left = null, right = null) {
3+
this.val = val
4+
this.left = left
5+
this.right = right
6+
}
7+
};
8+
9+
function createTreeNode(treeValues) {
10+
if (treeValues.length === 0) return null
11+
12+
const root = new TreeNode(treeValues[0])
13+
const queue = [root]
14+
let i = 1
15+
16+
while (i < treeValues.length) {
17+
const curr = queue.shift()
18+
if (treeValues[i] !== null) {
19+
curr.left = new TreeNode(treeValues[i])
20+
queue.push(curr.left)
21+
}
22+
i++
23+
if (i < treeValues.length && treeValues[i] !== null) {
24+
curr.right = new TreeNode(treeValues[i])
25+
queue.push(curr.right)
26+
}
27+
i++
28+
}
29+
30+
return root
31+
};
32+
33+
export { TreeNode, createTreeNode }

src/main/js/g0001_0100/s0021_merge_two_sorted_lists/solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// #Level_1_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(m+n)_Space_O(m+n)
44
// #2024_12_04_Time_0_ms_(100.00%)_Space_52.3_MB_(20.64%)
55

6-
import { ListNode } from "../../com_github_leetcode/listnode";
6+
import { ListNode } from '../../com_github_leetcode/listnode';
77

88
/**
99
* Definition for singly-linked list.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
56\. Merge Intervals
2+
3+
Medium
4+
5+
Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return _an array of the non-overlapping intervals that cover all the intervals in the input_.
6+
7+
**Example 1:**
8+
9+
**Input:** intervals = [[1,3],[2,6],[8,10],[15,18]]
10+
11+
**Output:** [[1,6],[8,10],[15,18]]
12+
13+
**Explanation:** Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
14+
15+
**Example 2:**
16+
17+
**Input:** intervals = [[1,4],[4,5]]
18+
19+
**Output:** [[1,5]]
20+
21+
**Explanation:** Intervals [1,4] and [4,5] are considered overlapping.
22+
23+
**Constraints:**
24+
25+
* <code>1 <= intervals.length <= 10<sup>4</sup></code>
26+
* `intervals[i].length == 2`
27+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting
2+
// #Data_Structure_II_Day_2_Array #Level_2_Day_17_Interval #Udemy_2D_Arrays/Matrix
3+
// #Big_O_Time_O(n_log_n)_Space_O(n) #2024_12_10_Time_7_ms_(81.68%)_Space_59.2_MB_(41.78%)
4+
5+
/**
6+
* @param {number[][]} intervals
7+
* @return {number[][]}
8+
*/
9+
var merge = function(intervals) {
10+
// Sort intervals based on the starting points
11+
intervals.sort((a, b) => a[0] - b[0])
12+
13+
const result = []
14+
let current = intervals[0]
15+
result.push(current)
16+
17+
for (const next of intervals) {
18+
if (current[1] >= next[0]) {
19+
// Merge intervals
20+
current[1] = Math.max(current[1], next[1])
21+
} else {
22+
// Move to the next interval
23+
current = next;
24+
result.push(current)
25+
}
26+
}
27+
28+
return result;
29+
};
30+
31+
export { merge }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
62\. Unique Paths
2+
3+
Medium
4+
5+
There is a robot on an `m x n` grid. The robot is initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m - 1][n - 1]`). The robot can only move either down or right at any point in time.
6+
7+
Given the two integers `m` and `n`, return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
8+
9+
The test cases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
14+
15+
**Input:** m = 3, n = 7
16+
17+
**Output:** 28
18+
19+
**Example 2:**
20+
21+
**Input:** m = 3, n = 2
22+
23+
**Output:** 3
24+
25+
**Explanation:** From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
26+
27+
1. Right -> Down -> Down
28+
29+
2. Down -> Down -> Right
30+
31+
3. Down -> Right -> Down
32+
33+
**Constraints:**
34+
35+
* `1 <= m, n <= 100`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math
2+
// #Combinatorics #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_15
3+
// #Level_1_Day_11_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
4+
// #2024_12_10_Time_0_ms_(100.00%)_Space_49.1_MB_(57.14%)
5+
6+
/**
7+
* @param {number} m
8+
* @param {number} n
9+
* @return {number}
10+
*/
11+
var uniquePaths = function(m, n) {
12+
// Initialize a 2D array with all values set to 0
13+
const dp = Array.from({ length: m }, () => Array(n).fill(0))
14+
15+
// Fill the first row and first column with 1
16+
for (let i = 0; i < m; i++) {
17+
dp[i][0] = 1
18+
}
19+
for (let j = 0; j < n; j++) {
20+
dp[0][j] = 1
21+
}
22+
23+
// Fill the rest of the dp table
24+
for (let i = 1; i < m; i++) {
25+
for (let j = 1; j < n; j++) {
26+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
27+
}
28+
}
29+
30+
// The answer is in the bottom-right corner
31+
return dp[m - 1][n - 1]
32+
};
33+
34+
export { uniquePaths }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
64\. Minimum Path Sum
2+
3+
Medium
4+
5+
Given a `m x n` `grid` filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
6+
7+
**Note:** You can only move either down or right at any point in time.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)
12+
13+
**Input:** grid = [[1,3,1],[1,5,1],[4,2,1]]
14+
15+
**Output:** 7
16+
17+
**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
18+
19+
**Example 2:**
20+
21+
**Input:** grid = [[1,2,3],[4,5,6]]
22+
23+
**Output:** 12
24+
25+
**Constraints:**
26+
27+
* `m == grid.length`
28+
* `n == grid[i].length`
29+
* `1 <= m, n <= 200`
30+
* `0 <= grid[i][j] <= 100`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix
2+
// #Dynamic_Programming_I_Day_16 #Udemy_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
3+
// #2024_12_10_Time_3_ms_(83.07%)_Space_51.2_MB_(74.79%)
4+
5+
/**
6+
* @param {number[][]} grid
7+
* @return {number}
8+
*/
9+
var minPathSum = function(grid) {
10+
const rows = grid.length
11+
const cols = grid[0].length
12+
13+
// Handle the special case where grid has only one cell
14+
if (rows === 1 && cols === 1) {
15+
return grid[0][0]
16+
}
17+
18+
// Create a 2D array for dynamic programming
19+
const dm = Array.from({ length: rows }, () => Array(cols).fill(0))
20+
21+
// Initialize the last column
22+
let s = 0
23+
for (let r = rows - 1; r >= 0; r--) {
24+
dm[r][cols - 1] = grid[r][cols - 1] + s
25+
s += grid[r][cols - 1]
26+
}
27+
28+
// Initialize the last row
29+
s = 0
30+
for (let c = cols - 1; c >= 0; c--) {
31+
dm[rows - 1][c] = grid[rows - 1][c] + s
32+
s += grid[rows - 1][c]
33+
}
34+
35+
// Recursive helper function
36+
const recur = (r, c) => {
37+
if (
38+
dm[r][c] === 0 &&
39+
r !== rows - 1 &&
40+
c !== cols - 1
41+
) {
42+
dm[r][c] =
43+
grid[r][c] +
44+
Math.min(
45+
recur(r + 1, c),
46+
recur(r, c + 1)
47+
)
48+
}
49+
return dm[r][c]
50+
}
51+
52+
// Start recursion from the top-left corner
53+
return recur(0, 0)
54+
};
55+
56+
export { minPathSum }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
70\. Climbing Stairs
2+
3+
Easy
4+
5+
You are climbing a staircase. It takes `n` steps to reach the top.
6+
7+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
8+
9+
**Example 1:**
10+
11+
**Input:** n = 2
12+
13+
**Output:** 2
14+
15+
**Explanation:** There are two ways to climb to the top.
16+
17+
1. 1 step + 1 step
18+
19+
2. 2 steps
20+
21+
**Example 2:**
22+
23+
**Input:** n = 3
24+
25+
**Output:** 3
26+
27+
**Explanation:** There are three ways to climb to the top.
28+
29+
1. 1 step + 1 step + 1 step
30+
31+
2. 1 step + 2 steps
32+
33+
3. 2 steps + 1 step
34+
35+
**Constraints:**
36+
37+
* `1 <= n <= 45`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math #Memoization
2+
// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_2
3+
// #Level_1_Day_10_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n)
4+
// #2024_12_10_Time_0_ms_(100.00%)_Space_49_MB_(22.45%)
5+
6+
/**
7+
* @param {number} n
8+
* @return {number}
9+
*/
10+
var climbStairs = function(n) {
11+
if (n < 2) {
12+
return n
13+
}
14+
15+
// Create a cache (DP array) to store results
16+
const cache = new Array(n)
17+
18+
// Initialize base cases
19+
cache[0] = 1
20+
cache[1] = 2
21+
22+
// Fill the cache using the recurrence relation
23+
for (let i = 2; i < n; i++) {
24+
cache[i] = cache[i - 1] + cache[i - 2]
25+
}
26+
27+
// Return the result for the nth step
28+
return cache[n - 1]
29+
};
30+
31+
export { climbStairs }

0 commit comments

Comments
 (0)