From 0e5dd4850449cf0c3fb164d0f8e395e8cf585533 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Sat, 5 Apr 2025 14:59:35 +0700 Subject: [PATCH 1/4] Added tasks 80-92 --- README.md | 14 ++++++ .../readme.md | 49 ++++++++++++++++++ .../solution.ts | 25 ++++++++++ .../readme.md | 27 ++++++++++ .../solution.ts | 43 ++++++++++++++++ .../g0001_0100/s0086_partition_list/readme.md | 27 ++++++++++ .../s0086_partition_list/solution.ts | 39 +++++++++++++++ .../s0088_merge_sorted_array/readme.md | 43 ++++++++++++++++ .../s0088_merge_sorted_array/solution.ts | 20 ++++++++ .../s0092_reverse_linked_list_ii/readme.md | 28 +++++++++++ .../s0092_reverse_linked_list_ii/solution.ts | 50 +++++++++++++++++++ .../solution.test.ts | 11 ++++ .../solution.test.ts | 12 +++++ .../s0086_partition_list/solution.test.ts | 12 +++++ .../s0088_merge_sorted_array/solution.test.ts | 21 ++++++++ .../solution.test.ts | 12 +++++ 16 files changed, 433 insertions(+) create mode 100644 src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/readme.md create mode 100644 src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.ts create mode 100644 src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/readme.md create mode 100644 src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts create mode 100644 src/main/ts/g0001_0100/s0086_partition_list/readme.md create mode 100644 src/main/ts/g0001_0100/s0086_partition_list/solution.ts create mode 100644 src/main/ts/g0001_0100/s0088_merge_sorted_array/readme.md create mode 100644 src/main/ts/g0001_0100/s0088_merge_sorted_array/solution.ts create mode 100644 src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/readme.md create mode 100644 src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts create mode 100644 src/test/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.test.ts create mode 100644 src/test/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.test.ts create mode 100644 src/test/ts/g0001_0100/s0086_partition_list/solution.test.ts create mode 100644 src/test/ts/g0001_0100/s0088_merge_sorted_array/solution.test.ts create mode 100644 src/test/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.test.ts diff --git a/README.md b/README.md index 892fe2c..c7eb371 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/readme.md b/src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/readme.md new file mode 100644 index 0000000..36e2637 --- /dev/null +++ b/src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/readme.md @@ -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:** + +* 1 <= nums.length <= 3 * 104 +* -104 <= nums[i] <= 104 +* `nums` is sorted in **non-decreasing** order. diff --git a/src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.ts b/src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.ts new file mode 100644 index 0000000..018d236 --- /dev/null +++ b/src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.ts @@ -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 } diff --git a/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/readme.md b/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/readme.md new file mode 100644 index 0000000..c5d55ac --- /dev/null +++ b/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/readme.md @@ -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. diff --git a/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts b/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts new file mode 100644 index 0000000..d407f76 --- /dev/null +++ b/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts @@ -0,0 +1,43 @@ +// #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 } diff --git a/src/main/ts/g0001_0100/s0086_partition_list/readme.md b/src/main/ts/g0001_0100/s0086_partition_list/readme.md new file mode 100644 index 0000000..fd27e77 --- /dev/null +++ b/src/main/ts/g0001_0100/s0086_partition_list/readme.md @@ -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` diff --git a/src/main/ts/g0001_0100/s0086_partition_list/solution.ts b/src/main/ts/g0001_0100/s0086_partition_list/solution.ts new file mode 100644 index 0000000..4774f28 --- /dev/null +++ b/src/main/ts/g0001_0100/s0086_partition_list/solution.ts @@ -0,0 +1,39 @@ +// #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 } diff --git a/src/main/ts/g0001_0100/s0088_merge_sorted_array/readme.md b/src/main/ts/g0001_0100/s0088_merge_sorted_array/readme.md new file mode 100644 index 0000000..7656505 --- /dev/null +++ b/src/main/ts/g0001_0100/s0088_merge_sorted_array/readme.md @@ -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` +* -109 <= nums1[i], nums2[j] <= 109 + +**Follow up:** Can you come up with an algorithm that runs in `O(m + n)` time? diff --git a/src/main/ts/g0001_0100/s0088_merge_sorted_array/solution.ts b/src/main/ts/g0001_0100/s0088_merge_sorted_array/solution.ts new file mode 100644 index 0000000..559a255 --- /dev/null +++ b/src/main/ts/g0001_0100/s0088_merge_sorted_array/solution.ts @@ -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 } diff --git a/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/readme.md b/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/readme.md new file mode 100644 index 0000000..d7d7133 --- /dev/null +++ b/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/readme.md @@ -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? diff --git a/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts b/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts new file mode 100644 index 0000000..a8a9fab --- /dev/null +++ b/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts @@ -0,0 +1,50 @@ +// #Medium #Linked_List #Top_Interview_150_Linked_List +// #2025_04_05_Time_0_ms_(100.00%)_Space_55.58_MB_(58.46%) + +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 reverseBetween(head: ListNode | null, left: number, right: number): ListNode | null { + if (left === right || head === null) { + return head + } + let prev: ListNode | null = null + let temp: ListNode | null = head + let k = left + while (temp !== null && k > 1) { + prev = temp + temp = temp.next + k-- + } + const start = temp + let prev1: ListNode | null = null + let tail: ListNode | null = temp + for (let i = 0; i <= right - left && tail !== null; i++) { + const next = tail.next + tail.next = prev1 + prev1 = tail + tail = next + } + if (prev !== null) { + prev.next = prev1 + } else { + head = prev1 + } + if (start !== null) { + start.next = tail + } + return head +} + +export { reverseBetween } diff --git a/src/test/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.test.ts b/src/test/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.test.ts new file mode 100644 index 0000000..56273c0 --- /dev/null +++ b/src/test/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.test.ts @@ -0,0 +1,11 @@ +// tslint:disable:no-magic-numbers +import { removeDuplicates } from 'src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution' +import { expect, test } from 'vitest' + +test('removeDuplicates', () => { + expect(removeDuplicates([1,1,1,2,2,3])).toEqual(5) +}) + +test('removeDuplicates2', () => { + expect(removeDuplicates([0,0,1,1,1,1,2,3,3])).toEqual(7) +}) diff --git a/src/test/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.test.ts b/src/test/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.test.ts new file mode 100644 index 0000000..c1e52c7 --- /dev/null +++ b/src/test/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.test.ts @@ -0,0 +1,12 @@ +// tslint:disable:no-magic-numbers +import { deleteDuplicates } from 'src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution' +import { expect, test } from 'vitest' +import { constructLinkedList } from '../../com_github_leetcode/linkedlistutils' + +test('deleteDuplicates', () => { + expect(deleteDuplicates(constructLinkedList([1,2,3,3,4,4,5])).toString()).toEqual('1, 2, 5') +}) + +test('deleteDuplicates2', () => { + expect(deleteDuplicates(constructLinkedList([1,1,1,2,3])).toString()).toEqual('2, 3') +}) diff --git a/src/test/ts/g0001_0100/s0086_partition_list/solution.test.ts b/src/test/ts/g0001_0100/s0086_partition_list/solution.test.ts new file mode 100644 index 0000000..f7f4d40 --- /dev/null +++ b/src/test/ts/g0001_0100/s0086_partition_list/solution.test.ts @@ -0,0 +1,12 @@ +// tslint:disable:no-magic-numbers +import { partition } from 'src/main/ts/g0001_0100/s0086_partition_list/solution' +import { expect, test } from 'vitest' +import { constructLinkedList } from '../../com_github_leetcode/linkedlistutils' + +test('partition', () => { + expect(partition(constructLinkedList([1,4,3,2,5,2]), 3).toString()).toEqual('1, 2, 2, 4, 3, 5') +}) + +test('partition2', () => { + expect(partition(constructLinkedList([2,1]), 2).toString()).toEqual('1, 2') +}) diff --git a/src/test/ts/g0001_0100/s0088_merge_sorted_array/solution.test.ts b/src/test/ts/g0001_0100/s0088_merge_sorted_array/solution.test.ts new file mode 100644 index 0000000..66d8ea3 --- /dev/null +++ b/src/test/ts/g0001_0100/s0088_merge_sorted_array/solution.test.ts @@ -0,0 +1,21 @@ +// tslint:disable:no-magic-numbers +import { merge } from 'src/main/ts/g0001_0100/s0088_merge_sorted_array/solution' +import { expect, test } from 'vitest' + +test('merge', () => { + const nums1 = [1,2,3,0,0,0] + merge(nums1, 3, [2,5,6], 3) + expect(nums1).toEqual([1,2,2,3,5,6]) +}) + +test('merge2', () => { + const nums1 = [1] + merge(nums1, 1, [], 0) + expect(nums1).toEqual([1]) +}) + +test('merge3', () => { + const nums1 = [0] + merge(nums1, 0, [1], 1) + expect(nums1).toEqual([1]) +}) diff --git a/src/test/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.test.ts b/src/test/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.test.ts new file mode 100644 index 0000000..8888d00 --- /dev/null +++ b/src/test/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.test.ts @@ -0,0 +1,12 @@ +// tslint:disable:no-magic-numbers +import { reverseBetween } from 'src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution' +import { expect, test } from 'vitest' +import { constructLinkedList } from '../../com_github_leetcode/linkedlistutils' + +test('reverseBetween', () => { + expect(reverseBetween(constructLinkedList([1,2,3,4,5]), 2, 4).toString()).toEqual('1, 4, 3, 2, 5') +}) + +test('reverseBetween2', () => { + expect(reverseBetween(constructLinkedList([5]), 1, 1).toString()).toEqual('5') +}) From 3bc702aaeeecfc17e1e75844d243d3b0db1f5ce5 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 5 Apr 2025 11:24:26 +0300 Subject: [PATCH 2/4] Update solution.ts --- src/main/ts/g0001_0100/s0086_partition_list/solution.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/ts/g0001_0100/s0086_partition_list/solution.ts b/src/main/ts/g0001_0100/s0086_partition_list/solution.ts index 4774f28..cbeeff9 100644 --- a/src/main/ts/g0001_0100/s0086_partition_list/solution.ts +++ b/src/main/ts/g0001_0100/s0086_partition_list/solution.ts @@ -14,7 +14,6 @@ import { ListNode } from '../../com_github_leetcode/listnode' * } * } */ - function partition(head: ListNode | null, x: number): ListNode | null { let beforeHead = new ListNode(0) let afterHead = new ListNode(0) From 4d4198554a2f2368a076d58cbf50fec873921c7f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 5 Apr 2025 11:24:54 +0300 Subject: [PATCH 3/4] Update solution.ts --- src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts b/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts index a8a9fab..41c7ae8 100644 --- a/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts +++ b/src/main/ts/g0001_0100/s0092_reverse_linked_list_ii/solution.ts @@ -14,7 +14,6 @@ import { ListNode } from '../../com_github_leetcode/listnode' * } * } */ - function reverseBetween(head: ListNode | null, left: number, right: number): ListNode | null { if (left === right || head === null) { return head From 92a2ae56e0d96014add944b92aeb6a7b5b58cf56 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 5 Apr 2025 11:26:01 +0300 Subject: [PATCH 4/4] Update solution.ts --- .../s0082_remove_duplicates_from_sorted_list_ii/solution.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts b/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts index d407f76..aac4eee 100644 --- a/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts +++ b/src/main/ts/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/solution.ts @@ -15,7 +15,6 @@ import { ListNode } from '../../com_github_leetcode/listnode' * } * } */ - function deleteDuplicates(head: ListNode | null): ListNode | null { if (!head || !head.next) { return head