Skip to content

Commit 567ec73

Browse files
authoredDec 11, 2024··
Added tasks 73-78
1 parent 1be6773 commit 567ec73

File tree

16 files changed

+492
-0
lines changed

16 files changed

+492
-0
lines changed
 

‎README.md

+16
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
110110
|-|-|-|-|-|-
111111
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/js/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
112112
| 0033 |[Search in Rotated Sorted Array](src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
113+
| 0074 |[Search a 2D Matrix](src/main/js/g0001_0100/s0074_search_a_2d_matrix/solution.js)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 0 | 100.00
113114

114115
#### Day 2 Binary Search
115116

@@ -152,6 +153,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
152153

153154
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
154155
|-|-|-|-|-|-
156+
| 0078 |[Subsets](src/main/js/g0001_0100/s0078_subsets/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 0 | 100.00
155157

156158
#### Day 10 Recursion Backtracking
157159

@@ -264,6 +266,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
264266

265267
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
266268
|-|-|-|-|-|-
269+
| 0074 |[Search a 2D Matrix](src/main/js/g0001_0100/s0074_search_a_2d_matrix/solution.js)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 0 | 100.00
267270

268271
#### Day 9
269272

@@ -918,6 +921,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
918921

919922
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
920923
|-|-|-|-|-|-
924+
| 0074 |[Search a 2D Matrix](src/main/js/g0001_0100/s0074_search_a_2d_matrix/solution.js)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 0 | 100.00
921925
| 0033 |[Search in Rotated Sorted Array](src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
922926

923927
#### Day 9 Binary Search Tree
@@ -950,6 +954,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
950954
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
951955
|-|-|-|-|-|-
952956
| 0003 |[Longest Substring Without Repeating Characters](src/main/js/g0001_0100/s0003_longest_substring_without_repeating_characters/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1), AI_can_be_used_to_solve_the_task | 3 | 98.96
957+
| 0076 |[Minimum Window Substring](src/main/js/g0001_0100/s0076_minimum_window_substring/solution.js)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(s.length())_Space_O(1) | 4 | 99.86
953958

954959
#### Day 15 Tree
955960

@@ -1014,6 +1019,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
10141019
|-|-|-|-|-|-
10151020
| 0001 |[Two Sum](src/main/js/g0001_0100/s0001_two_sum/solution.js)| 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.15
10161021
| 0055 |[Jump Game](src/main/js/g0001_0100/s0055_jump_game/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1022+
| 0075 |[Sort Colors](src/main/js/g0001_0100/s0075_sort_colors/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10171023
| 0041 |[First Missing Positive](src/main/js/g0001_0100/s0041_first_missing_positive/solution.js)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 1 | 98.86
10181024

10191025
#### Udemy Two Pointers
@@ -1038,7 +1044,9 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
10381044

10391045
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10401046
|-|-|-|-|-|-
1047+
| 0074 |[Search a 2D Matrix](src/main/js/g0001_0100/s0074_search_a_2d_matrix/solution.js)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 0 | 100.00
10411048
| 0048 |[Rotate Image](src/main/js/g0001_0100/s0048_rotate_image/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
1049+
| 0073 |[Set Matrix Zeroes](src/main/js/g0001_0100/s0073_set_matrix_zeroes/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Big_O_Time_O(m\*n)_Space_O(1) | 0 | 100.00
10421050
| 0056 |[Merge Intervals](src/main/js/g0001_0100/s0056_merge_intervals/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 81.68
10431051

10441052
#### Udemy Linked List
@@ -1079,6 +1087,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
10791087
|-|-|-|-|-|-
10801088
| 0022 |[Generate Parentheses](src/main/js/g0001_0100/s0022_generate_parentheses/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
10811089
| 0039 |[Combination Sum](src/main/js/g0001_0100/s0039_combination_sum/solution.js)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 1 | 98.51
1090+
| 0078 |[Subsets](src/main/js/g0001_0100/s0078_subsets/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Big_O_Time_O(2^n)_Space_O(n\*2^n) | 0 | 100.00
10821091
| 0017 |[Letter Combinations of a Phone Number](src/main/js/g0001_0100/s0017_letter_combinations_of_a_phone_number/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 0 | 100.00
10831092
| 0046 |[Permutations](src/main/js/g0001_0100/s0046_permutations/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 0 | 100.00
10841093

@@ -1120,6 +1129,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
11201129

11211130
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11221131
|-|-|-|-|-|-
1132+
| 0074 |[Search a 2D Matrix](src/main/js/g0001_0100/s0074_search_a_2d_matrix/solution.js)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 0 | 100.00
11231133

11241134
#### Day 6 String
11251135

@@ -1180,6 +1190,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
11801190

11811191
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11821192
|-|-|-|-|-|-
1193+
| 0075 |[Sort Colors](src/main/js/g0001_0100/s0075_sort_colors/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
11831194
| 0056 |[Merge Intervals](src/main/js/g0001_0100/s0056_merge_intervals/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 81.68
11841195

11851196
#### Day 3 Array
@@ -1287,6 +1298,11 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
12871298

12881299
| # | Title | Difficulty | Tag | Time, ms | Time, %
12891300
|------|----------------|-------------|-------------|----------|---------
1301+
| 0078 |[Subsets](src/main/js/g0001_0100/s0078_subsets/solution.js)| 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
1302+
| 0076 |[Minimum Window Substring](src/main/js/g0001_0100/s0076_minimum_window_substring/solution.js)| 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) | 4 | 99.86
1303+
| 0075 |[Sort Colors](src/main/js/g0001_0100/s0075_sort_colors/solution.js)| 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) | 0 | 100.00
1304+
| 0074 |[Search a 2D Matrix](src/main/js/g0001_0100/s0074_search_a_2d_matrix/solution.js)| 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) | 0 | 100.00
1305+
| 0073 |[Set Matrix Zeroes](src/main/js/g0001_0100/s0073_set_matrix_zeroes/solution.js)| 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) | 0 | 100.00
12901306
| 0072 |[Edit Distance](src/main/js/g0001_0100/s0072_edit_distance/solution.js)| Medium | 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) | 5 | 99.01
12911307
| 0070 |[Climbing Stairs](src/main/js/g0001_0100/s0070_climbing_stairs/solution.js)| 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) | 0 | 100.00
12921308
| 0064 |[Minimum Path Sum](src/main/js/g0001_0100/s0064_minimum_path_sum/solution.js)| 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) | 3 | 83.07
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
73\. Set Matrix Zeroes
2+
3+
Medium
4+
5+
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s.
6+
7+
You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm).
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg)
12+
13+
**Input:** matrix = [[1,1,1],[1,0,1],[1,1,1]]
14+
15+
**Output:** [[1,0,1],[0,0,0],[1,0,1]]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg)
20+
21+
**Input:** matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
22+
23+
**Output:** [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
24+
25+
**Constraints:**
26+
27+
* `m == matrix.length`
28+
* `n == matrix[0].length`
29+
* `1 <= m, n <= 200`
30+
* <code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code>
31+
32+
**Follow up:**
33+
34+
* A straightforward solution using `O(mn)` space is probably a bad idea.
35+
* A simple improvement uses `O(m + n)` space, but still not the best solution.
36+
* Could you devise a constant space solution?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Matrix
2+
// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(m*n)_Space_O(1)
3+
// #2024_12_11_Time_0_ms_(100.00%)_Space_54_MB_(33.87%)
4+
5+
/**
6+
* @param {number[][]} matrix
7+
* @return {void} Do not return anything, modify matrix in-place instead.
8+
*/
9+
var setZeroes = function(matrix) {
10+
const m = matrix.length
11+
const n = matrix[0].length
12+
let row0 = false
13+
let col0 = false
14+
15+
// Check if the first column needs to be set to 0 in the future
16+
for (let i = 0; i < m; i++) {
17+
if (matrix[i][0] === 0) {
18+
col0 = true
19+
break
20+
}
21+
}
22+
23+
// Check if the first row needs to be set to 0 in the future
24+
for (let j = 0; j < n; j++) {
25+
if (matrix[0][j] === 0) {
26+
row0 = true
27+
break
28+
}
29+
}
30+
31+
// Store the signals in the first row and first column
32+
for (let i = 1; i < m; i++) {
33+
for (let j = 1; j < n; j++) {
34+
if (matrix[i][j] === 0) {
35+
matrix[i][0] = 0
36+
matrix[0][j] = 0
37+
}
38+
}
39+
}
40+
41+
// Mark cells as 0 based on the signals in the first row and column
42+
for (let i = 1; i < m; i++) {
43+
for (let j = 1; j < n; j++) {
44+
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
45+
matrix[i][j] = 0
46+
}
47+
}
48+
}
49+
50+
// Set the first column to 0 if needed
51+
if (col0) {
52+
for (let i = 0; i < m; i++) {
53+
matrix[i][0] = 0
54+
}
55+
}
56+
57+
// Set the first row to 0 if needed
58+
if (row0) {
59+
for (let j = 0; j < n; j++) {
60+
matrix[0][j] = 0
61+
}
62+
}
63+
};
64+
65+
export { setZeroes }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
74\. Search a 2D Matrix
2+
3+
Medium
4+
5+
Write an efficient algorithm that searches for a value `target` in an `m x n` integer matrix `matrix`. This matrix has the following properties:
6+
7+
* Integers in each row are sorted from left to right.
8+
* The first integer of each row is greater than the last integer of the previous row.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/10/05/mat.jpg)
13+
14+
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/10/05/mat2.jpg)
21+
22+
**Input:** matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* `m == matrix.length`
29+
* `n == matrix[i].length`
30+
* `1 <= m, n <= 100`
31+
* <code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Data_Structure_I_Day_5_Array
2+
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_8 #Level_2_Day_8_Binary_Search
3+
// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(endRow+endCol)_Space_O(1)
4+
// #2024_12_11_Time_0_ms_(100.00%)_Space_49.1_MB_(63.49%)
5+
6+
/**
7+
* @param {number[][]} matrix
8+
* @param {number} target
9+
* @return {boolean}
10+
*/
11+
var searchMatrix = function(matrix, target) {
12+
const endRow = matrix.length
13+
const endCol = matrix[0].length
14+
let targetRow = 0
15+
let result = false
16+
17+
// Find the target row
18+
for (let i = 0; i < endRow; i++) {
19+
if (matrix[i][endCol - 1] >= target) {
20+
targetRow = i
21+
break
22+
}
23+
}
24+
25+
// Search in the target row
26+
for (let i = 0; i < endCol; i++) {
27+
if (matrix[targetRow][i] === target) {
28+
result = true
29+
break
30+
}
31+
}
32+
33+
return result
34+
};
35+
36+
export { searchMatrix }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
75\. Sort Colors
2+
3+
Medium
4+
5+
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.
6+
7+
We will use the integers `0`, `1`, and `2` to represent the color red, white, and blue, respectively.
8+
9+
You must solve this problem without using the library's sort function.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,0,2,1,1,0]
14+
15+
**Output:** [0,0,1,1,2,2]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [2,0,1]
20+
21+
**Output:** [0,1,2]
22+
23+
**Constraints:**
24+
25+
* `n == nums.length`
26+
* `1 <= n <= 300`
27+
* `nums[i]` is either `0`, `1`, or `2`.
28+
29+
**Follow up:** Could you come up with a one-pass algorithm using only constant extra space?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Two_Pointers
2+
// #Data_Structure_II_Day_2_Array #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
3+
// #2024_12_11_Time_0_ms_(100.00%)_Space_48.8_MB_(84.42%)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @return {void} Do not return anything, modify nums in-place instead.
8+
*/
9+
var sortColors = function(nums) {
10+
let zeroes = 0
11+
let ones = 0
12+
13+
// Count 0s and place them at the start
14+
for (let i = 0; i < nums.length; i++) {
15+
if (nums[i] === 0) {
16+
nums[zeroes++] = 0
17+
} else if (nums[i] === 1) {
18+
ones++
19+
}
20+
}
21+
22+
// Place 1s after the 0s
23+
for (let j = zeroes; j < zeroes + ones; j++) {
24+
nums[j] = 1
25+
}
26+
27+
// Place 2s after the 1s
28+
for (let k = zeroes + ones; k < nums.length; k++) {
29+
nums[k] = 2
30+
}
31+
};
32+
33+
export { sortColors }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
76\. Minimum Window Substring
2+
3+
Hard
4+
5+
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_ `""`_._
6+
7+
The testcases will be generated such that the answer is **unique**.
8+
9+
A **substring** is a contiguous sequence of characters within the string.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "ADOBECODEBANC", t = "ABC"
14+
15+
**Output:** "BANC"
16+
17+
**Explanation:** The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "a", t = "a"
22+
23+
**Output:** "a"
24+
25+
**Explanation:** The entire string s is the minimum window.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "a", t = "aa"
30+
31+
**Output:** ""
32+
33+
**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.
34+
35+
**Constraints:**
36+
37+
* `m == s.length`
38+
* `n == t.length`
39+
* <code>1 <= m, n <= 10<sup>5</sup></code>
40+
* `s` and `t` consist of uppercase and lowercase English letters.
41+
42+
**Follow up:** Could you find an algorithm that runs in `O(m + n)` time?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
2+
// #Level_2_Day_14_Sliding_Window/Two_Pointer #Big_O_Time_O(s.length())_Space_O(1)
3+
// #2024_12_11_Time_4_ms_(99.86%)_Space_51.7_MB_(98.27%)
4+
5+
/**
6+
* @param {string} s
7+
* @param {string} t
8+
* @return {string}
9+
*/
10+
var minWindow = function(s, t) {
11+
const map = new Array(128).fill(0)
12+
13+
// Populate the map with the characters of `t`
14+
for (let i = 0; i < t.length; i++) {
15+
map[t.charCodeAt(i)]++
16+
}
17+
18+
let count = t.length
19+
let begin = 0
20+
let end = 0
21+
let d = Number.MAX_VALUE
22+
let head = 0
23+
24+
while (end < s.length) {
25+
// Decrease count for characters in `s` found in `t`
26+
if (map[s.charCodeAt(end++)]-- > 0) {
27+
count--
28+
}
29+
30+
// When all characters of `t` are matched
31+
while (count === 0) {
32+
if (end - begin < d) {
33+
d = end - begin
34+
head = begin
35+
}
36+
// Move the `begin` pointer and update the map
37+
if (map[s.charCodeAt(begin++)]++ === 0) {
38+
count++
39+
}
40+
}
41+
}
42+
43+
return d === Number.MAX_VALUE ? "" : s.substring(head, head + d)
44+
};
45+
46+
export { minWindow }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
78\. Subsets
2+
3+
Medium
4+
5+
Given an integer array `nums` of **unique** elements, return _all possible subsets (the power set)_.
6+
7+
The solution set **must not** contain duplicate subsets. Return the solution in **any order**.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3]
12+
13+
**Output:** [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [0]
18+
19+
**Output:** [[],[0]]
20+
21+
**Constraints:**
22+
23+
* `1 <= nums.length <= 10`
24+
* `-10 <= nums[i] <= 10`
25+
* All the numbers of `nums` are **unique**.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Bit_Manipulation #Backtracking
2+
// #Algorithm_II_Day_9_Recursion_Backtracking #Udemy_Backtracking/Recursion
3+
// #Big_O_Time_O(2^n)_Space_O(n*2^n) #2024_12_11_Time_0_ms_(100.00%)_Space_53.5_MB_(8.72%)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @return {number[][]}
8+
*/
9+
var subsets = function(nums) {
10+
const res = []
11+
12+
const solve = (temp, start) => {
13+
// Add a copy of the current subset
14+
res.push([...temp])
15+
16+
for (let i = start; i < nums.length; i++) {
17+
// Include nums[i] in the current subset
18+
temp.push(nums[i])
19+
20+
// Recur for the next index
21+
solve(temp, i + 1)
22+
23+
// Backtrack by removing the last added element
24+
temp.pop()
25+
}
26+
}
27+
28+
solve([], 0) // Start with an empty subset and index 0
29+
return res
30+
};
31+
32+
export { subsets }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// tslint:disable:no-magic-numbers
2+
import { setZeroes } from 'src/main/js/g0001_0100/s0073_set_matrix_zeroes/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('setZeroes', () => {
6+
const matrix = [
7+
[1, 1, 1],
8+
[1, 0, 1],
9+
[1, 1, 1],
10+
]
11+
setZeroes(matrix)
12+
expect(matrix).toEqual([
13+
[1, 0, 1],
14+
[0, 0, 0],
15+
[1, 0, 1],
16+
])
17+
})
18+
19+
test('setZeroes2', () => {
20+
const matrix = [
21+
[0, 1, 2, 0],
22+
[3, 4, 5, 2],
23+
[1, 3, 1, 5],
24+
]
25+
setZeroes(matrix)
26+
expect(matrix).toEqual([
27+
[0, 0, 0, 0],
28+
[0, 4, 5, 0],
29+
[0, 3, 1, 0],
30+
])
31+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// tslint:disable:no-magic-numbers
2+
import { searchMatrix } from 'src/main/js/g0001_0100/s0074_search_a_2d_matrix/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('searchMatrix', () => {
6+
expect(
7+
searchMatrix(
8+
[
9+
[1, 3, 5, 7],
10+
[10, 11, 16, 20],
11+
[23, 30, 34, 60],
12+
],
13+
3,
14+
),
15+
).toEqual(true)
16+
})
17+
18+
test('searchMatrix2', () => {
19+
expect(
20+
searchMatrix(
21+
[
22+
[1, 3, 5, 7],
23+
[10, 11, 16, 20],
24+
[23, 30, 34, 60],
25+
],
26+
13,
27+
),
28+
).toEqual(false)
29+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// tslint:disable:no-magic-numbers
2+
import { sortColors } from 'src/main/js/g0001_0100/s0075_sort_colors/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('sortColors', () => {
6+
const arr = [2, 0, 2, 1, 1, 0]
7+
sortColors(arr)
8+
expect(arr).toEqual([0, 0, 1, 1, 2, 2])
9+
})
10+
11+
test('sortColors2', () => {
12+
const arr = [2, 0, 1]
13+
sortColors(arr)
14+
expect(arr).toEqual([0, 1, 2])
15+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// tslint:disable:no-magic-numbers
2+
import { minWindow } from 'src/main/js/g0001_0100/s0076_minimum_window_substring/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('minWindow', () => {
6+
expect(minWindow('ADOBECODEBANC', 'ABC')).toEqual('BANC')
7+
})
8+
9+
test('minWindow2', () => {
10+
expect(minWindow('a', 'a')).toEqual('a')
11+
})
12+
13+
test('minWindow3', () => {
14+
expect(minWindow('a', 'aa')).toEqual('')
15+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// tslint:disable:no-magic-numbers
2+
import { subsets } from 'src/main/js/g0001_0100/s0078_subsets/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('subsets', () => {
6+
expect(subsets([1, 2, 3])).toEqual([[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]])
7+
})
8+
9+
test('subsets2', () => {
10+
expect(subsets([0])).toEqual([[], [0]])
11+
})

0 commit comments

Comments
 (0)
Please sign in to comment.