Skip to content

Added tasks 79-394 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 152 additions & 5 deletions README.md

Large diffs are not rendered by default.

77 changes: 37 additions & 40 deletions src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ _Merge all the linked-lists into one sorted linked-list and return it._
## Solution

```typescript
/**
import { ListNode } from '../../com_github_leetcode/listnode'

/*
* Definition for singly-linked list.
* class ListNode {
* val: number
Expand All @@ -52,51 +54,46 @@ _Merge all the linked-lists into one sorted linked-list and return it._
* }
* }
*/
function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
if (lists.length === 0) {
return null
const merge2Lists = (list1: ListNode | null, list2: ListNode | null): ListNode | null => {
if (!list1 || !list2) {
return list1 ?? list2
}
return mergeKListsRecursive(lists, 0, lists.length)
}

function mergeKListsRecursive(lists: Array<ListNode | null>, leftIndex: number, rightIndex: number): ListNode | null {
if (rightIndex > leftIndex + 1) {
const mid = Math.floor((leftIndex + rightIndex) / 2)
const left = mergeKListsRecursive(lists, leftIndex, mid)
const right = mergeKListsRecursive(lists, mid, rightIndex)
return mergeTwoLists(left, right)
} else {
return lists[leftIndex]
const tempHead = new ListNode()
let current = tempHead
let l1 = list1
let l2 = list2
while (l1 || l2) {
if (!l1) {
current.next = l2
break
}
if (!l2) {
current.next = l1
break
}
if (l1.val < l2.val) {
current.next = l1
l1 = l1.next
} else {
current.next = l2
l2 = l2.next
}
current = current.next
}
return tempHead.next
}

function mergeTwoLists(left: ListNode | null, right: ListNode | null): ListNode | null {
if (left === null) {
return right
}
if (right === null) {
return left
}
let res: ListNode | null
if (left.val <= right.val) {
res = left
left = left.next
} else {
res = right
right = right.next
}
let node = res
while (left !== null || right !== null) {
if (right === null || left.val <= right.val) {
node.next = left
left = left.next
} else {
node.next = right
right = right.next
const mergeKLists = (lists: Array<ListNode | null>): ListNode | null => {
while (lists.length > 1) {
const mergedLists = []
for (let i = 0; i < lists.length; i += 2) {
const list1 = lists[i]
const list2 = lists[i + 1] ?? null
mergedLists.push(merge2Lists(list1, list2))
}
node = node.next
lists = mergedLists
}
return res
return lists[0] ?? null
}

export { mergeKLists }
Expand Down
105 changes: 105 additions & 0 deletions src/main/ts/g0001_0100/s0079_word_search/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
[![](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)
[![](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)

## 79\. Word Search

Medium

Given an `m x n` grid of characters `board` and a string `word`, return `true` _if_ `word` _exists in the grid_.

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.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/04/word2.jpg)

**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"

**Output:** true

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg)

**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"

**Output:** true

**Example 3:**

![](https://assets.leetcode.com/uploads/2020/10/15/word3.jpg)

**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"

**Output:** false

**Constraints:**

* `m == board.length`
* `n = board[i].length`
* `1 <= m, n <= 6`
* `1 <= word.length <= 15`
* `board` and `word` consists of only lowercase and uppercase English letters.

**Follow up:** Could you use search pruning to make your solution faster with a larger `board`?

## Solution

```typescript
function exist(board: string[][], word: string): boolean {
if (word.length === 0) return false
let ret = false
const marks = makeArray(board)
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] !== word.charAt(0)) continue
if (loop(marks, board, i, j, word, 0)) return true
}
}

return ret
}

function makeArray(board: string[][]) {
const arr = []
for (let i = 0; i < board.length; i++) {
arr[i] = []
for (let j = 0; j < board[i].length; j++) {
arr[i][j] = false
}
}
return arr
}

function loop(marks: boolean[][], board: string[][], i: number, j: number, word: string, index: number): boolean {
if (i < 0 || j < 0 || i >= board.length || j >= board[i].length || marks[i][j]) {
return false
}

if (board[i][j] !== word.charAt(index)) {
return false
} else if (index === word.length - 1) {
return true
}

marks[i][j] = true
index++

let r = loop(marks, board, i - 1, j, word, index)
if (r) return true

r = loop(marks, board, i + 1, j, word, index)
if (r) return true

r = loop(marks, board, i, j - 1, word, index)
if (r) return true

r = loop(marks, board, i, j + 1, word, index)
if (r) return true

marks[i][j] = false
return false
}

export { exist }
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[![](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)
[![](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)

## 84\. Largest Rectangle in Histogram

Hard

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_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg)

**Input:** heights = [2,1,5,6,2,3]

**Output:** 10

**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.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg)

**Input:** heights = [2,4]

**Output:** 4

**Constraints:**

* <code>1 <= heights.length <= 10<sup>5</sup></code>
* <code>0 <= heights[i] <= 10<sup>4</sup></code>

## Solution

```typescript
function largestRectangleArea(heights: number[]): number {
let stack = []
let nextSmall = new Array(heights.length).fill(heights.length)
let prevSmall = new Array(heights.length).fill(-1)
for (let i = 0; i < heights.length; i++) {
while (stack.length !== 0 && heights[stack[stack.length - 1]] > heights[i]) {
nextSmall[stack[stack.length - 1]] = i
stack.pop()
}
if (stack.length !== 0) {
prevSmall[i] = stack[stack.length - 1]
}
stack.push(i)
}
let maxArea = 0
for (let i = 0; i < heights.length; i++) {
let currentArea = heights[i] * (nextSmall[i] - prevSmall[i] - 1)
maxArea = Math.max(maxArea, currentArea)
}
return maxArea
}

export { largestRectangleArea }
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
[![](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)
[![](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)

## 94\. Binary Tree Inorder Traversal

Easy

Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)

**Input:** root = [1,null,2,3]

**Output:** [1,3,2]

**Example 2:**

**Input:** root = []

**Output:** []

**Example 3:**

**Input:** root = [1]

**Output:** [1]

**Example 4:**

![](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg)

**Input:** root = [1,2]

**Output:** [2,1]

**Example 5:**

![](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg)

**Input:** root = [1,null,2]

**Output:** [1,2]

**Constraints:**

* The number of nodes in the tree is in the range `[0, 100]`.
* `-100 <= Node.val <= 100`

**Follow up:** Recursive solution is trivial, could you do it iteratively?

## Solution

```typescript
import { TreeNode } from '../../com_github_leetcode/treenode'

/*
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function inorderTraversal(root: TreeNode | null): number[] {
if (!root) return []
if (!root.val) return []
const result: number[] = []
function traverse(node: TreeNode, arr: number[]) {
if (node.left) {
traverse(node.left, result)
}
result.push(node.val)
if (node.right) {
traverse(node.right, result)
}
}
traverse(root, result)
return result
}

export { inorderTraversal }
```
Loading