Skip to content

Commit 9a21aa2

Browse files
authored
Added tasks 373-433
1 parent a1ea4d7 commit 9a21aa2

File tree

16 files changed

+577
-0
lines changed

16 files changed

+577
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
319319

320320
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
321321
|-|-|-|-|-|-
322+
| 0392 |[Is Subsequence](src/main/ts/g0301_0400/s0392_is_subsequence/solution.ts)| Easy | String, Dynamic_Programming, Two_Pointers | 0 | 100.00
322323
| 1143 |[Longest Common Subsequence](src/main/ts/g1101_1200/s1143_longest_common_subsequence/solution.ts)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 50 | 69.40
323324
| 0072 |[Edit Distance](src/main/ts/g0001_0100/s0072_edit_distance/solution.ts)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 6 | 93.83
324325

@@ -579,6 +580,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
579580

580581
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
581582
|-|-|-|-|-|-
583+
| 0433 |[Minimum Genetic Mutation](src/main/ts/g0401_0500/s0433_minimum_genetic_mutation/solution.ts)| Medium | String, Hash_Table, Breadth_First_Search | 0 | 100.00
582584
| 0127 |[Word Ladder](src/main/ts/g0101_0200/s0127_word_ladder/solution.ts)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search | 41 | 95.63
583585

584586
#### Day 13 Graph Theory
@@ -655,6 +657,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
655657
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
656658
|-|-|-|-|-|-
657659
| 0205 |[Isomorphic Strings](src/main/ts/g0201_0300/s0205_isomorphic_strings/solution.ts)| Easy | String, Hash_Table | 3 | 96.02
660+
| 0392 |[Is Subsequence](src/main/ts/g0301_0400/s0392_is_subsequence/solution.ts)| Easy | String, Dynamic_Programming, Two_Pointers | 0 | 100.00
658661

659662
#### Day 3 Linked List
660663

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

919922
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
920923
|-|-|-|-|-|-
924+
| 0392 |[Is Subsequence](src/main/ts/g0301_0400/s0392_is_subsequence/solution.ts)| Easy | String, Dynamic_Programming, Two_Pointers | 0 | 100.00
921925
| 0125 |[Valid Palindrome](src/main/ts/g0101_0200/s0125_valid_palindrome/solution.ts)| Easy | Top_Interview_Questions, String, Two_Pointers | 0 | 100.00
922926
| 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
923927
| 0042 |[Trapping Rain Water](src/main/ts/g0001_0100/s0042_trapping_rain_water/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
@@ -1062,6 +1066,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10621066
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10631067
|-|-|-|-|-|-
10641068
| 0125 |[Valid Palindrome](src/main/ts/g0101_0200/s0125_valid_palindrome/solution.ts)| Easy | Top_Interview_Questions, String, Two_Pointers | 0 | 100.00
1069+
| 0392 |[Is Subsequence](src/main/ts/g0301_0400/s0392_is_subsequence/solution.ts)| Easy | String, Dynamic_Programming, Two_Pointers | 0 | 100.00
10651070
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts)| Medium | Array, Binary_Search, Two_Pointers | 0 | 100.00
10661071
| 0011 |[Container With Most Water](src/main/ts/g0001_0100/s0011_container_with_most_water/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 2 | 80.13
10671072
| 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
@@ -1089,6 +1094,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10891094

10901095
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10911096
|-|-|-|-|-|-
1097+
| 0383 |[Ransom Note](src/main/ts/g0301_0400/s0383_ransom_note/solution.ts)| Easy | String, Hash_Table, Counting | 4 | 97.40
10921098
| 0205 |[Isomorphic Strings](src/main/ts/g0201_0300/s0205_isomorphic_strings/solution.ts)| Easy | String, Hash_Table | 3 | 96.02
10931099
| 0290 |[Word Pattern](src/main/ts/g0201_0300/s0290_word_pattern/solution.ts)| Easy | String, Hash_Table | 0 | 100.00
10941100
| 0242 |[Valid Anagram](src/main/ts/g0201_0300/s0242_valid_anagram/solution.ts)| Easy | String, Hash_Table, Sorting | 4 | 97.99
@@ -1177,6 +1183,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11771183

11781184
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11791185
|-|-|-|-|-|-
1186+
| 0433 |[Minimum Genetic Mutation](src/main/ts/g0401_0500/s0433_minimum_genetic_mutation/solution.ts)| Medium | String, Hash_Table, Breadth_First_Search | 0 | 100.00
11801187
| 0127 |[Word Ladder](src/main/ts/g0101_0200/s0127_word_ladder/solution.ts)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search | 41 | 95.63
11811188

11821189
#### Top Interview 150 Trie
@@ -1205,6 +1212,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12051212
|-|-|-|-|-|-
12061213
| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 0 | 100.00
12071214
| 0148 |[Sort List](src/main/ts/g0101_0200/s0148_sort_list/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(log(N))_Space_O(log(N)) | 36 | 44.94
1215+
| 0427 |[Construct Quad Tree](src/main/ts/g0401_0500/s0427_construct_quad_tree/solution.ts)| Medium | Array, Tree, Matrix, Divide_and_Conquer | ew 150 | ew 150 Divide and Conquer
12081216
| 0023 |[Merge k Sorted Lists](src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Heap_Priority_Queue, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(k\*n\*log(k))_Space_O(log(k)) | 4 | 97.65
12091217

12101218
#### Top Interview 150 Kadane's Algorithm
@@ -1230,6 +1238,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12301238
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12311239
|-|-|-|-|-|-
12321240
| 0215 |[Kth Largest Element in an Array](src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 4 | 99.64
1241+
| 0373 |[Find K Pairs with Smallest Sums](src/main/ts/g0301_0400/s0373_find_k_pairs_with_smallest_sums/solution.ts)| Medium | Array, Heap_Priority_Queue | 42 | 85.15
12331242
| 0295 |[Find Median from Data Stream](src/main/ts/g0201_0300/s0295_find_median_from_data_stream/solution.ts)| Hard | Top_100_Liked_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 106 | 92.31
12341243

12351244
#### Top Interview 150 Bit Manipulation
@@ -1315,6 +1324,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
13151324

13161325
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13171326
|-|-|-|-|-|-
1327+
| 0383 |[Ransom Note](src/main/ts/g0301_0400/s0383_ransom_note/solution.ts)| Easy | String, Hash_Table, Counting | 4 | 97.40
13181328
| 0242 |[Valid Anagram](src/main/ts/g0201_0300/s0242_valid_anagram/solution.ts)| Easy | String, Hash_Table, Sorting | 4 | 97.99
13191329

13201330
#### Day 7 Linked List
@@ -1741,8 +1751,13 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
17411751
| 0494 |[Target Sum](src/main/ts/g0401_0500/s0494_target_sum/solution.ts)| Medium | Array, Dynamic_Programming, Backtracking, Big_O_Time_O(n\*(sum+s))_Space_O(n\*(sum+s)) | 24 | 83.43
17421752
| 0438 |[Find All Anagrams in a String](src/main/ts/g0401_0500/s0438_find_all_anagrams_in_a_string/solution.ts)| 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) | 8 | 97.80
17431753
| 0437 |[Path Sum III](src/main/ts/g0401_0500/s0437_path_sum_iii/solution.ts)| Medium | Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Big_O_Time_O(n)_Space_O(n) | 3 | 86.41
1754+
| 0433 |[Minimum Genetic Mutation](src/main/ts/g0401_0500/s0433_minimum_genetic_mutation/solution.ts)| Medium | String, Hash_Table, Breadth_First_Search, Graph_Theory_I_Day_12_Breadth_First_Search, Top_Interview_150_Graph_BFS | 0 | 100.00
1755+
| 0427 |[Construct Quad Tree](src/main/ts/g0401_0500/s0427_construct_quad_tree/solution.ts)| Medium | Array, Tree, Matrix, Divide_and_Conquer | ew 150 | ew 150 Divide and Conquer
17441756
| 0416 |[Partition Equal Subset Sum](src/main/ts/g0401_0500/s0416_partition_equal_subset_sum/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 33 | 93.24
17451757
| 0394 |[Decode String](src/main/ts/g0301_0400/s0394_decode_string/solution.ts)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1758+
| 0392 |[Is Subsequence](src/main/ts/g0301_0400/s0392_is_subsequence/solution.ts)| Easy | String, Dynamic_Programming, Two_Pointers, Dynamic_Programming_I_Day_19, Level_1_Day_2_String, Udemy_Two_Pointers, Top_Interview_150_Two_Pointers | 0 | 100.00
1759+
| 0383 |[Ransom Note](src/main/ts/g0301_0400/s0383_ransom_note/solution.ts)| Easy | String, Hash_Table, Counting, Data_Structure_I_Day_6_String, Top_Interview_150_Hashmap | 4 | 97.40
1760+
| 0373 |[Find K Pairs with Smallest Sums](src/main/ts/g0301_0400/s0373_find_k_pairs_with_smallest_sums/solution.ts)| Medium | Array, Heap_Priority_Queue, Top_Interview_150_Heap | 42 | 85.15
17461761
| 0347 |[Top K Frequent Elements](src/main/ts/g0301_0400/s0347_top_k_frequent_elements/solution.ts)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(k) | 7 | 87.13
17471762
| 0338 |[Counting Bits](src/main/ts/g0301_0400/s0338_counting_bits/solution.ts)| Easy | Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 1 | 89.22
17481763
| 0322 |[Coin Change](src/main/ts/g0301_0400/s0322_coin_change/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(m\*n)_Space_O(amount) | 27 | 89.42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
373\. Find K Pairs with Smallest Sums
2+
3+
Medium
4+
5+
You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.
6+
7+
Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.
8+
9+
Return _the_ `k` _pairs_ <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v<sub>2</sub>), ..., (u<sub>k</sub>, v<sub>k</sub>)</code> _with the smallest sums_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = [1,7,11], nums2 = [2,4,6], k = 3
14+
15+
**Output:** [[1,2],[1,4],[1,6]]
16+
17+
**Explanation:** The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
18+
19+
**Example 2:**
20+
21+
**Input:** nums1 = [1,1,2], nums2 = [1,2,3], k = 2
22+
23+
**Output:** [[1,1],[1,1]]
24+
25+
**Explanation:** The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
26+
27+
**Example 3:**
28+
29+
**Input:** nums1 = [1,2], nums2 = [3], k = 3
30+
31+
**Output:** [[1,3],[2,3]]
32+
33+
**Explanation:** All possible pairs are returned from the sequence: [1,3],[2,3]
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code>
38+
* <code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
39+
* `nums1` and `nums2` both are sorted in **ascending order**.
40+
* `1 <= k <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// #Medium #Array #Heap_Priority_Queue #Top_Interview_150_Heap
2+
// #2025_04_14_Time_42_ms_(85.15%)_Space_85.10_MB_(76.24%)
3+
4+
class MinHeap {
5+
private heap: { sum: number; i: number; j: number }[]
6+
7+
constructor() {
8+
this.heap = []
9+
}
10+
11+
push(val: { sum: number; i: number; j: number }) {
12+
this.heap.push(val)
13+
this.bubbleUp()
14+
}
15+
16+
pop(): { sum: number; i: number; j: number } | undefined {
17+
if (this.heap.length === 0) {
18+
return undefined
19+
}
20+
if (this.heap.length === 1) {
21+
return this.heap.pop()
22+
}
23+
const min = this.heap[0]
24+
this.heap[0] = this.heap.pop()!
25+
this.bubbleDown()
26+
return min
27+
}
28+
29+
isEmpty(): boolean {
30+
return this.heap.length === 0
31+
}
32+
33+
private bubbleUp() {
34+
let index = this.heap.length - 1
35+
while (index > 0) {
36+
let parentIndex = Math.floor((index - 1) / 2)
37+
if (this.heap[parentIndex].sum <= this.heap[index].sum) {
38+
break
39+
}
40+
;[this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]]
41+
index = parentIndex
42+
}
43+
}
44+
45+
private bubbleDown() {
46+
let index = 0
47+
let length = this.heap.length
48+
while (true) {
49+
let leftChildIndex = 2 * index + 1
50+
let rightChildIndex = 2 * index + 2
51+
let smallest = index
52+
if (leftChildIndex < length && this.heap[leftChildIndex].sum < this.heap[smallest].sum) {
53+
smallest = leftChildIndex
54+
}
55+
if (rightChildIndex < length && this.heap[rightChildIndex].sum < this.heap[smallest].sum) {
56+
smallest = rightChildIndex
57+
}
58+
if (smallest === index) {
59+
break
60+
}
61+
;[this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]]
62+
index = smallest
63+
}
64+
}
65+
}
66+
67+
function kSmallestPairs(nums1: number[], nums2: number[], k: number): number[][] {
68+
let ans: number[][] = []
69+
if (nums1.length === 0 || nums2.length === 0 || k === 0) {
70+
return ans
71+
}
72+
let minHeap = new MinHeap()
73+
for (let i = 0; i < Math.min(nums1.length, k); i++) {
74+
minHeap.push({ sum: nums1[i] + nums2[0], i, j: 0 })
75+
}
76+
while (k-- > 0 && !minHeap.isEmpty()) {
77+
let { i, j } = minHeap.pop()!
78+
ans.push([nums1[i], nums2[j]])
79+
if (j + 1 < nums2.length) {
80+
minHeap.push({ sum: nums1[i] + nums2[j + 1], i, j: j + 1 })
81+
}
82+
}
83+
return ans
84+
}
85+
86+
export { kSmallestPairs }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
383\. Ransom Note
2+
3+
Easy
4+
5+
Given two stings `ransomNote` and `magazine`, return `true` if `ransomNote` can be constructed from `magazine` and `false` otherwise.
6+
7+
Each letter in `magazine` can only be used once in `ransomNote`.
8+
9+
**Example 1:**
10+
11+
**Input:** ransomNote = "a", magazine = "b"
12+
13+
**Output:** false
14+
15+
**Example 2:**
16+
17+
**Input:** ransomNote = "aa", magazine = "ab"
18+
19+
**Output:** false
20+
21+
**Example 3:**
22+
23+
**Input:** ransomNote = "aa", magazine = "aab"
24+
25+
**Output:** true
26+
27+
**Constraints:**
28+
29+
* <code>1 <= ransomNote.length, magazine.length <= 10<sup>5</sup></code>
30+
* `ransomNote` and `magazine` consist of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// #Easy #String #Hash_Table #Counting #Data_Structure_I_Day_6_String #Top_Interview_150_Hashmap
2+
// #2025_04_14_Time_4_ms_(97.40%)_Space_57.51_MB_(84.32%)
3+
4+
function canConstruct(ransomNote: string, magazine: string): boolean {
5+
const freq: number[] = new Array(26).fill(0)
6+
let remaining = ransomNote.length
7+
for (let i = 0; i < remaining; i++) {
8+
freq[ransomNote.charCodeAt(i) - 97]++
9+
}
10+
for (let i = 0; i < magazine.length && remaining > 0; i++) {
11+
const index = magazine.charCodeAt(i) - 97
12+
if (freq[index] > 0) {
13+
freq[index]--
14+
remaining--
15+
}
16+
}
17+
return remaining === 0
18+
}
19+
20+
export { canConstruct }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
392\. Is Subsequence
2+
3+
Easy
4+
5+
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_.
6+
7+
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., `"ace"` is a subsequence of `"abcde"` while `"aec"` is not).
8+
9+
**Example 1:**
10+
11+
**Input:** s = "abc", t = "ahbgdc"
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** s = "axc", t = "ahbgdc"
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* `0 <= s.length <= 100`
24+
* <code>0 <= t.length <= 10<sup>4</sup></code>
25+
* `s` and `t` consist only of lowercase English letters.
26+
27+
**Follow up:** Suppose there are lots of incoming `s`, say <code>s<sub>1</sub>, s<sub>2</sub>, ..., s<sub>k</sub></code> where <code>k >= 10<sup>9</sup></code>, and you want to check one by one to see if `t` has its subsequence. In this scenario, how would you change your code?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// #Easy #String #Dynamic_Programming #Two_Pointers #Dynamic_Programming_I_Day_19
2+
// #Level_1_Day_2_String #Udemy_Two_Pointers #Top_Interview_150_Two_Pointers
3+
// #2025_04_14_Time_0_ms_(100.00%)_Space_56.51_MB_(36.22%)
4+
5+
function isSubsequence(s: string, t: string): boolean {
6+
let i = 0
7+
let j = 0
8+
const m = s.length
9+
const n = t.length
10+
if (m === 0) {
11+
return true
12+
}
13+
while (j < n) {
14+
if (s[i] === t[j]) {
15+
i++
16+
if (i === m) {
17+
return true
18+
}
19+
}
20+
j++
21+
}
22+
return false
23+
}
24+
25+
export { isSubsequence }

0 commit comments

Comments
 (0)