diff --git a/README.md b/README.md index 6dd984a..5b90a62 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ |-|-|-|-|-|- | 0034 |[Find First and Last Position of Element in Sorted Array](src/main/ts/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 45 | 98.05 | 0033 |[Search in Rotated Sorted Array](src/main/ts/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 43 | 98.54 +| 0074 |[Search a 2D Matrix](src/main/ts/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 45 | 96.82 #### Day 2 Binary Search @@ -146,6 +147,7 @@ | | | | | | |-|-|-|-|-|- +| 0078 |[Subsets](src/main/ts/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Big_O_Time_O(2^n)_Space_O(n*2^n) | 50 | 94.61 #### Day 10 Recursion Backtracking @@ -259,6 +261,7 @@ | | | | | | |-|-|-|-|-|- +| 0074 |[Search a 2D Matrix](src/main/ts/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 45 | 96.82 #### Day 9 @@ -919,6 +922,7 @@ | | | | | | |-|-|-|-|-|- +| 0074 |[Search a 2D Matrix](src/main/ts/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 45 | 96.82 | 0033 |[Search in Rotated Sorted Array](src/main/ts/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 43 | 98.54 #### Day 9 Binary Search Tree @@ -952,6 +956,7 @@ | | | | | | |-|-|-|-|-|- | 0003 |[Longest Substring Without Repeating Characters](src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1) | 65 | 90.77 +| 0076 |[Minimum Window Substring](src/main/ts/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(s.length())_Space_O(1) | 82 | 82.17 #### Day 15 Tree @@ -1016,6 +1021,7 @@ |-|-|-|-|-|- | 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 54 | 91.72 | 0055 |[Jump Game](src/main/ts/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 58 | 91.95 +| 0075 |[Sort Colors](src/main/ts/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 43 | 97.41 | 0041 |[First Missing Positive](src/main/ts/g0001_0100/s0041_first_missing_positive)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 55 | 98.51 #### Udemy Two Pointers @@ -1040,7 +1046,9 @@ | | | | | | |-|-|-|-|-|- +| 0074 |[Search a 2D Matrix](src/main/ts/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 45 | 96.82 | 0048 |[Rotate Image](src/main/ts/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 45 | 95.31 +| 0073 |[Set Matrix Zeroes](src/main/ts/g0001_0100/s0073_set_matrix_zeroes)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Big_O_Time_O(m*n)_Space_O(1) | 61 | 97.66 | 0056 |[Merge Intervals](src/main/ts/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 84 | 92.06 #### Udemy Linked List @@ -1083,6 +1091,7 @@ |-|-|-|-|-|- | 0022 |[Generate Parentheses](src/main/ts/g0001_0100/s0022_generate_parentheses)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 60 | 62.03 | 0039 |[Combination Sum](src/main/ts/g0001_0100/s0039_combination_sum)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 65 | 86.86 +| 0078 |[Subsets](src/main/ts/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Big_O_Time_O(2^n)_Space_O(n*2^n) | 50 | 94.61 | 0017 |[Letter Combinations of a Phone Number](src/main/ts/g0001_0100/s0017_letter_combinations_of_a_phone_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 52 | 79.11 | 0046 |[Permutations](src/main/ts/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n*n!)_Space_O(n+n!) | 56 | 96.26 @@ -1124,6 +1133,7 @@ | | | | | | |-|-|-|-|-|- +| 0074 |[Search a 2D Matrix](src/main/ts/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 45 | 96.82 #### Day 6 String @@ -1184,6 +1194,7 @@ | | | | | | |-|-|-|-|-|- +| 0075 |[Sort Colors](src/main/ts/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 43 | 97.41 | 0056 |[Merge Intervals](src/main/ts/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 84 | 92.06 #### Day 3 Array @@ -1303,6 +1314,11 @@ | 0438 |[Find All Anagrams in a String](src/main/ts/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer, Big_O_Time_O(n+m)_Space_O(1) | 69 | 96.69 | 0437 |[Path Sum III](src/main/ts/g0401_0500/s0437_path_sum_iii)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Big_O_Time_O(n)_Space_O(n) | 68 | 86.71 | 0416 |[Partition Equal Subset Sum](src/main/ts/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming, Big_O_Time_O(n*sums)_Space_O(n*sums) | 91 | 74.38 +| 0078 |[Subsets](src/main/ts/g0001_0100/s0078_subsets)| 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) | 50 | 94.61 +| 0076 |[Minimum Window Substring](src/main/ts/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer, Big_O_Time_O(s.length())_Space_O(1) | 82 | 82.17 +| 0075 |[Sort Colors](src/main/ts/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_2_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 43 | 97.41 +| 0074 |[Search a 2D Matrix](src/main/ts/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Data_Structure_I_Day_5_Array, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_8, Level_2_Day_8_Binary_Search, Udemy_2D_Arrays/Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 45 | 96.82 +| 0073 |[Set Matrix Zeroes](src/main/ts/g0001_0100/s0073_set_matrix_zeroes)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Udemy_2D_Arrays/Matrix, Big_O_Time_O(m*n)_Space_O(1) | 61 | 97.66 | 0072 |[Edit Distance](src/main/ts/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 68 | 95.20 | 0070 |[Climbing Stairs](src/main/ts/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 44 | 94.58 | 0064 |[Minimum Path Sum](src/main/ts/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming, Big_O_Time_O(m*n)_Space_O(m*n) | 48 | 99.67 diff --git a/src/main/ts/g0001_0100/s0073_set_matrix_zeroes/readme.md b/src/main/ts/g0001_0100/s0073_set_matrix_zeroes/readme.md new file mode 100644 index 0000000..cff98fc --- /dev/null +++ b/src/main/ts/g0001_0100/s0073_set_matrix_zeroes/readme.md @@ -0,0 +1,71 @@ +[![](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) + +## 73\. Set Matrix Zeroes + +Medium + +Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s, and return _the matrix_. + +You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg) + +**Input:** matrix = \[\[1,1,1],[1,0,1],[1,1,1]] + +**Output:** [[1,0,1],[0,0,0],[1,0,1]] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg) + +**Input:** matrix = \[\[0,1,2,0],[3,4,5,2],[1,3,1,5]] + +**Output:** [[0,0,0,0],[0,4,5,0],[0,3,1,0]] + +**Constraints:** + +* `m == matrix.length` +* `n == matrix[0].length` +* `1 <= m, n <= 200` +* -231 <= matrix[i][j] <= 231 - 1 + +**Follow up:** + +* A straightforward solution using `O(mn)` space is probably a bad idea. +* A simple improvement uses `O(m + n)` space, but still not the best solution. +* Could you devise a constant space solution? + +## Solution + +```typescript +/* + Do not return anything, modify matrix in-place instead. + */ +function setZeroes(matrix: number[][]): void { //NOSONAR + const cols: number[] = [] + const rows: number[] = [] + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + if (matrix[i][j] === 0) { + if (!rows.includes(i)) rows.push(i) + if (!cols.includes(j)) cols.push(j) + } + } + } + for (const row of matrix) { + for (const col of cols) { + row[col] = 0 + } + } + for (let j = 0; j < rows.length; j++) { + for (let i = 0; i < matrix[j].length; i++) { + matrix[rows[j]][i] = 0 + } + } +} + +export { setZeroes } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0074_search_a_2d_matrix/readme.md b/src/main/ts/g0001_0100/s0074_search_a_2d_matrix/readme.md new file mode 100644 index 0000000..c1fefeb --- /dev/null +++ b/src/main/ts/g0001_0100/s0074_search_a_2d_matrix/readme.md @@ -0,0 +1,58 @@ +[![](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) + +## 74\. Search a 2D Matrix + +Medium + +Write an efficient algorithm that searches for a value in an `m x n` matrix. This matrix has the following properties: + +* Integers in each row are sorted from left to right. +* The first integer of each row is greater than the last integer of the previous row. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/10/05/mat.jpg) + +**Input:** matrix = \[\[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 + +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg) + +**Input:** matrix = \[\[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 + +**Output:** false + +**Constraints:** + +* `m == matrix.length` +* `n == matrix[i].length` +* `1 <= m, n <= 100` +* -104 <= matrix[i][j], target <= 104 + +## Solution + +```typescript +function searchMatrix(matrix: number[][], target: number): boolean { + const [m, n] = [matrix.length, matrix[0].length] + let [l, r] = [0, m * n - 1] + while (l <= r) { + const mid = Math.floor((l + r) / 2) + const row = Math.floor(mid / n) + const col = mid % n + const el = matrix[row][col] + if (el === target) return true + if (el < target) { + l = mid + 1 + } else { + r = mid - 1 + } + } + return false +} + +export { searchMatrix } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0075_sort_colors/readme.md b/src/main/ts/g0001_0100/s0075_sort_colors/readme.md new file mode 100644 index 0000000..356a834 --- /dev/null +++ b/src/main/ts/g0001_0100/s0075_sort_colors/readme.md @@ -0,0 +1,65 @@ +[![](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) + +## 75\. Sort Colors + +Medium + +Given an array `nums` with `n` objects colored red, white, or blue, sort them **[in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** so that objects of the same color are adjacent, with the colors in the order red, white, and blue. + +We will use the integers `0`, `1`, and `2` to represent the color red, white, and blue, respectively. + +You must solve this problem without using the library's sort function. + +**Example 1:** + +**Input:** nums = [2,0,2,1,1,0] + +**Output:** [0,0,1,1,2,2] + +**Example 2:** + +**Input:** nums = [2,0,1] + +**Output:** [0,1,2] + +**Example 3:** + +**Input:** nums = [0] + +**Output:** [0] + +**Example 4:** + +**Input:** nums = [1] + +**Output:** [1] + +**Constraints:** + +* `n == nums.length` +* `1 <= n <= 300` +* `nums[i]` is `0`, `1`, or `2`. + +**Follow up:** Could you come up with a one-pass algorithm using only constant extra space? + +## Solution + +```typescript +const sortColors = (nums: number[]) => { + let i = 0 + let num2s = 0 + while (i < nums.length) { + const num = nums.shift() + if (num === 0) nums.splice(nums.length - i, 0, 0) + else if (num === 1) nums.splice(nums.length - num2s, 0, 1) + else { + num2s++ + nums.push(2) + } + i++ + } +} + +export { sortColors } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0076_minimum_window_substring/readme.md b/src/main/ts/g0001_0100/s0076_minimum_window_substring/readme.md new file mode 100644 index 0000000..82fa2dc --- /dev/null +++ b/src/main/ts/g0001_0100/s0076_minimum_window_substring/readme.md @@ -0,0 +1,90 @@ +[![](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) + +## 76\. Minimum Window Substring + +Hard + +Given two strings `s` and `t` of lengths `m` and `n` respectively, return _the **minimum window substring** of_ `s` _such that every character in_ `t` _(**including duplicates**) is included in the window. If there is no such substring__, return the empty string_ `""`_._ + +The testcases will be generated such that the answer is **unique**. + +A **substring** is a contiguous sequence of characters within the string. + +**Example 1:** + +**Input:** s = "ADOBECODEBANC", t = "ABC" + +**Output:** "BANC" + +**Explanation:** The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t. + +**Example 2:** + +**Input:** s = "a", t = "a" + +**Output:** "a" + +**Explanation:** The entire string s is the minimum window. + +**Example 3:** + +**Input:** s = "a", t = "aa" + +**Output:** "" + +**Explanation:** Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string. + +**Constraints:** + +* `m == s.length` +* `n == t.length` +* 1 <= m, n <= 105 +* `s` and `t` consist of uppercase and lowercase English letters. + +**Follow up:** Could you find an algorithm that runs in `O(m + n)` time? + +## Solution + +```typescript +function minWindow(s: string, t: string): string { //NOSONAR + const map: Map = new Map() + for (const char of t) { + if (map.has(char)) { + map.set(char, map.get(char) + 1) + } else { + map.set(char, 1) + } + } + let minStartIdx = -Infinity, + minEndIdx = Infinity + let remainingChars = t.length + let startIdx = 0 + for (let endIdx = 0; endIdx < s.length; endIdx++) { + const endChar = s[endIdx] + if (map.has(endChar)) { + map.set(endChar, map.get(endChar) - 1) + if (map.get(endChar) >= 0) { + remainingChars-- + } + } + while (remainingChars === 0) { + if (minEndIdx - minStartIdx > endIdx - startIdx) { + minStartIdx = startIdx + minEndIdx = endIdx + } + const startChar = s[startIdx] + if (map.has(startChar)) { + map.set(startChar, map.get(startChar) + 1) + } + if (map.get(startChar) > 0) { + remainingChars++ + } + startIdx++ + } + } + return minStartIdx === -Infinity ? '' : s.slice(minStartIdx, minEndIdx + 1) +} + +export { minWindow } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0078_subsets/readme.md b/src/main/ts/g0001_0100/s0078_subsets/readme.md new file mode 100644 index 0000000..00a5be3 --- /dev/null +++ b/src/main/ts/g0001_0100/s0078_subsets/readme.md @@ -0,0 +1,40 @@ +[![](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) + +## 78\. Subsets + +Medium + +Given an integer array `nums` of **unique** elements, return _all possible subsets (the power set)_. + +The solution set **must not** contain duplicate subsets. Return the solution in **any order**. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] + +**Example 2:** + +**Input:** nums = [0] + +**Output:** [[],[0]] + +**Constraints:** + +* `1 <= nums.length <= 10` +* `-10 <= nums[i] <= 10` +* All the numbers of `nums` are **unique**. + +## Solution + +```typescript +const subsets = (nums: number[]): number[][] => { + const sets: number[][] = [[]] + for (const num of nums) sets.push(...sets.map((set) => [...set, num])) + return sets +} + +export { subsets } +``` \ No newline at end of file