Skip to content

Commit c2446d8

Browse files
committed
Added javascript tasks
1 parent 46bfcc9 commit c2446d8

File tree

21 files changed

+1521
-1
lines changed

21 files changed

+1521
-1
lines changed

src/main/js/g0001_0100/s0021_merge_two_sorted_lists/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Merge two sorted linked lists and return it as a **sorted** list. The list shoul
3636
## Solution
3737

3838
```javascript
39-
import { ListNode } from "../../com_github_leetcode/listnode";
39+
import { ListNode } from '../../com_github_leetcode/listnode';
4040

4141
/**
4242
* Definition for singly-linked list.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 56\. Merge Intervals
5+
6+
Medium
7+
8+
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_.
9+
10+
**Example 1:**
11+
12+
**Input:** intervals = \[\[1,3],[2,6],[8,10],[15,18]]
13+
14+
**Output:** [[1,6],[8,10],[15,18]]
15+
16+
**Explanation:** Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
17+
18+
**Example 2:**
19+
20+
**Input:** intervals = \[\[1,4],[4,5]]
21+
22+
**Output:** [[1,5]]
23+
24+
**Explanation:** Intervals [1,4] and [4,5] are considered overlapping.
25+
26+
**Constraints:**
27+
28+
* <code>1 <= intervals.length <= 10<sup>4</sup></code>
29+
* `intervals[i].length == 2`
30+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code>
31+
32+
## Solution
33+
34+
```javascript
35+
/**
36+
* @param {number[][]} intervals
37+
* @return {number[][]}
38+
*/
39+
var merge = function(intervals) {
40+
// Sort intervals based on the starting points
41+
intervals.sort((a, b) => a[0] - b[0])
42+
43+
const result = []
44+
let current = intervals[0]
45+
result.push(current)
46+
47+
for (const next of intervals) {
48+
if (current[1] >= next[0]) {
49+
// Merge intervals
50+
current[1] = Math.max(current[1], next[1])
51+
} else {
52+
// Move to the next interval
53+
current = next;
54+
result.push(current)
55+
}
56+
}
57+
58+
return result;
59+
};
60+
61+
export { merge }
62+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 62\. Unique Paths
5+
6+
Medium
7+
8+
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.
9+
10+
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_.
11+
12+
The test cases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
17+
18+
**Input:** m = 3, n = 7
19+
20+
**Output:** 28
21+
22+
**Example 2:**
23+
24+
**Input:** m = 3, n = 2
25+
26+
**Output:** 3
27+
28+
**Explanation:** From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
29+
30+
1. Right -> Down -> Down
31+
32+
2. Down -> Down -> Right
33+
34+
3. Down -> Right -> Down
35+
36+
**Constraints:**
37+
38+
* `1 <= m, n <= 100`
39+
40+
## Solution
41+
42+
```javascript
43+
/**
44+
* @param {number} m
45+
* @param {number} n
46+
* @return {number}
47+
*/
48+
var uniquePaths = function(m, n) {
49+
// Initialize a 2D array with all values set to 0
50+
const dp = Array.from({ length: m }, () => Array(n).fill(0))
51+
52+
// Fill the first row and first column with 1
53+
for (let i = 0; i < m; i++) {
54+
dp[i][0] = 1
55+
}
56+
for (let j = 0; j < n; j++) {
57+
dp[0][j] = 1
58+
}
59+
60+
// Fill the rest of the dp table
61+
for (let i = 1; i < m; i++) {
62+
for (let j = 1; j < n; j++) {
63+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
64+
}
65+
}
66+
67+
// The answer is in the bottom-right corner
68+
return dp[m - 1][n - 1]
69+
};
70+
71+
export { uniquePaths }
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 64\. Minimum Path Sum
5+
6+
Medium
7+
8+
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.
9+
10+
**Note:** You can only move either down or right at any point in time.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)
15+
16+
**Input:** grid = \[\[1,3,1],[1,5,1],[4,2,1]]
17+
18+
**Output:** 7
19+
20+
**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
21+
22+
**Example 2:**
23+
24+
**Input:** grid = \[\[1,2,3],[4,5,6]]
25+
26+
**Output:** 12
27+
28+
**Constraints:**
29+
30+
* `m == grid.length`
31+
* `n == grid[i].length`
32+
* `1 <= m, n <= 200`
33+
* `0 <= grid[i][j] <= 100`
34+
35+
## Solution
36+
37+
```javascript
38+
/**
39+
* @param {number[][]} grid
40+
* @return {number}
41+
*/
42+
var minPathSum = function(grid) {
43+
const rows = grid.length
44+
const cols = grid[0].length
45+
46+
// Handle the special case where grid has only one cell
47+
if (rows === 1 && cols === 1) {
48+
return grid[0][0]
49+
}
50+
51+
// Create a 2D array for dynamic programming
52+
const dm = Array.from({ length: rows }, () => Array(cols).fill(0))
53+
54+
// Initialize the last column
55+
let s = 0
56+
for (let r = rows - 1; r >= 0; r--) {
57+
dm[r][cols - 1] = grid[r][cols - 1] + s
58+
s += grid[r][cols - 1]
59+
}
60+
61+
// Initialize the last row
62+
s = 0
63+
for (let c = cols - 1; c >= 0; c--) {
64+
dm[rows - 1][c] = grid[rows - 1][c] + s
65+
s += grid[rows - 1][c]
66+
}
67+
68+
// Recursive helper function
69+
const recur = (r, c) => {
70+
if (
71+
dm[r][c] === 0 &&
72+
r !== rows - 1 &&
73+
c !== cols - 1
74+
) {
75+
dm[r][c] =
76+
grid[r][c] +
77+
Math.min(
78+
recur(r + 1, c),
79+
recur(r, c + 1)
80+
)
81+
}
82+
return dm[r][c]
83+
}
84+
85+
// Start recursion from the top-left corner
86+
return recur(0, 0)
87+
};
88+
89+
export { minPathSum }
90+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 70\. Climbing Stairs
5+
6+
Easy
7+
8+
You are climbing a staircase. It takes `n` steps to reach the top.
9+
10+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
11+
12+
**Example 1:**
13+
14+
**Input:** n = 2
15+
16+
**Output:** 2
17+
18+
**Explanation:** There are two ways to climb to the top.
19+
20+
1. 1 step + 1 step
21+
22+
2. 2 steps
23+
24+
**Example 2:**
25+
26+
**Input:** n = 3
27+
28+
**Output:** 3
29+
30+
**Explanation:** There are three ways to climb to the top.
31+
32+
1. 1 step + 1 step + 1 step
33+
34+
2. 1 step + 2 steps
35+
36+
3. 2 steps + 1 step
37+
38+
**Constraints:**
39+
40+
* `1 <= n <= 45`
41+
42+
## Solution
43+
44+
```javascript
45+
/**
46+
* @param {number} n
47+
* @return {number}
48+
*/
49+
var climbStairs = function(n) {
50+
if (n < 2) {
51+
return n
52+
}
53+
54+
// Create a cache (DP array) to store results
55+
const cache = new Array(n)
56+
57+
// Initialize base cases
58+
cache[0] = 1
59+
cache[1] = 2
60+
61+
// Fill the cache using the recurrence relation
62+
for (let i = 2; i < n; i++) {
63+
cache[i] = cache[i - 1] + cache[i - 2]
64+
}
65+
66+
// Return the result for the nth step
67+
return cache[n - 1]
68+
};
69+
70+
export { climbStairs }
71+
```

0 commit comments

Comments
 (0)