Skip to content

Commit ff9f6dd

Browse files
committedApr 7, 2025
Added tasks 57-137
1 parent abc3e3f commit ff9f6dd

File tree

31 files changed

+2239
-0
lines changed

31 files changed

+2239
-0
lines changed
 

Diff for: ‎README.md

+89
Large diffs are not rendered by default.
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 57\. Insert Interval
5+
6+
Medium
7+
8+
You are given an array of non-overlapping intervals `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> represent the start and the end of the <code>i<sup>th</sup></code> interval and `intervals` is sorted in ascending order by <code>start<sub>i</sub></code>. You are also given an interval `newInterval = [start, end]` that represents the start and end of another interval.
9+
10+
Insert `newInterval` into `intervals` such that `intervals` is still sorted in ascending order by <code>start<sub>i</sub></code> and `intervals` still does not have any overlapping intervals (merge overlapping intervals if necessary).
11+
12+
Return `intervals` _after the insertion_.
13+
14+
**Example 1:**
15+
16+
**Input:** intervals = \[\[1,3],[6,9]], newInterval = [2,5]
17+
18+
**Output:** [[1,5],[6,9]]
19+
20+
**Example 2:**
21+
22+
**Input:** intervals = \[\[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
23+
24+
**Output:** [[1,2],[3,10],[12,16]]
25+
26+
**Explanation:** Because the new interval `[4,8]` overlaps with `[3,5],[6,7],[8,10]`.
27+
28+
**Example 3:**
29+
30+
**Input:** intervals = [], newInterval = [5,7]
31+
32+
**Output:** [[5,7]]
33+
34+
**Example 4:**
35+
36+
**Input:** intervals = \[\[1,5]], newInterval = [2,3]
37+
38+
**Output:** [[1,5]]
39+
40+
**Example 5:**
41+
42+
**Input:** intervals = \[\[1,5]], newInterval = [2,7]
43+
44+
**Output:** [[1,7]]
45+
46+
**Constraints:**
47+
48+
* <code>0 <= intervals.length <= 10<sup>4</sup></code>
49+
* `intervals[i].length == 2`
50+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>5</sup></code>
51+
* `intervals` is sorted by <code>start<sub>i</sub></code> in **ascending** order.
52+
* `newInterval.length == 2`
53+
* <code>0 <= start <= end <= 10<sup>5</sup></code>
54+
55+
## Solution
56+
57+
```typescript
58+
function insert(intervals: number[][], newInterval: number[]): number[][] {
59+
const n = intervals.length
60+
let l = 0
61+
let r = n - 1
62+
while (l < n && newInterval[0] > intervals[l][1]) {
63+
l++
64+
}
65+
while (r >= 0 && newInterval[1] < intervals[r][0]) {
66+
r--
67+
}
68+
const res: number[][] = new Array(l + n - r).fill(0).map(() => new Array(2))
69+
for (let i = 0; i < l; i++) {
70+
res[i] = [...intervals[i]]
71+
}
72+
res[l] = [
73+
Math.min(newInterval[0], l === n ? newInterval[0] : intervals[l][0]),
74+
Math.max(newInterval[1], r === -1 ? newInterval[1] : intervals[r][1]),
75+
]
76+
for (let i = l + 1, j = r + 1; j < n; i++, j++) {
77+
res[i] = intervals[j]
78+
}
79+
return res
80+
}
81+
82+
export { insert }
83+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 58\. Length of Last Word
5+
6+
Easy
7+
8+
Given a string `s` consisting of some words separated by some number of spaces, return _the length of the **last** word in the string._
9+
10+
A **word** is a maximal substring consisting of non-space characters only.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "Hello World"
15+
16+
**Output:** 5
17+
18+
**Explanation:** The last word is "World" with length 5.
19+
20+
**Example 2:**
21+
22+
**Input:** s = " fly me to the moon "
23+
24+
**Output:** 4
25+
26+
**Explanation:** The last word is "moon" with length 4.
27+
28+
**Example 3:**
29+
30+
**Input:** s = "luffy is still joyboy"
31+
32+
**Output:** 6
33+
34+
**Explanation:** The last word is "joyboy" with length 6.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= s.length <= 10<sup>4</sup></code>
39+
* `s` consists of only English letters and spaces `' '`.
40+
* There will be at least one word in `s`.
41+
42+
## Solution
43+
44+
```typescript
45+
function lengthOfLastWord(s: string): number {
46+
let len = 0
47+
for (let i = s.length - 1; i >= 0; i--) {
48+
const ch = s[i]
49+
if (ch === ' ' && len > 0) {
50+
break
51+
} else if (ch !== ' ') {
52+
len++
53+
}
54+
}
55+
return len
56+
}
57+
58+
export { lengthOfLastWord }
59+
```

Diff for: ‎src/main/ts/g0001_0100/s0061_rotate_list/readme.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 61\. Rotate List
5+
6+
Medium
7+
8+
Given the `head` of a linked list, rotate the list to the right by `k` places.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg)
13+
14+
**Input:** head = [1,2,3,4,5], k = 2
15+
16+
**Output:** [4,5,1,2,3]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg)
21+
22+
**Input:** head = [0,1,2], k = 4
23+
24+
**Output:** [2,0,1]
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the list is in the range `[0, 500]`.
29+
* `-100 <= Node.val <= 100`
30+
* <code>0 <= k <= 2 * 10<sup>9</sup></code>
31+
32+
## Solution
33+
34+
```typescript
35+
/**
36+
* Definition for singly-linked list.
37+
* class ListNode {
38+
* val: number
39+
* next: ListNode | null
40+
* constructor(val?: number, next?: ListNode | null) {
41+
* this.val = (val===undefined ? 0 : val)
42+
* this.next = (next===undefined ? null : next)
43+
* }
44+
* }
45+
*/
46+
function rotateRight(head: ListNode | null, k: number): ListNode | null {
47+
if (!head || k === 0) {
48+
return head
49+
}
50+
let tail: ListNode | null = head
51+
let count = 1
52+
while (tail.next) {
53+
count++
54+
tail = tail.next
55+
}
56+
let times = k % count
57+
if (times === 0) {
58+
return head
59+
}
60+
let temp: ListNode | null = head
61+
for (let i = 1; i < count - times; i++) {
62+
temp = temp!.next
63+
}
64+
let newHead = temp!.next
65+
temp!.next = null
66+
tail!.next = head
67+
return newHead
68+
}
69+
70+
export { rotateRight }
71+
```
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 63\. Unique Paths II
5+
6+
Medium
7+
8+
A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
9+
10+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
11+
12+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
13+
14+
An obstacle and space is marked as `1` and `0` respectively in the grid.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg)
19+
20+
**Input:** obstacleGrid = \[\[0,0,0],[0,1,0],[0,0,0]]
21+
22+
**Output:** 2
23+
24+
**Explanation:** There is one obstacle in the middle of the 3x3 grid above. There are two ways to reach the bottom-right corner: 1. Right -> Right -> Down -> Down 2. Down -> Down -> Right -> Right
25+
26+
**Example 2:**
27+
28+
![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)
29+
30+
**Input:** obstacleGrid = \[\[0,1],[0,0]]
31+
32+
**Output:** 1
33+
34+
**Constraints:**
35+
36+
* `m == obstacleGrid.length`
37+
* `n == obstacleGrid[i].length`
38+
* `1 <= m, n <= 100`
39+
* `obstacleGrid[i][j]` is `0` or `1`.
40+
41+
## Solution
42+
43+
```typescript
44+
function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
45+
if (obstacleGrid[0][0] === 1) {
46+
return 0
47+
}
48+
obstacleGrid[0][0] = 1
49+
const m = obstacleGrid.length
50+
const n = obstacleGrid[0].length
51+
for (let i = 1; i < m; i++) {
52+
if (obstacleGrid[i][0] === 1) {
53+
obstacleGrid[i][0] = 0
54+
} else {
55+
obstacleGrid[i][0] = obstacleGrid[i - 1][0]
56+
}
57+
}
58+
for (let j = 1; j < n; j++) {
59+
if (obstacleGrid[0][j] === 1) {
60+
obstacleGrid[0][j] = 0
61+
} else {
62+
obstacleGrid[0][j] = obstacleGrid[0][j - 1]
63+
}
64+
}
65+
for (let i = 1; i < m; i++) {
66+
for (let j = 1; j < n; j++) {
67+
if (obstacleGrid[i][j] === 1) {
68+
obstacleGrid[i][j] = 0
69+
} else {
70+
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]
71+
}
72+
}
73+
}
74+
return obstacleGrid[m - 1][n - 1]
75+
}
76+
77+
export { uniquePathsWithObstacles }
78+
```

Diff for: ‎src/main/ts/g0001_0100/s0066_plus_one/readme.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 66\. Plus One
5+
6+
Easy
7+
8+
You are given a **large integer** represented as an integer array `digits`, where each `digits[i]` is the `ith` digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading `0`'s.
9+
10+
Increment the large integer by one and return _the resulting array of digits_.
11+
12+
**Example 1:**
13+
14+
**Input:** digits = [1,2,3]
15+
16+
**Output:** [1,2,4]
17+
18+
**Explanation:** The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4].
19+
20+
**Example 2:**
21+
22+
**Input:** digits = [4,3,2,1]
23+
24+
**Output:** [4,3,2,2]
25+
26+
**Explanation:** The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2].
27+
28+
**Example 3:**
29+
30+
**Input:** digits = [0]
31+
32+
**Output:** [1]
33+
34+
**Explanation:** The array represents the integer 0. Incrementing by one gives 0 + 1 = 1. Thus, the result should be [1].
35+
36+
**Example 4:**
37+
38+
**Input:** digits = [9]
39+
40+
**Output:** [1,0]
41+
42+
**Explanation:** The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0].
43+
44+
**Constraints:**
45+
46+
* `1 <= digits.length <= 100`
47+
* `0 <= digits[i] <= 9`
48+
* `digits` does not contain any leading `0`'s.
49+
50+
## Solution
51+
52+
```typescript
53+
function plusOne(digits: number[]): number[] {
54+
let num = 1
55+
let carry = 0
56+
let sum
57+
for (let i = digits.length - 1; i >= 0; i--) {
58+
if (i === digits.length - 1) {
59+
sum = digits[i] + carry + num
60+
} else {
61+
sum = digits[i] + carry
62+
}
63+
carry = Math.floor(sum / 10)
64+
digits[i] = sum % 10
65+
}
66+
if (carry !== 0) {
67+
return [carry, ...digits]
68+
}
69+
return digits
70+
}
71+
72+
export { plusOne }
73+
```

0 commit comments

Comments
 (0)