Skip to content

Commit e632054

Browse files
authored
Added tasks 79-394
1 parent 3395475 commit e632054

File tree

59 files changed

+3989
-72
lines changed

Some content is hidden

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

59 files changed

+3989
-72
lines changed

README.md

+152-5
Large diffs are not rendered by default.

src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/readme.md

+37-40
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ _Merge all the linked-lists into one sorted linked-list and return it._
4141
## Solution
4242

4343
```typescript
44-
/**
44+
import { ListNode } from '../../com_github_leetcode/listnode'
45+
46+
/*
4547
* Definition for singly-linked list.
4648
* class ListNode {
4749
* val: number
@@ -52,51 +54,46 @@ _Merge all the linked-lists into one sorted linked-list and return it._
5254
* }
5355
* }
5456
*/
55-
function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
56-
if (lists.length === 0) {
57-
return null
57+
const merge2Lists = (list1: ListNode | null, list2: ListNode | null): ListNode | null => {
58+
if (!list1 || !list2) {
59+
return list1 ?? list2
5860
}
59-
return mergeKListsRecursive(lists, 0, lists.length)
60-
}
61-
62-
function mergeKListsRecursive(lists: Array<ListNode | null>, leftIndex: number, rightIndex: number): ListNode | null {
63-
if (rightIndex > leftIndex + 1) {
64-
const mid = Math.floor((leftIndex + rightIndex) / 2)
65-
const left = mergeKListsRecursive(lists, leftIndex, mid)
66-
const right = mergeKListsRecursive(lists, mid, rightIndex)
67-
return mergeTwoLists(left, right)
68-
} else {
69-
return lists[leftIndex]
61+
const tempHead = new ListNode()
62+
let current = tempHead
63+
let l1 = list1
64+
let l2 = list2
65+
while (l1 || l2) {
66+
if (!l1) {
67+
current.next = l2
68+
break
69+
}
70+
if (!l2) {
71+
current.next = l1
72+
break
73+
}
74+
if (l1.val < l2.val) {
75+
current.next = l1
76+
l1 = l1.next
77+
} else {
78+
current.next = l2
79+
l2 = l2.next
80+
}
81+
current = current.next
7082
}
83+
return tempHead.next
7184
}
7285

73-
function mergeTwoLists(left: ListNode | null, right: ListNode | null): ListNode | null {
74-
if (left === null) {
75-
return right
76-
}
77-
if (right === null) {
78-
return left
79-
}
80-
let res: ListNode | null
81-
if (left.val <= right.val) {
82-
res = left
83-
left = left.next
84-
} else {
85-
res = right
86-
right = right.next
87-
}
88-
let node = res
89-
while (left !== null || right !== null) {
90-
if (right === null || left.val <= right.val) {
91-
node.next = left
92-
left = left.next
93-
} else {
94-
node.next = right
95-
right = right.next
86+
const mergeKLists = (lists: Array<ListNode | null>): ListNode | null => {
87+
while (lists.length > 1) {
88+
const mergedLists = []
89+
for (let i = 0; i < lists.length; i += 2) {
90+
const list1 = lists[i]
91+
const list2 = lists[i + 1] ?? null
92+
mergedLists.push(merge2Lists(list1, list2))
9693
}
97-
node = node.next
94+
lists = mergedLists
9895
}
99-
return res
96+
return lists[0] ?? null
10097
}
10198

10299
export { mergeKLists }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
## 79\. Word Search
5+
6+
Medium
7+
8+
Given an `m x n` grid of characters `board` and a string `word`, return `true` _if_ `word` _exists in the grid_.
9+
10+
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/04/word2.jpg)
15+
16+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
17+
18+
**Output:** true
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg)
23+
24+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
25+
26+
**Output:** true
27+
28+
**Example 3:**
29+
30+
![](https://assets.leetcode.com/uploads/2020/10/15/word3.jpg)
31+
32+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
33+
34+
**Output:** false
35+
36+
**Constraints:**
37+
38+
* `m == board.length`
39+
* `n = board[i].length`
40+
* `1 <= m, n <= 6`
41+
* `1 <= word.length <= 15`
42+
* `board` and `word` consists of only lowercase and uppercase English letters.
43+
44+
**Follow up:** Could you use search pruning to make your solution faster with a larger `board`?
45+
46+
## Solution
47+
48+
```typescript
49+
function exist(board: string[][], word: string): boolean {
50+
if (word.length === 0) return false
51+
let ret = false
52+
const marks = makeArray(board)
53+
for (let i = 0; i < board.length; i++) {
54+
for (let j = 0; j < board[i].length; j++) {
55+
if (board[i][j] !== word.charAt(0)) continue
56+
if (loop(marks, board, i, j, word, 0)) return true
57+
}
58+
}
59+
60+
return ret
61+
}
62+
63+
function makeArray(board: string[][]) {
64+
const arr = []
65+
for (let i = 0; i < board.length; i++) {
66+
arr[i] = []
67+
for (let j = 0; j < board[i].length; j++) {
68+
arr[i][j] = false
69+
}
70+
}
71+
return arr
72+
}
73+
74+
function loop(marks: boolean[][], board: string[][], i: number, j: number, word: string, index: number): boolean {
75+
if (i < 0 || j < 0 || i >= board.length || j >= board[i].length || marks[i][j]) {
76+
return false
77+
}
78+
79+
if (board[i][j] !== word.charAt(index)) {
80+
return false
81+
} else if (index === word.length - 1) {
82+
return true
83+
}
84+
85+
marks[i][j] = true
86+
index++
87+
88+
let r = loop(marks, board, i - 1, j, word, index)
89+
if (r) return true
90+
91+
r = loop(marks, board, i + 1, j, word, index)
92+
if (r) return true
93+
94+
r = loop(marks, board, i, j - 1, word, index)
95+
if (r) return true
96+
97+
r = loop(marks, board, i, j + 1, word, index)
98+
if (r) return true
99+
100+
marks[i][j] = false
101+
return false
102+
}
103+
104+
export { exist }
105+
```
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+
## 84\. Largest Rectangle in Histogram
5+
6+
Hard
7+
8+
Given an array of integers `heights` representing the histogram's bar height where the width of each bar is `1`, return _the area of the largest rectangle in the histogram_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg)
13+
14+
**Input:** heights = [2,1,5,6,2,3]
15+
16+
**Output:** 10
17+
18+
**Explanation:** The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units.
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg)
23+
24+
**Input:** heights = [2,4]
25+
26+
**Output:** 4
27+
28+
**Constraints:**
29+
30+
* <code>1 <= heights.length <= 10<sup>5</sup></code>
31+
* <code>0 <= heights[i] <= 10<sup>4</sup></code>
32+
33+
## Solution
34+
35+
```typescript
36+
function largestRectangleArea(heights: number[]): number {
37+
let stack = []
38+
let nextSmall = new Array(heights.length).fill(heights.length)
39+
let prevSmall = new Array(heights.length).fill(-1)
40+
for (let i = 0; i < heights.length; i++) {
41+
while (stack.length !== 0 && heights[stack[stack.length - 1]] > heights[i]) {
42+
nextSmall[stack[stack.length - 1]] = i
43+
stack.pop()
44+
}
45+
if (stack.length !== 0) {
46+
prevSmall[i] = stack[stack.length - 1]
47+
}
48+
stack.push(i)
49+
}
50+
let maxArea = 0
51+
for (let i = 0; i < heights.length; i++) {
52+
let currentArea = heights[i] * (nextSmall[i] - prevSmall[i] - 1)
53+
maxArea = Math.max(maxArea, currentArea)
54+
}
55+
return maxArea
56+
}
57+
58+
export { largestRectangleArea }
59+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
## 94\. Binary Tree Inorder Traversal
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
13+
14+
**Input:** root = [1,null,2,3]
15+
16+
**Output:** [1,3,2]
17+
18+
**Example 2:**
19+
20+
**Input:** root = []
21+
22+
**Output:** []
23+
24+
**Example 3:**
25+
26+
**Input:** root = [1]
27+
28+
**Output:** [1]
29+
30+
**Example 4:**
31+
32+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg)
33+
34+
**Input:** root = [1,2]
35+
36+
**Output:** [2,1]
37+
38+
**Example 5:**
39+
40+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg)
41+
42+
**Input:** root = [1,null,2]
43+
44+
**Output:** [1,2]
45+
46+
**Constraints:**
47+
48+
* The number of nodes in the tree is in the range `[0, 100]`.
49+
* `-100 <= Node.val <= 100`
50+
51+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
52+
53+
## Solution
54+
55+
```typescript
56+
import { TreeNode } from '../../com_github_leetcode/treenode'
57+
58+
/*
59+
* Definition for a binary tree node.
60+
* class TreeNode {
61+
* val: number
62+
* left: TreeNode | null
63+
* right: TreeNode | null
64+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
65+
* this.val = (val===undefined ? 0 : val)
66+
* this.left = (left===undefined ? null : left)
67+
* this.right = (right===undefined ? null : right)
68+
* }
69+
* }
70+
*/
71+
function inorderTraversal(root: TreeNode | null): number[] {
72+
if (!root) return []
73+
if (!root.val) return []
74+
const result: number[] = []
75+
function traverse(node: TreeNode, arr: number[]) {
76+
if (node.left) {
77+
traverse(node.left, result)
78+
}
79+
result.push(node.val)
80+
if (node.right) {
81+
traverse(node.right, result)
82+
}
83+
}
84+
traverse(root, result)
85+
return result
86+
}
87+
88+
export { inorderTraversal }
89+
```

0 commit comments

Comments
 (0)