Skip to content

Added tasks 80-92 #105

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 4 commits into from
Apr 5, 2025
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
| 0283 |[Move Zeroes](src/main/ts/g0201_0300/s0283_move_zeroes/solution.ts)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 1 | 82.86
| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 1 | 89.70
| 0058 |[Length of Last Word](src/main/ts/g0001_0100/s0058_length_of_last_word/solution.ts)| Easy | String | 0 | 100.00
| 0080 |[Remove Duplicates from Sorted Array II](src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.ts)| Medium | Array, Two_Pointers | 40 | 99.63
| 0189 |[Rotate Array](src/main/ts/g0101_0200/s0189_rotate_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 1 | 86.17
| 0055 |[Jump Game](src/main/ts/g0001_0100/s0055_jump_game/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
| 0075 |[Sort Colors](src/main/ts/g0001_0100/s0075_sort_colors/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
Expand Down Expand Up @@ -1005,8 +1006,10 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0088 |[Merge Sorted Array](src/main/ts/g0001_0100/s0088_merge_sorted_array/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00
| 0027 |[Remove Element](src/main/ts/g0001_0100/s0027_remove_element/solution.ts)| Easy | Array, Two_Pointers | 0 | 100.00
| 0026 |[Remove Duplicates from Sorted Array](src/main/ts/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.ts)| Easy | Top_Interview_Questions, Array, Two_Pointers | 0 | 100.00
| 0080 |[Remove Duplicates from Sorted Array II](src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.ts)| Medium | Array, Two_Pointers | 40 | 99.63
| 0169 |[Majority Element](src/main/ts/g0101_0200/s0169_majority_element/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
| 0189 |[Rotate Array](src/main/ts/g0101_0200/s0189_rotate_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 1 | 86.17
| 0121 |[Best Time to Buy and Sell Stock](src/main/ts/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 96.44
Expand Down Expand Up @@ -1077,9 +1080,12 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)), AI_can_be_used_to_solve_the_task | 2 | 95.82
| 0021 |[Merge Two Sorted Lists](src/main/ts/g0001_0100/s0021_merge_two_sorted_lists/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 1 | 51.21
| 0138 |[Copy List with Random Pointer](src/main/ts/g0101_0200/s0138_copy_list_with_random_pointer/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 49 | 72.42
| 0092 |[Reverse Linked List II](src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts)| Medium | Linked_List | 0 | 100.00
| 0025 |[Reverse Nodes in k-Group](src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group/solution.ts)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
| 0019 |[Remove Nth Node From End of List](src/main/ts/g0001_0100/s0019_remove_nth_node_from_end_of_list/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 0 | 100.00
| 0082 |[Remove Duplicates from Sorted List II](src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00
| 0061 |[Rotate List](src/main/ts/g0001_0100/s0061_rotate_list/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00
| 0086 |[Partition List](src/main/ts/g0001_0100/s0086_partition_list/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00
| 0146 |[LRU Cache](src/main/ts/g0101_0200/s0146_lru_cache/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 97 | 81.52

#### Top Interview 150 Binary Tree General
Expand Down Expand Up @@ -1217,6 +1223,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 1 | 89.70
| 0088 |[Merge Sorted Array](src/main/ts/g0001_0100/s0088_merge_sorted_array/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00

#### Day 3 Array

Expand Down Expand Up @@ -1362,6 +1369,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0160 |[Intersection of Two Linked Lists](src/main/ts/g0101_0200/s0160_intersection_of_two_linked_lists/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 65 | 72.36
| 0082 |[Remove Duplicates from Sorted List II](src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00

#### Day 12 Linked List

Expand Down Expand Up @@ -1525,6 +1533,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0082 |[Remove Duplicates from Sorted List II](src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00
| 0015 |[3Sum](src/main/ts/g0001_0100/s0015_3sum/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 30 | 91.56

#### Day 4 Two Pointers
Expand Down Expand Up @@ -1695,7 +1704,12 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
| 0098 |[Validate Binary Search Tree](src/main/ts/g0001_0100/s0098_validate_binary_search_tree/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
| 0096 |[Unique Binary Search Trees](src/main/ts/g0001_0100/s0096_unique_binary_search_trees/solution.ts)| Medium | Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
| 0094 |[Binary Tree Inorder Traversal](src/main/ts/g0001_0100/s0094_binary_tree_inorder_traversal/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
| 0092 |[Reverse Linked List II](src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts)| Medium | Linked_List, Top_Interview_150_Linked_List | 0 | 100.00
| 0088 |[Merge Sorted Array](src/main/ts/g0001_0100/s0088_merge_sorted_array/solution.ts)| Medium | Two_Pointers, Linked_List, Top_Interview_150_Linked_List | 0 | 100.00
| 0086 |[Partition List](src/main/ts/g0001_0100/s0086_partition_list/solution.ts)| Medium | Two_Pointers, Linked_List, Top_Interview_150_Linked_List | 0 | 100.00
| 0084 |[Largest Rectangle in Histogram](src/main/ts/g0001_0100/s0084_largest_rectangle_in_histogram/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Stack, Monotonic_Stack, Big_O_Time_O(n_log_n)_Space_O(log_n) | 15 | 72.81
| 0082 |[Remove Duplicates from Sorted List II](src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts)| Medium | Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Algorithm_II_Day_3_Two_Pointers, Top_Interview_150_Linked_List | 0 | 100.00
| 0080 |[Remove Duplicates from Sorted Array II](src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.ts)| Medium | Array, Two_Pointers, Udemy_Arrays, Top_Interview_150_Array/String | 40 | 99.63
| 0079 |[Word Search](src/main/ts/g0001_0100/s0079_word_search/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Top_Interview_150_Backtracking, Big_O_Time_O(4^(m\*n))_Space_O(m\*n) | 243 | 85.30
| 0078 |[Subsets](src/main/ts/g0001_0100/s0078_subsets/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 0 | 100.00
| 0077 |[Combinations](src/main/ts/g0001_0100/s0077_combinations/solution.ts)| Medium | Backtracking, Algorithm_I_Day_11_Recursion_Backtracking, Top_Interview_150_Backtracking | 46 | 96.14
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
80\. Remove Duplicates from Sorted Array II

Medium

Given an integer array `nums` sorted in **non-decreasing order**, remove some duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears **at most twice**. The **relative order** of the elements should be kept the **same**.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.

Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.

Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.

**Custom Judge:**

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be **accepted**.

**Example 1:**

**Input:** nums = [1,1,1,2,2,3]

**Output:** 5, nums = [1,1,2,2,3,\_]

**Explanation:** Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).

**Example 2:**

**Input:** nums = [0,0,1,1,1,1,2,3,3]

**Output:** 7, nums = [0,0,1,1,2,3,3,\_,\_]

**Explanation:** Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).

**Constraints:**

* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
* `nums` is sorted in **non-decreasing** order.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// #Medium #Array #Two_Pointers #Udemy_Arrays #Top_Interview_150_Array/String
// #2025_04_05_Time_40_ms_(99.63%)_Space_59.84_MB_(30.62%)

function removeDuplicates(nums: number[]): number {
let i = 0
let k = 0
let count = 0
while (i < nums.length - 1) {
count++
if (count <= 2) {
nums[k++] = nums[i]
}
if (nums[i] !== nums[i + 1]) {
count = 0
}
i++
}
count++
if (count <= 2) {
nums[k++] = nums[i]
}
return k
}

export { removeDuplicates }
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
82\. Remove Duplicates from Sorted List II

Medium

Given the `head` of a sorted linked list, _delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list_. Return _the linked list **sorted** as well_.

**Example 1:**

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

**Input:** head = [1,2,3,3,4,4,5]

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

**Example 2:**

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

**Input:** head = [1,1,1,2,3]

**Output:** [2,3]

**Constraints:**

* The number of nodes in the list is in the range `[0, 300]`.
* `-100 <= Node.val <= 100`
* The list is guaranteed to be **sorted** in ascending order.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// #Medium #Two_Pointers #Linked_List #Data_Structure_II_Day_11_Linked_List
// #Algorithm_II_Day_3_Two_Pointers #Top_Interview_150_Linked_List
// #2025_04_05_Time_0_ms_(100.00%)_Space_58.58_MB_(53.51%)

import { ListNode } from '../../com_github_leetcode/listnode'

/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function deleteDuplicates(head: ListNode | null): ListNode | null {
if (!head || !head.next) {
return head
}
const dummy = new ListNode(0)
dummy.next = head
let prev: ListNode = dummy
let curr: ListNode | null = head
while (curr) {
let hasDuplicate = false
while (curr.next && curr.val === curr.next.val) {
hasDuplicate = true
curr = curr.next
}
if (hasDuplicate) {
prev.next = curr.next
} else {
prev = prev.next!
}
curr = curr.next
}
return dummy.next
}

export { deleteDuplicates }
27 changes: 27 additions & 0 deletions src/main/ts/g0001_0100/s0086_partition_list/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
86\. Partition List

Medium

Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`.

You should **preserve** the original relative order of the nodes in each of the two partitions.

**Example 1:**

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

**Input:** head = [1,4,3,2,5,2], x = 3

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

**Example 2:**

**Input:** head = [2,1], x = 2

**Output:** [1,2]

**Constraints:**

* The number of nodes in the list is in the range `[0, 200]`.
* `-100 <= Node.val <= 100`
* `-200 <= x <= 200`
38 changes: 38 additions & 0 deletions src/main/ts/g0001_0100/s0086_partition_list/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// #Medium #Two_Pointers #Linked_List #Top_Interview_150_Linked_List
// #2025_04_05_Time_0_ms_(100.00%)_Space_58.16_MB_(62.07%)

import { ListNode } from '../../com_github_leetcode/listnode'

/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function partition(head: ListNode | null, x: number): ListNode | null {
let beforeHead = new ListNode(0)
let afterHead = new ListNode(0)
let before = beforeHead
let after = afterHead
while (head !== null) {
const nextNode = head.next
if (head.val < x) {
before.next = head
before = before.next
} else {
after.next = head
after = after.next
}
head = nextNode
}
after.next = null
before.next = afterHead.next
return beforeHead.next
}

export { partition }
43 changes: 43 additions & 0 deletions src/main/ts/g0001_0100/s0088_merge_sorted_array/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
88\. Merge Sorted Array

Easy

You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively.

**Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**.

The final sorted array should not be returned by the function, but instead be _stored inside the array_ `nums1`. To accommodate this, `nums1` has a length of `m + n`, where the first `m` elements denote the elements that should be merged, and the last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`.

**Example 1:**

**Input:** nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3

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

**Explanation:** The arrays we are merging are [1,2,3] and [2,5,6]. The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.

**Example 2:**

**Input:** nums1 = [1], m = 1, nums2 = [], n = 0

**Output:** [1]

**Explanation:** The arrays we are merging are [1] and []. The result of the merge is [1].

**Example 3:**

**Input:** nums1 = [0], m = 0, nums2 = [1], n = 1

**Output:** [1]

**Explanation:** The arrays we are merging are [] and [1]. The result of the merge is [1]. Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.

**Constraints:**

* `nums1.length == m + n`
* `nums2.length == n`
* `0 <= m, n <= 200`
* `1 <= m + n <= 200`
* <code>-10<sup>9</sup> <= nums1[i], nums2[j] <= 10<sup>9</sup></code>

**Follow up:** Can you come up with an algorithm that runs in `O(m + n)` time?
20 changes: 20 additions & 0 deletions src/main/ts/g0001_0100/s0088_merge_sorted_array/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// #Medium #Two_Pointers #Linked_List #Top_Interview_150_Linked_List
// #2025_04_05_Time_0_ms_(100.00%)_Space_58.16_MB_(62.07%)

/**
* Do not return anything, modify nums1 in-place instead.
*/
function merge(nums1: number[], m: number, nums2: number[], n: number): void {
let i = m - 1
let j = nums1.length - 1
let p2 = n - 1
while (p2 >= 0) {
if (i >= 0 && nums1[i] > nums2[p2]) {
nums1[j--] = nums1[i--]
} else {
nums1[j--] = nums2[p2--]
}
}
}

export { merge }
28 changes: 28 additions & 0 deletions src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
92\. Reverse Linked List II

Medium

Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg)

**Input:** head = [1,2,3,4,5], left = 2, right = 4

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

**Example 2:**

**Input:** head = [5], left = 1, right = 1

**Output:** [5]

**Constraints:**

* The number of nodes in the list is `n`.
* `1 <= n <= 500`
* `-500 <= Node.val <= 500`
* `1 <= left <= right <= n`

**Follow up:** Could you do it in one pass?
Loading