diff --git a/README.md b/README.md index c69e9aa..2c6669c 100644 --- a/README.md +++ b/README.md @@ -518,6 +518,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0380 |[Insert Delete GetRandom O(1)](src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution.ts)| Medium | Array, Hash_Table, Math, Design, Randomized | 73 | 82.52 ### Graph Theory I @@ -834,6 +835,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0100 |[Same Tree](src/main/ts/g0001_0100/s0100_same_tree/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 | 0101 |[Symmetric Tree](src/main/ts/g0101_0200/s0101_symmetric_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00 | 0199 |[Binary Tree Right Side View](src/main/ts/g0101_0200/s0199_binary_tree_right_side_view/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 @@ -976,6 +978,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 | 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 | 0543 |[Diameter of Binary Tree](src/main/ts/g0501_0600/s0543_diameter_of_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 1 | 87.16 +| 0100 |[Same Tree](src/main/ts/g0001_0100/s0100_same_tree/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 | 0226 |[Invert Binary Tree](src/main/ts/g0201_0300/s0226_invert_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 | 0104 |[Maximum Depth of Binary Tree](src/main/ts/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00 | 0124 |[Binary Tree Maximum Path Sum](src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 2 | 71.11 @@ -1049,6 +1052,8 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 0122 |[Best Time to Buy and Sell Stock II](src/main/ts/g0101_0200/s0122_best_time_to_buy_and_sell_stock_ii/solution.ts)| Medium | Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 0 | 100.00 | 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 | 0045 |[Jump Game II](src/main/ts/g0001_0100/s0045_jump_game_ii/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 1 | 84.27 +| 0274 |[H-Index](src/main/ts/g0201_0300/s0274_h_index/solution.ts)| Medium | Array, Sorting, Counting_Sort | 0 | 100.00 +| 0380 |[Insert Delete GetRandom O(1)](src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution.ts)| Medium | Array, Hash_Table, Math, Design, Randomized | 73 | 82.52 | 0238 |[Product of Array Except Self](src/main/ts/g0201_0300/s0238_product_of_array_except_self/solution.ts)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 3 | 92.81 | 0134 |[Gas Station](src/main/ts/g0101_0200/s0134_gas_station/solution.ts)| Medium | Top_Interview_Questions, Array, Greedy | 0 | 100.00 | 0135 |[Candy](src/main/ts/g0101_0200/s0135_candy/solution.ts)| Hard | Array, Greedy | 2 | 96.15 @@ -1145,6 +1150,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- | 0104 |[Maximum Depth of Binary Tree](src/main/ts/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00 +| 0100 |[Same Tree](src/main/ts/g0001_0100/s0100_same_tree/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00 | 0226 |[Invert Binary Tree](src/main/ts/g0201_0300/s0226_invert_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00 | 0101 |[Symmetric Tree](src/main/ts/g0101_0200/s0101_symmetric_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00 | 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/ts/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Big_O_Time_O(N)_Space_O(N) | 2 | 93.38 @@ -1180,6 +1186,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- | 0200 |[Number of Islands](src/main/ts/g0101_0200/s0200_number_of_islands/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 57 | 93.94 +| 0399 |[Evaluate Division](src/main/ts/g0301_0400/s0399_evaluate_division/solution.ts)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path | 0 | 100.00 | 0207 |[Course Schedule](src/main/ts/g0201_0300/s0207_course_schedule/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 11 | 81.08 | 0210 |[Course Schedule II](src/main/ts/g0201_0300/s0210_course_schedule_ii/solution.ts)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 2 | 99.76 @@ -1217,7 +1224,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. |-|-|-|-|-|- | 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 | 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 -| 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 +| 0427 |[Construct Quad Tree](src/main/ts/g0401_0500/s0427_construct_quad_tree/solution.ts)| Medium | Array, Tree, Matrix, Divide_and_Conquer | 51 | 96.23 | 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 #### Top Interview 150 Kadane's Algorithm @@ -1765,11 +1772,13 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 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 | 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 | 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 -| 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 +| 0427 |[Construct Quad Tree](src/main/ts/g0401_0500/s0427_construct_quad_tree/solution.ts)| Medium | Array, Tree, Matrix, Divide_and_Conquer, Top_Interview_150_Divide_and_Conquer | 51 | 96.23 | 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 +| 0399 |[Evaluate Division](src/main/ts/g0301_0400/s0399_evaluate_division/solution.ts)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path, Top_Interview_150_Graph_General | 0 | 100.00 | 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 | 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 | 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 +| 0380 |[Insert Delete GetRandom O(1)](src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution.ts)| Medium | Array, Hash_Table, Math, Design, Randomized, Programming_Skills_II_Day_20, Top_Interview_150_Array/String | 73 | 82.52 | 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 | 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 | 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 @@ -1780,6 +1789,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 0289 |[Game of Life](src/main/ts/g0201_0300/s0289_game_of_life/solution.ts)| Medium | Array, Matrix, Simulation, Top_Interview_150_Matrix | 0 | 100.00 | 0287 |[Find the Duplicate Number](src/main/ts/g0201_0300/s0287_find_the_duplicate_number/solution.ts)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 5 | 88.65 | 0283 |[Move Zeroes](src/main/ts/g0201_0300/s0283_move_zeroes/solution.ts)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 1 | 82.86 +| 0274 |[H-Index](src/main/ts/g0201_0300/s0274_h_index/solution.ts)| Medium | Array, Sorting, Counting_Sort, Top_Interview_150_Array/String | 0 | 100.00 | 0242 |[Valid Anagram](src/main/ts/g0201_0300/s0242_valid_anagram/solution.ts)| Easy | String, Hash_Table, Sorting, Data_Structure_I_Day_6_String, Programming_Skills_I_Day_11_Containers_and_Libraries, Udemy_Strings, Top_Interview_150_Hashmap | 4 | 97.99 | 0240 |[Search a 2D Matrix II](src/main/ts/g0201_0300/s0240_search_a_2d_matrix_ii/solution.ts)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8, Big_O_Time_O(n+m)_Space_O(1) | 42 | 94.61 | 0239 |[Sliding Window Maximum](src/main/ts/g0201_0300/s0239_sliding_window_maximum/solution.ts)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n\*k)_Space_O(n+k) | 26 | 99.07 @@ -1853,6 +1863,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_15_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_BFS | 0 | 100.00 | 0102 |[Binary Tree Level Order Traversal](src/main/ts/g0101_0200/s0102_binary_tree_level_order_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_BFS, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00 | 0101 |[Symmetric Tree](src/main/ts/g0101_0200/s0101_symmetric_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00 +| 0100 |[Same Tree](src/main/ts/g0001_0100/s0100_same_tree/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Level_2_Day_15_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General | 0 | 100.00 | 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 | 0097 |[Interleaving String](src/main/ts/g0001_0100/s0097_interleaving_string/solution.ts)| Medium | String, Dynamic_Programming, Top_Interview_150_Multidimensional_DP | 43 | 97.65 | 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 diff --git a/src/main/ts/g0001_0100/s0098_validate_binary_search_tree/solution.ts b/src/main/ts/g0001_0100/s0098_validate_binary_search_tree/solution.ts index 13cca78..3405944 100644 --- a/src/main/ts/g0001_0100/s0098_validate_binary_search_tree/solution.ts +++ b/src/main/ts/g0001_0100/s0098_validate_binary_search_tree/solution.ts @@ -5,6 +5,19 @@ import { TreeNode } from '../../com_github_leetcode/treenode' +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ function dfs(node: TreeNode | null, lowerBound: number, upperBound: number): boolean { if (!node) return true if (node.val <= lowerBound) return false diff --git a/src/main/ts/g0001_0100/s0100_same_tree/readme.md b/src/main/ts/g0001_0100/s0100_same_tree/readme.md new file mode 100644 index 0000000..0172a56 --- /dev/null +++ b/src/main/ts/g0001_0100/s0100_same_tree/readme.md @@ -0,0 +1,36 @@ +100\. Same Tree + +Easy + +Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not. + +Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg) + +**Input:** p = [1,2,3], q = [1,2,3] + +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg) + +**Input:** p = [1,2], q = [1,null,2] + +**Output:** false + +**Example 3:** + +![](https://assets.leetcode.com/uploads/2020/12/20/ex3.jpg) + +**Input:** p = [1,2,1], q = [1,1,2] + +**Output:** false + +**Constraints:** + +* The number of nodes in both trees is in the range `[0, 100]`. +* -104 <= Node.val <= 104 \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0100_same_tree/solution.ts b/src/main/ts/g0001_0100/s0100_same_tree/solution.ts new file mode 100644 index 0000000..fe8994c --- /dev/null +++ b/src/main/ts/g0001_0100/s0100_same_tree/solution.ts @@ -0,0 +1,29 @@ +// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Level_2_Day_15_Tree +// #Udemy_Tree_Stack_Queue #Top_Interview_150_Binary_Tree_General +// #2025_04_16_Time_0_ms_(100.00%)_Space_55.66_MB_(67.00%) + +import { TreeNode } from '../../com_github_leetcode/treenode' + +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ +function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + if (p === null || q === null) { + return p === null && q === null + } + const b1 = isSameTree(p.left, q.left) + const b2 = isSameTree(p.right, q.right) + return p.val === q.val && b1 && b2 +} + +export { isSameTree } diff --git a/src/main/ts/g0201_0300/s0274_h_index/readme.md b/src/main/ts/g0201_0300/s0274_h_index/readme.md new file mode 100644 index 0000000..73d5909 --- /dev/null +++ b/src/main/ts/g0201_0300/s0274_h_index/readme.md @@ -0,0 +1,32 @@ +274\. H-Index + +Medium + +Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their ith paper, return compute the researcher's `h`**\-index**. + +According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): A scientist has an index `h` if `h` of their `n` papers have at least `h` citations each, and the other `n − h` papers have no more than `h` citations each. + +If there are several possible values for `h`, the maximum one is taken as the `h`**\-index**. + +**Example 1:** + +**Input:** citations = [3,0,6,1,5] + +**Output:** 3 + +**Explanation:** + + [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. + Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3. + +**Example 2:** + +**Input:** citations = [1,3,1] + +**Output:** 1 + +**Constraints:** + +* `n == citations.length` +* `1 <= n <= 5000` +* `0 <= citations[i] <= 1000` diff --git a/src/main/ts/g0201_0300/s0274_h_index/solution.ts b/src/main/ts/g0201_0300/s0274_h_index/solution.ts new file mode 100644 index 0000000..d832195 --- /dev/null +++ b/src/main/ts/g0201_0300/s0274_h_index/solution.ts @@ -0,0 +1,20 @@ +// #Medium #Array #Sorting #Counting_Sort #Top_Interview_150_Array/String +// #2025_04_16_Time_0_ms_(100.00%)_Space_54.06_MB_(91.25%) + +function hIndex(citations: number[]): number { + const len = citations.length + const freqArray = new Array(len + 1).fill(0) + for (const citation of citations) { + freqArray[Math.min(citation, len)]++ + } + let totalSoFar = 0 + for (let k = len; k >= 0; k--) { + totalSoFar += freqArray[k] + if (totalSoFar >= k) { + return k + } + } + return -1 +} + +export { hIndex } diff --git a/src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/readme.md b/src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/readme.md new file mode 100644 index 0000000..7e2c591 --- /dev/null +++ b/src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/readme.md @@ -0,0 +1,38 @@ +380\. Insert Delete GetRandom O(1) + +Medium + +Implement the `RandomizedSet` class: + +* `RandomizedSet()` Initializes the `RandomizedSet` object. +* `bool insert(int val)` Inserts an item `val` into the set if not present. Returns `true` if the item was not present, `false` otherwise. +* `bool remove(int val)` Removes an item `val` from the set if present. Returns `true` if the item was present, `false` otherwise. +* `int getRandom()` Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the **same probability** of being returned. + +You must implement the functions of the class such that each function works in **average** `O(1)` time complexity. + +**Example 1:** + +**Input** + + ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"] + [[], [1], [2], [2], [], [1], [2], []] + +**Output:** [null, true, false, true, 2, true, false, 2] + +**Explanation:** + + RandomizedSet randomizedSet = new RandomizedSet(); + randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully. + randomizedSet.remove(2); // Returns false as 2 does not exist in the set. + randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2]. + randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly. + randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2]. + randomizedSet.insert(2); // 2 was already in the set, so return false. + randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2. + +**Constraints:** + +* -231 <= val <= 231 - 1 +* At most `2 * `105 calls will be made to `insert`, `remove`, and `getRandom`. +* There will be **at least one** element in the data structure when `getRandom` is called. diff --git a/src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution.ts b/src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution.ts new file mode 100644 index 0000000..6b57db9 --- /dev/null +++ b/src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution.ts @@ -0,0 +1,51 @@ +// #Medium #Array #Hash_Table #Math #Design #Randomized #Programming_Skills_II_Day_20 +// #Top_Interview_150_Array/String #2025_04_16_Time_73_ms_(82.52%)_Space_106.13_MB_(72.14%) + +class RandomizedSet { + private rand: () => number + private list: number[] + private map: Map + + constructor() { + this.rand = () => Math.floor(Math.random() * this.list.length) // NOSONAR + this.list = [] + this.map = new Map() + } + + insert(val: number): boolean { + if (this.map.has(val)) { + return false + } + this.map.set(val, this.list.length) + this.list.push(val) + return true + } + + remove(val: number): boolean { + if (!this.map.has(val)) { + return false + } + const swap1 = this.map.get(val)! + const swap2 = this.list.length - 1 + const val2 = this.list[swap2] + this.map.set(val2, swap1) + this.map.delete(val) + this.list[swap1] = val2 + this.list.pop() + return true + } + + getRandom(): number { + return this.list[this.rand()] + } +} + +/** + * Your RandomizedSet object will be instantiated and called as such: + * var obj = new RandomizedSet() + * var param_1 = obj.insert(val) + * var param_2 = obj.remove(val) + * var param_3 = obj.getRandom() + */ + +export { RandomizedSet } diff --git a/src/main/ts/g0301_0400/s0399_evaluate_division/readme.md b/src/main/ts/g0301_0400/s0399_evaluate_division/readme.md new file mode 100644 index 0000000..18a83c5 --- /dev/null +++ b/src/main/ts/g0301_0400/s0399_evaluate_division/readme.md @@ -0,0 +1,49 @@ +399\. Evaluate Division + +Medium + +You are given an array of variable pairs `equations` and an array of real numbers `values`, where equations[i] = [Ai, Bi] and `values[i]` represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable. + +You are also given some `queries`, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?. + +Return _the answers to all queries_. If a single answer cannot be determined, return `-1.0`. + +**Note:** The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction. + +**Example 1:** + +**Input:** equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]] + +**Output:** [6.00000,0.50000,-1.00000,1.00000,-1.00000] + +**Explanation:** + +Given: _a / b = 2.0_, _b / c = 3.0_ + +queries are: _a / c = ?_, _b / a = ?_, _a / e = ?_, _a / a = ?_, _x / x = ?_ + +return: [6.0, 0.5, -1.0, 1.0, -1.0 ] + +**Example 2:** + +**Input:** equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]] + +**Output:** [3.75000,0.40000,5.00000,0.20000] + +**Example 3:** + +**Input:** equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]] + +**Output:** [0.50000,2.00000,-1.00000,-1.00000] + +**Constraints:** + +* `1 <= equations.length <= 20` +* `equations[i].length == 2` +* 1 <= Ai.length, Bi.length <= 5 +* `values.length == equations.length` +* `0.0 < values[i] <= 20.0` +* `1 <= queries.length <= 20` +* `queries[i].length == 2` +* 1 <= Cj.length, Dj.length <= 5 +* Ai, Bi, Cj, Dj consist of lower case English letters and digits. \ No newline at end of file diff --git a/src/main/ts/g0301_0400/s0399_evaluate_division/solution.ts b/src/main/ts/g0301_0400/s0399_evaluate_division/solution.ts new file mode 100644 index 0000000..1c9fa7e --- /dev/null +++ b/src/main/ts/g0301_0400/s0399_evaluate_division/solution.ts @@ -0,0 +1,62 @@ +// #Medium #Array #Depth_First_Search #Breadth_First_Search #Graph #Union_Find #Shortest_Path +// #Top_Interview_150_Graph_General #2025_04_16_Time_0_ms_(100.00%)_Space_54.98_MB_(77.31%) + +type MapType = Map +type RateType = Map + +function calcEquation(equations: [string, string][], values: number[], queries: [string, string][]): number[] { + const root: MapType = new Map() + const rate: RateType = new Map() + for (const [x, y] of equations) { + if (!root.has(x)) { + root.set(x, x) + rate.set(x, 1.0) + } + if (!root.has(y)) { + root.set(y, y) + rate.set(y, 1.0) + } + } + for (let i = 0; i < equations.length; ++i) { + const [x, y] = equations[i] + union(x, y, values[i], root, rate) + } + const result: number[] = [] + for (const [x, y] of queries) { + if (!root.has(x) || !root.has(y)) { + result.push(-1.0) + } else { + const rootX = findRoot(x, x, 1.0, root, rate) + const rootY = findRoot(y, y, 1.0, root, rate) + if (rootX === rootY) { + result.push(rate.get(x)! / rate.get(y)!) + } else { + result.push(-1.0) + } + } + } + return result +} + +function union(x: string, y: string, value: number, root: MapType, rate: RateType): void { + const rootX = findRoot(x, x, 1.0, root, rate) + const rootY = findRoot(y, y, 1.0, root, rate) + + if (rootX !== rootY) { + root.set(rootX, rootY) + const rateX = rate.get(x)! + const rateY = rate.get(y)! + rate.set(rootX, (value * rateY) / rateX) + } +} + +function findRoot(originalX: string, x: string, rateSoFar: number, root: MapType, rate: RateType): string { + if (root.get(x) === x) { + root.set(originalX, x) + rate.set(originalX, rateSoFar * rate.get(x)!) + return x + } + return findRoot(originalX, root.get(x)!, rateSoFar * rate.get(x)!, root, rate) +} + +export { calcEquation } diff --git a/src/main/ts/g0401_0500/s0427_construct_quad_tree/solution.ts b/src/main/ts/g0401_0500/s0427_construct_quad_tree/solution.ts index 7c91dce..78a00d8 100644 --- a/src/main/ts/g0401_0500/s0427_construct_quad_tree/solution.ts +++ b/src/main/ts/g0401_0500/s0427_construct_quad_tree/solution.ts @@ -1,4 +1,5 @@ // #Medium #Array #Tree #Matrix #Divide_and_Conquer #Top_Interview_150_Divide_and_Conquer +// #2025_04_16_Time_51_ms_(96.23%)_Space_60.83_MB_(77.36%) class _Node { val: boolean diff --git a/src/main/ts/g0501_0600/s0502_ipo/solution.ts b/src/main/ts/g0501_0600/s0502_ipo/solution.ts index 72cb0af..3800003 100644 --- a/src/main/ts/g0501_0600/s0502_ipo/solution.ts +++ b/src/main/ts/g0501_0600/s0502_ipo/solution.ts @@ -2,85 +2,87 @@ // #2025_04_15_Time_193_ms_(89.19%)_Space_102.47_MB_(8.11%) class MaxHeap { - private heap: number[]; + private heap: number[] constructor() { - this.heap = []; + this.heap = [] } push(value: number) { - this.heap.push(value); - this.heapifyUp(); + this.heap.push(value) + this.heapifyUp() } pop(): number | undefined { if (this.heap.length === 0) { - return undefined; + return undefined } if (this.heap.length === 1) { - return this.heap.pop(); + return this.heap.pop() } - const max = this.heap[0]; - this.heap[0] = this.heap.pop()!; - this.heapifyDown(); - return max; + const max = this.heap[0] + this.heap[0] = this.heap.pop()! + this.heapifyDown() + return max } isEmpty(): boolean { - return this.heap.length === 0; + return this.heap.length === 0 } private heapifyUp() { - let index = this.heap.length - 1; + let index = this.heap.length - 1 while (index > 0) { - let parentIndex = Math.floor((index - 1) / 2); + let parentIndex = Math.floor((index - 1) / 2) if (this.heap[parentIndex] >= this.heap[index]) { - break; + break } - [this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]]; - index = parentIndex; + ;[this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]] + index = parentIndex } } private heapifyDown() { - let index = 0; + let index = 0 while (true) { - let left = 2 * index + 1; - let right = 2 * index + 2; - let largest = index; + let left = 2 * index + 1 + let right = 2 * index + 2 + let largest = index if (left < this.heap.length && this.heap[left] > this.heap[largest]) { - largest = left; + largest = left } if (right < this.heap.length && this.heap[right] > this.heap[largest]) { - largest = right; + largest = right } - if (largest === index) break; - [this.heap[index], this.heap[largest]] = [this.heap[largest], this.heap[index]]; - index = largest; + if (largest === index) { + break + } + ;[this.heap[index], this.heap[largest]] = [this.heap[largest], this.heap[index]] + index = largest } } } function findMaximizedCapital(k: number, w: number, profits: number[], capital: number[]): number { - let projects: [number, number][] = []; - const n = profits.length; + let projects: [number, number][] = [] + const n = profits.length for (let i = 0; i < n; i++) { - projects.push([capital[i], profits[i]]); + projects.push([capital[i], profits[i]]) } - projects.sort((a, b) => a[0] - b[0]); - const maxHeap = new MaxHeap(); - let i = 0; + projects.sort((a, b) => a[0] - b[0]) + const maxHeap = new MaxHeap() + let i = 0 while (k--) { while (i < n && projects[i][0] <= w) { - maxHeap.push(projects[i][1]); - i++; + maxHeap.push(projects[i][1]) + i++ } if (maxHeap.isEmpty()) { - break; + break } - w += maxHeap.pop()!; + w += maxHeap.pop()! } - return w; + return w } export { findMaximizedCapital } diff --git a/src/test/ts/g0001_0100/s0100_same_tree/solution.test.ts b/src/test/ts/g0001_0100/s0100_same_tree/solution.test.ts new file mode 100644 index 0000000..6f994a4 --- /dev/null +++ b/src/test/ts/g0001_0100/s0100_same_tree/solution.test.ts @@ -0,0 +1,16 @@ +// tslint:disable:no-magic-numbers +import { createTreeNode } from 'src/main/ts/com_github_leetcode/treenode' +import { isSameTree } from 'src/main/ts/g0001_0100/s0100_same_tree/solution' +import { expect, test } from 'vitest' + +test('isSameTree', () => { + expect(isSameTree(createTreeNode([1, 2, 3]), createTreeNode([1, 2, 3]))).toEqual(true) +}) + +test('isSameTree2', () => { + expect(isSameTree(createTreeNode([1, 2, 3]), createTreeNode([1, null, 2]))).toEqual(false) +}) + +test('isSameTree3', () => { + expect(isSameTree(createTreeNode([1, 2, 1]), createTreeNode([1, 1, 2]))).toEqual(false) +}) diff --git a/src/test/ts/g0201_0300/s0274_h_index/solution.test.ts b/src/test/ts/g0201_0300/s0274_h_index/solution.test.ts new file mode 100644 index 0000000..3e0fb9a --- /dev/null +++ b/src/test/ts/g0201_0300/s0274_h_index/solution.test.ts @@ -0,0 +1,11 @@ +// tslint:disable:no-magic-numbers +import { hIndex } from 'src/main/ts/g0201_0300/s0274_h_index/solution' +import { expect, test } from 'vitest' + +test('hIndex', () => { + expect(hIndex([3, 0, 6, 1, 5])).toEqual(3) +}) + +test('hIndex2', () => { + expect(hIndex([1, 3, 1])).toEqual(1) +}) diff --git a/src/test/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution.test.ts b/src/test/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution.test.ts new file mode 100644 index 0000000..d025ba4 --- /dev/null +++ b/src/test/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution.test.ts @@ -0,0 +1,25 @@ +// tslint:disable:no-magic-numbers +import { RandomizedSet } from 'src/main/ts/g0301_0400/s0380_insert_delete_getrandom_o1/solution' +import { expect, test } from 'vitest' + +test('randomizedSet', () => { + const result: string[] = [] + let randomizedSet: RandomizedSet | null = null + result.push(randomizedSet + '') + randomizedSet = new RandomizedSet() + result.push(randomizedSet.insert(1) + '') + result.push(randomizedSet.remove(2) + '') + result.push(randomizedSet.insert(2) + '') + const random = randomizedSet.getRandom() + result.push(random + '') + result.push(randomizedSet.remove(1) + '') + result.push(randomizedSet.insert(2) + '') + result.push(randomizedSet.getRandom() + '') + const expected = ['null', 'true', 'false', 'true', '1', 'true', 'false', '2'] + const expected2 = ['null', 'true', 'false', 'true', '2', 'true', 'false', '2'] + if (random === 1) { + expect(result).toEqual(expected) + } else { + expect(result).toEqual(expected2) + } +}) diff --git a/src/test/ts/g0301_0400/s0399_evaluate_division/solution.test.ts b/src/test/ts/g0301_0400/s0399_evaluate_division/solution.test.ts new file mode 100644 index 0000000..9b22724 --- /dev/null +++ b/src/test/ts/g0301_0400/s0399_evaluate_division/solution.test.ts @@ -0,0 +1,50 @@ +// tslint:disable:no-magic-numbers +import { calcEquation } from 'src/main/ts/g0301_0400/s0399_evaluate_division/solution' +import { expect, test } from 'vitest' + +test('calcEquation', () => { + const equations = [ + ['a', 'b'], + ['b', 'c'], + ] as [string, string][] + const values = [2.0, 3.0] + const queries = [ + ['a', 'c'], + ['b', 'a'], + ['a', 'e'], + ['a', 'a'], + ['x', 'x'], + ] as [string, string][] + const expected = [6.0, 0.5, -1.0, 1.0, -1.0] + expect(calcEquation(equations, values, queries)).toEqual(expected) +}) + +test('calcEquation2', () => { + const equations = [ + ['a', 'b'], + ['b', 'c'], + ['bc', 'cd'], + ] as [string, string][] + const values = [1.5, 2.5, 5.0] + const queries = [ + ['a', 'c'], + ['c', 'b'], + ['bc', 'cd'], + ['cd', 'bc'], + ] as [string, string][] + const expected = [3.75, 0.4, 5.0, 0.2] + expect(calcEquation(equations, values, queries)).toEqual(expected) +}) + +test('calcEquation3', () => { + const equations = [['a', 'b']] as [string, string][] + const values = [0.5] + const queries = [ + ['a', 'b'], + ['b', 'a'], + ['a', 'c'], + ['x', 'y'], + ] as [string, string][] + const expected = [0.5, 2.0, -1.0, -1.0] + expect(calcEquation(equations, values, queries)).toEqual(expected) +})