diff --git a/README.md b/README.md index 58f15dd..6b1c680 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ | | | | | | |-|-|-|-|-|- +| 0560 |[Subarray Sum Equals K](src/main/ts/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Big_O_Time_O(n)_Space_O(n) | 70 | 92.45 #### Day 6 String @@ -54,6 +55,7 @@ | | | | | | |-|-|-|-|-|- +| 0763 |[Partition Labels](src/main/ts/g0701_0800/s0763_partition_labels)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 54 | 94.74 #### Day 8 String @@ -64,13 +66,13 @@ | | | | | | |-|-|-|-|-|- -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 480 | 35.37 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 68 | 95.34 #### Day 10 Linked List | | | | | | |-|-|-|-|-|- -| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 88 | 93.29 +| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 92 | 77.86 #### Day 11 Linked List @@ -162,7 +164,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) | 81 | 68.46 +| 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 #### Day 7 Breadth First Search Depth First Search @@ -235,6 +237,7 @@ | | | | | | |-|-|-|-|-|- +| 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, Big_O_Time_O(n+m)_Space_O(1) | 69 | 96.69 #### Day 6 Breadth First Search Depth First Search @@ -282,7 +285,7 @@ | | | | | | |-|-|-|-|-|- -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 480 | 35.37 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 68 | 95.34 #### Day 15 Dynamic Programming @@ -298,6 +301,7 @@ | | | | | | |-|-|-|-|-|- +| 1143 |[Longest Common Subsequence](src/main/ts/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n*m)_Space_O(n*m) | 94 | 80.56 #### Day 18 Dynamic Programming @@ -572,7 +576,7 @@ | | | | | | |-|-|-|-|-|- -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 480 | 35.37 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 68 | 95.34 #### Day 18 @@ -583,6 +587,7 @@ | | | | | | |-|-|-|-|-|- +| 1143 |[Longest Common Subsequence](src/main/ts/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n*m)_Space_O(n*m) | 94 | 80.56 #### Day 20 @@ -687,6 +692,7 @@ | | | | | | |-|-|-|-|-|- +| 0739 |[Daily Temperatures](src/main/ts/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(n) | 194 | 97.36 #### Day 7 @@ -717,6 +723,7 @@ | | | | | | |-|-|-|-|-|- +| 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, Big_O_Time_O(n+m)_Space_O(1) | 69 | 96.69 #### Day 13 @@ -732,7 +739,7 @@ | | | | | | |-|-|-|-|-|- -| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 88 | 93.29 +| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 92 | 77.86 #### Day 16 @@ -945,12 +952,13 @@ | | | | | | |-|-|-|-|-|- +| 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, Big_O_Time_O(n+m)_Space_O(1) | 69 | 96.69 #### Day 13 Hashmap | | | | | | |-|-|-|-|-|- -| 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) | 51 | 97.89 +| 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 #### Day 14 Stack @@ -999,6 +1007,8 @@ | | | | | | |-|-|-|-|-|- +| 0543 |[Diameter of Binary Tree](src/main/ts/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 61 | 80.89 +| 0437 |[Path Sum III](src/main/ts/g0401_0500/s0437_path_sum_iii)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 68 | 86.71 #### Day 8 Binary Search @@ -1030,12 +1040,13 @@ | | | | | | |-|-|-|-|-|- +| 0416 |[Partition Equal Subset Sum](src/main/ts/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Big_O_Time_O(n*sums)_Space_O(n*sums) | 91 | 74.38 #### Day 14 Sliding Window/Two Pointer | | | | | | |-|-|-|-|-|- -| 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) | 81 | 68.46 +| 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 #### Day 15 Tree @@ -1080,9 +1091,9 @@ | | | | | | |-|-|-|-|-|- -| 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) | 81 | 68.46 +| 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 | 0020 |[Valid Parentheses](src/main/ts/g0001_0100/s0020_valid_parentheses)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 50 | 95.90 -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 480 | 35.37 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 68 | 95.34 #### Udemy Binary Search @@ -1094,7 +1105,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) | 51 | 97.89 +| 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 #### Udemy Two Pointers @@ -1129,6 +1140,7 @@ | | | | | | |-|-|-|-|-|- +| 0543 |[Diameter of Binary Tree](src/main/ts/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 61 | 80.89 #### Udemy Trie and Heap @@ -1144,6 +1156,7 @@ | | | | | | |-|-|-|-|-|- +| 1143 |[Longest Common Subsequence](src/main/ts/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n*m)_Space_O(n*m) | 94 | 80.56 | 0010 |[Regular Expression Matching](src/main/ts/g0001_0100/s0010_regular_expression_matching)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion, Big_O_Time_O(m*n)_Space_O(m*n) | 69 | 92.65 #### Udemy Backtracking/Recursion @@ -1174,7 +1187,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) | 51 | 97.89 +| 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 #### Day 3 Array @@ -1242,6 +1255,16 @@ | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- +| 1143 |[Longest Common Subsequence](src/main/ts/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Big_O_Time_O(n*m)_Space_O(n*m) | 94 | 80.56 +| 0763 |[Partition Labels](src/main/ts/g0701_0800/s0763_partition_labels)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String, Big_O_Time_O(n)_Space_O(1) | 54 | 94.74 +| 0739 |[Daily Temperatures](src/main/ts/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6, Big_O_Time_O(n)_Space_O(n) | 194 | 97.36 +| 0647 |[Palindromic Substrings](src/main/ts/g0601_0700/s0647_palindromic_substrings)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n) | 60 | 89.76 +| 0560 |[Subarray Sum Equals K](src/main/ts/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array, Big_O_Time_O(n)_Space_O(n) | 70 | 92.45 +| 0543 |[Diameter of Binary Tree](src/main/ts/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 61 | 80.89 +| 0494 |[Target Sum](src/main/ts/g0401_0500/s0494_target_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking, Big_O_Time_O(n*(sum+s))_Space_O(n*(sum+s)) | 77 | 81.99 +| 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 | 0035 |[Search Insert Position](src/main/ts/g0001_0100/s0035_search_insert_position)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_2, Big_O_Time_O(log_n)_Space_O(1) | 51 | 80.95 | 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, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_5, 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, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_11, Level_2_Day_8_Binary_Search, Udemy_Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 43 | 98.54 @@ -1262,9 +1285,9 @@ | 0008 |[String to Integer (atoi)](src/main/ts/g0001_0100/s0008_string_to_integer_atoi)| Medium | Top_Interview_Questions, String | 56 | 99.01 | 0007 |[Reverse Integer](src/main/ts/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math, Udemy_Integers | 56 | 95.68 | 0006 |[Zigzag Conversion](src/main/ts/g0001_0100/s0006_zigzag_conversion)| Medium | String | 64 | 95.38 -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Data_Structure_II_Day_9_String, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_17, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 480 | 35.37 +| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Data_Structure_II_Day_9_String, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_17, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 68 | 95.34 | 0004 |[Median of Two Sorted Arrays](src/main/ts/g0001_0100/s0004_median_of_two_sorted_arrays)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Divide_and_Conquer, Big_O_Time_O(log(min(N,M)))_Space_O(1) | 86 | 92.15 -| 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, Algorithm_I_Day_6_Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer, Udemy_Strings, Big_O_Time_O(n)_Space_O(1) | 81 | 68.46 -| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Data_Structure_II_Day_10_Linked_List, Programming_Skills_II_Day_15, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 88 | 93.29 -| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Data_Structure_I_Day_2_Array, Level_1_Day_13_Hashmap, Udemy_Arrays, Big_O_Time_O(n)_Space_O(n) | 51 | 97.89 +| 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, Algorithm_I_Day_6_Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer, Udemy_Strings, Big_O_Time_O(n)_Space_O(1) | 65 | 90.77 +| 0002 |[Add Two Numbers](src/main/ts/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Data_Structure_II_Day_10_Linked_List, Programming_Skills_II_Day_15, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 92 | 77.86 +| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Data_Structure_I_Day_2_Array, Level_1_Day_13_Hashmap, Udemy_Arrays, Big_O_Time_O(n)_Space_O(n) | 54 | 91.72 diff --git a/src/main/ts/g0001_0100/s0001_two_sum/readme.md b/src/main/ts/g0001_0100/s0001_two_sum/readme.md index cdc7a3d..5c4cdea 100644 --- a/src/main/ts/g0001_0100/s0001_two_sum/readme.md +++ b/src/main/ts/g0001_0100/s0001_two_sum/readme.md @@ -43,19 +43,16 @@ You can return the answer in any order. ## Solution ```typescript -interface ITemp { - [key: number]: number -} - function twoSum(nums: number[], target: number): number[] { - const temp: ITemp = {} + const indexMap: Map = new Map() for (let i = 0; i < nums.length; i++) { - const tag = target - nums[i] - if (temp[tag] >= 0) { - return [temp[tag], i] + const requiredNum: number = target - nums[i] + if (indexMap.has(requiredNum)) { + return [indexMap.get(requiredNum)!, i] } - temp[nums[i]] = i + indexMap.set(nums[i], i) } + return [-1, -1] } export { twoSum } diff --git a/src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md b/src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md index cdae041..5f45bf2 100644 --- a/src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md +++ b/src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md @@ -54,29 +54,29 @@ import { ListNode } from '../../com_github_leetcode/listnode' * } */ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null { - const result = new ListNode(null) - let next = result - let carryNum = 0 - let currSum = 0 - let num1 = 0 - let num2 = 0 - - while (l1 !== null || l2 !== null) { - // @ts-ignore - num1 = l1 !== null ? l1.val : 0 - // @ts-ignore - num2 = l2 !== null ? l2.val : 0 - currSum = (num1 + num2 + carryNum) % 10 - carryNum = Math.floor((num1 + num2 + carryNum) / 10) - next.next = new ListNode(currSum) - next = next.next - l1 = l1 !== null ? l1.next : null - l2 = l2 !== null ? l2.next : null + const dummyHead: ListNode = new ListNode(0) + let p: ListNode | null = l1 + let q: ListNode | null = l2 + let curr: ListNode | null = dummyHead + let carry: number = 0 + while (p !== null || q !== null) { + const x: number = p !== null ? p.val : 0 + const y: number = q !== null ? q.val : 0 + const sum: number = carry + x + y + carry = Math.floor(sum / 10) + curr.next = new ListNode(sum % 10) + curr = curr.next + if (p !== null) { + p = p.next + } + if (q !== null) { + q = q.next + } } - if (carryNum) { - next.next = new ListNode(carryNum) + if (carry > 0) { + curr.next = new ListNode(carry) } - return result.next + return dummyHead.next } export { addTwoNumbers } diff --git a/src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md b/src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md index 9ddee15..19064e4 100644 --- a/src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md +++ b/src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md @@ -44,21 +44,27 @@ Notice that the answer must be a substring, "pwke" is a subsequence and not a su ```typescript function lengthOfLongestSubstring(s: string): number { - const hash: { [key: string]: number } = {} - let maxLength = 0 - let length = 0 - let start = 0 + const lastIndices: number[] = new Array(256).fill(-1) + let maxLen: number = 0 + let curLen: number = 0 + let start: number = 0 for (let i = 0; i < s.length; i++) { - const char = s[i] - if (hash[char] !== undefined && hash[char] >= start) { - start = hash[char] + 1 - length = i - start + const cur: string = s.charAt(i) + const charCode: number = cur.charCodeAt(0) + if (lastIndices[charCode] < start) { + lastIndices[charCode] = i + curLen++ + } else { + const lastIndex: number = lastIndices[charCode] + start = lastIndex + 1 + curLen = i - start + 1 + lastIndices[charCode] = i + } + if (curLen > maxLen) { + maxLen = curLen } - length++ - hash[char] = i - maxLength = Math.max(maxLength, length) } - return maxLength + return maxLen } export { lengthOfLongestSubstring } diff --git a/src/main/ts/g0001_0100/s0005_longest_palindromic_substring/readme.md b/src/main/ts/g0001_0100/s0005_longest_palindromic_substring/readme.md index 7d43fe8..ab49c79 100644 --- a/src/main/ts/g0001_0100/s0005_longest_palindromic_substring/readme.md +++ b/src/main/ts/g0001_0100/s0005_longest_palindromic_substring/readme.md @@ -30,30 +30,33 @@ Given a string `s`, return _the longest_ _palindromic_ _substring_ in `s`. ```typescript function longestPalindrome(s: string): string { - let max = 0 - let res = '' + const newStr: string[] = new Array(s.length * 2 + 1) + newStr[0] = '#' for (let i = 0; i < s.length; i++) { - for (let j = i + 1; j <= s.length; j++) { - if (s.slice(i, j).length > max && isPalindrome(s.slice(i, j))) { - max = s.slice(i, j).length - res = s.slice(i, j) - } - } + newStr[2 * i + 1] = s.charAt(i) + newStr[2 * i + 2] = '#' } - return res -} - -const isPalindrome = (s: string): boolean => { - let i = 0 - let j = s.length - 1 - while (i < j) { - if (s[i] !== s[j]) { - return false + const dp: number[] = new Array(newStr.length) + let friendCenter: number = 0 + let friendRadius: number = 0 + let lpsCenter: number = 0 + let lpsRadius: number = 0 + for (let i = 0; i < newStr.length; i++) { + dp[i] = + friendCenter + friendRadius > i ? Math.min(dp[2 * friendCenter - i], friendCenter + friendRadius - i) : 1 + while (i + dp[i] < newStr.length && i - dp[i] >= 0 && newStr[i + dp[i]] === newStr[i - dp[i]]) { + dp[i]++ + } + if (friendCenter + friendRadius < i + dp[i]) { + friendCenter = i + friendRadius = dp[i] + } + if (lpsRadius < dp[i]) { + lpsCenter = i + lpsRadius = dp[i] } - i++ - j-- } - return true + return s.substring((lpsCenter - lpsRadius + 1) / 2, (lpsCenter + lpsRadius - 1) / 2) } export { longestPalindrome } diff --git a/src/main/ts/g0401_0500/s0416_partition_equal_subset_sum/readme.md b/src/main/ts/g0401_0500/s0416_partition_equal_subset_sum/readme.md new file mode 100644 index 0000000..d7b569d --- /dev/null +++ b/src/main/ts/g0401_0500/s0416_partition_equal_subset_sum/readme.md @@ -0,0 +1,54 @@ +[![](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) + +## 416\. Partition Equal Subset Sum + +Medium + +Given a **non-empty** array `nums` containing **only positive integers**, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. + +**Example 1:** + +**Input:** nums = [1,5,11,5] + +**Output:** true + +**Explanation:** The array can be partitioned as [1, 5, 5] and [11]. + +**Example 2:** + +**Input:** nums = [1,2,3,5] + +**Output:** false + +**Explanation:** The array cannot be partitioned into equal sum subsets. + +**Constraints:** + +* `1 <= nums.length <= 200` +* `1 <= nums[i] <= 100` + +## Solution + +```typescript +function canPartition(nums: number[]): boolean { + let sums: number = 0 + for (const num of nums) { + sums += num + } + if (sums % 2 === 1) { + return false + } + sums /= 2 + const dp: boolean[] = new Array(sums + 1).fill(false) + dp[0] = true + for (const num of nums) { + for (let sum = sums; sum >= num; sum--) { + dp[sum] = dp[sum] || dp[sum - num] + } + } + return dp[sums] +} + +export { canPartition } +``` \ No newline at end of file diff --git a/src/main/ts/g0401_0500/s0437_path_sum_iii/readme.md b/src/main/ts/g0401_0500/s0437_path_sum_iii/readme.md new file mode 100644 index 0000000..7ca64a6 --- /dev/null +++ b/src/main/ts/g0401_0500/s0437_path_sum_iii/readme.md @@ -0,0 +1,63 @@ +[![](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) + +## 437\. Path Sum III + +Medium + +Given the `root` of a binary tree and an integer `targetSum`, return _the number of paths where the sum of the values along the path equals_ `targetSum`. + +The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes). + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg) + +**Input:** root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8 + +**Output:** 3 + +**Explanation:** The paths that sum to 8 are shown. + +**Example 2:** + +**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 + +**Output:** 3 + +**Constraints:** + +* The number of nodes in the tree is in the range `[0, 1000]`. +* -109 <= Node.val <= 109 +* `-1000 <= targetSum <= 1000` + +## Solution + +```typescript +function pathSum(root: TreeNode | null, targetSum: number): number { + let count = 0 + let map = new Map() + + function dfs(node: TreeNode | null, currentSum: number): void { + if (!node) return + + currentSum += node.val + if (currentSum === targetSum) count++ + + count += map.get(currentSum - targetSum) ?? 0 + + map.set(currentSum, map.get(currentSum) + 1 || 1) + dfs(node?.left, currentSum) + dfs(node?.right, currentSum) + + //remove from hashmap + map.set(currentSum, map.get(currentSum) - 1) + if (map.get(currentSum) === 0) map.delete(currentSum) + } + + dfs(root, 0) + return count +} + +export { pathSum } +``` \ No newline at end of file diff --git a/src/main/ts/g0401_0500/s0438_find_all_anagrams_in_a_string/readme.md b/src/main/ts/g0401_0500/s0438_find_all_anagrams_in_a_string/readme.md new file mode 100644 index 0000000..5813c3f --- /dev/null +++ b/src/main/ts/g0401_0500/s0438_find_all_anagrams_in_a_string/readme.md @@ -0,0 +1,79 @@ +[![](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) + +## 438\. Find All Anagrams in a String + +Medium + +Given two strings `s` and `p`, return _an array of all the start indices of_ `p`_'s anagrams in_ `s`. You may return the answer in **any order**. + +An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. + +**Example 1:** + +**Input:** s = "cbaebabacd", p = "abc" + +**Output:** [0,6] + +**Explanation:** + + The substring with start index = 0 is "cba", which is an anagram of "abc". + The substring with start index = 6 is "bac", which is an anagram of "abc". + +**Example 2:** + +**Input:** s = "abab", p = "ab" + +**Output:** [0,1,2] + +**Explanation:** + + The substring with start index = 0 is "ab", which is an anagram of "ab". + The substring with start index = 1 is "ba", which is an anagram of "ab". + The substring with start index = 2 is "ab", which is an anagram of "ab". + +**Constraints:** + +* 1 <= s.length, p.length <= 3 * 104 +* `s` and `p` consist of lowercase English letters. + +## Solution + +```typescript +function findAnagrams(s: string, p: string): number[] { + const map: number[] = new Array(26).fill(0) + for (let i = 0; i < p.length; ++i) { + map[p.charCodeAt(i) - 'a'.charCodeAt(0)]++ + } + const res: number[] = [] + let i: number = 0 + let j: number = 0 + + while (i < s.length) { + const idx: number = s.charCodeAt(i) - 'a'.charCodeAt(0) + // Add the new character + map[idx]-- + // If the length is greater than window's length, pop the left character in the window + if (i >= p.length) { + map[s.charCodeAt(j++) - 'a'.charCodeAt(0)]++ + } + let finish: boolean = true + + for (let k = 0; k < 26; k++) { + // If it is not an anagram of string p + if (map[k] !== 0) { + finish = false + break + } + } + + if (i >= p.length - 1 && finish) { + res.push(j) + } + i++ + } + return res +} + +export { findAnagrams } +``` \ No newline at end of file diff --git a/src/main/ts/g0401_0500/s0494_target_sum/readme.md b/src/main/ts/g0401_0500/s0494_target_sum/readme.md new file mode 100644 index 0000000..885427f --- /dev/null +++ b/src/main/ts/g0401_0500/s0494_target_sum/readme.md @@ -0,0 +1,85 @@ +[![](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) + +## 494\. Target Sum + +Medium + +You are given an integer array `nums` and an integer `target`. + +You want to build an **expression** out of nums by adding one of the symbols `'+'` and `'-'` before each integer in nums and then concatenate all the integers. + +* For example, if `nums = [2, 1]`, you can add a `'+'` before `2` and a `'-'` before `1` and concatenate them to build the expression `"+2-1"`. + +Return the number of different **expressions** that you can build, which evaluates to `target`. + +**Example 1:** + +**Input:** nums = [1,1,1,1,1], target = 3 + +**Output:** 5 + +**Explanation:** + + There are 5 ways to assign symbols to make the sum of nums be target 3. + -1 + 1 + 1 + 1 + 1 = 3 + +1 - 1 + 1 + 1 + 1 = 3 + +1 + 1 - 1 + 1 + 1 = 3 + +1 + 1 + 1 - 1 + 1 = 3 + +1 + 1 + 1 + 1 - 1 = 3 + +**Example 2:** + +**Input:** nums = [1], target = 1 + +**Output:** 1 + +**Constraints:** + +* `1 <= nums.length <= 20` +* `0 <= nums[i] <= 1000` +* `0 <= sum(nums[i]) <= 1000` +* `-1000 <= target <= 1000` + +## Solution + +```typescript +function findTargetSumWays(nums: number[], s: number): number { + let sum: number = 0 + s = Math.abs(s) + + for (const num of nums) { + sum += num + } + + // Invalid s, just return 0 + if (s > sum || (sum + s) % 2 !== 0) { + return 0 + } + + const dp: number[][] = new Array((sum + s) / 2 + 1).fill(null).map(() => new Array(nums.length + 1).fill(0)) + dp[0][0] = 1 + + // Empty knapsack must be processed specially + for (let i = 0; i < nums.length; i++) { + if (nums[i] === 0) { + dp[0][i + 1] = dp[0][i] * 2 + } else { + dp[0][i + 1] = dp[0][i] + } + } + + for (let i = 1; i < dp.length; i++) { + for (let j = 0; j < nums.length; j++) { + dp[i][j + 1] += dp[i][j] + if (nums[j] <= i) { + dp[i][j + 1] += dp[i - nums[j]][j] + } + } + } + + return dp[(sum + s) / 2][nums.length] +} + +export { findTargetSumWays } +``` \ No newline at end of file diff --git a/src/main/ts/g0501_0600/s0543_diameter_of_binary_tree/readme.md b/src/main/ts/g0501_0600/s0543_diameter_of_binary_tree/readme.md new file mode 100644 index 0000000..353cf92 --- /dev/null +++ b/src/main/ts/g0501_0600/s0543_diameter_of_binary_tree/readme.md @@ -0,0 +1,55 @@ +[![](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) + +## 543\. Diameter of Binary Tree + +Easy + +Given the `root` of a binary tree, return _the length of the **diameter** of the tree_. + +The **diameter** of a binary tree is the **length** of the longest path between any two nodes in a tree. This path may or may not pass through the `root`. + +The **length** of a path between two nodes is represented by the number of edges between them. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg) + +**Input:** root = [1,2,3,4,5] + +**Output:** 3 + +**Explanation:** 3 is the length of the path [4,2,1,3] or [5,2,1,3]. + +**Example 2:** + +**Input:** root = [1,2] + +**Output:** 1 + +**Constraints:** + +* The number of nodes in the tree is in the range [1, 104]. +* `-100 <= Node.val <= 100` + +## Solution + +```typescript +function diameterOfBinaryTree(root: TreeNode | null): number { + let ans = 0 + function dfs(node: TreeNode | null): number { + if (node === null) { + return 0 + } + let left = dfs(node.left) + let right = dfs(node.right) + ans = Math.max(ans, left + right) + + return Math.max(left, right) + 1 + } + dfs(root) + return ans +} + +export { diameterOfBinaryTree } +``` \ No newline at end of file diff --git a/src/main/ts/g0501_0600/s0560_subarray_sum_equals_k/readme.md b/src/main/ts/g0501_0600/s0560_subarray_sum_equals_k/readme.md new file mode 100644 index 0000000..f49af62 --- /dev/null +++ b/src/main/ts/g0501_0600/s0560_subarray_sum_equals_k/readme.md @@ -0,0 +1,55 @@ +[![](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) + +## 560\. Subarray Sum Equals K + +Medium + +Given an array of integers `nums` and an integer `k`, return _the total number of continuous subarrays whose sum equals to `k`_. + +**Example 1:** + +**Input:** nums = [1,1,1], k = 2 + +**Output:** 2 + +**Example 2:** + +**Input:** nums = [1,2,3], k = 3 + +**Output:** 2 + +**Constraints:** + +* 1 <= nums.length <= 2 * 104 +* `-1000 <= nums[i] <= 1000` +* -107 <= k <= 107 + +## Solution + +```typescript +function subarraySum(nums: number[], k: number): number { + let tempSum: number = 0 + let ret: number = 0 + const sumCount: Map = new Map() + sumCount.set(0, 1) + + for (const num of nums) { + tempSum += num + + if (sumCount.has(tempSum - k)) { + ret += sumCount.get(tempSum - k)! + } + + if (sumCount.has(tempSum)) { + sumCount.set(tempSum, sumCount.get(tempSum)! + 1) + } else { + sumCount.set(tempSum, 1) + } + } + + return ret +} + +export { subarraySum } +``` \ No newline at end of file diff --git a/src/main/ts/g0601_0700/s0647_palindromic_substrings/readme.md b/src/main/ts/g0601_0700/s0647_palindromic_substrings/readme.md new file mode 100644 index 0000000..369db95 --- /dev/null +++ b/src/main/ts/g0601_0700/s0647_palindromic_substrings/readme.md @@ -0,0 +1,63 @@ +[![](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) + +## 647\. Palindromic Substrings + +Medium + +Given a string `s`, return _the number of **palindromic substrings** in it_. + +A string is a **palindrome** when it reads the same backward as forward. + +A **substring** is a contiguous sequence of characters within the string. + +**Example 1:** + +**Input:** s = "abc" + +**Output:** 3 + +**Explanation:** Three palindromic strings: "a", "b", "c". + +**Example 2:** + +**Input:** s = "aaa" + +**Output:** 6 + +**Explanation:** Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". + +**Constraints:** + +* `1 <= s.length <= 1000` +* `s` consists of lowercase English letters. + +## Solution + +```typescript +function expand(a: string[], l: number, r: number, res: number[]): void { + while (l >= 0 && r < a.length) { + if (a[l] !== a[r]) { + return + } else { + res[0]++ + l-- + r++ + } + } +} + +function countSubstrings(s: string): number { + const a: string[] = s.split('') + const res: number[] = [0] + + for (let i = 0; i < a.length; i++) { + expand(a, i, i, res) + expand(a, i, i + 1, res) + } + + return res[0] +} + +export { countSubstrings } +``` \ No newline at end of file diff --git a/src/main/ts/g0701_0800/s0739_daily_temperatures/readme.md b/src/main/ts/g0701_0800/s0739_daily_temperatures/readme.md new file mode 100644 index 0000000..1adabec --- /dev/null +++ b/src/main/ts/g0701_0800/s0739_daily_temperatures/readme.md @@ -0,0 +1,51 @@ +[![](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) + +## 739\. Daily Temperatures + +Medium + +Given an array of integers `temperatures` represents the daily temperatures, return _an array_ `answer` _such that_ `answer[i]` _is the number of days you have to wait after the_ ith _day to get a warmer temperature_. If there is no future day for which this is possible, keep `answer[i] == 0` instead. + +**Example 1:** + +**Input:** temperatures = [73,74,75,71,69,72,76,73] + +**Output:** [1,1,4,2,1,1,0,0] + +**Example 2:** + +**Input:** temperatures = [30,40,50,60] + +**Output:** [1,1,1,0] + +**Example 3:** + +**Input:** temperatures = [30,60,90] + +**Output:** [1,1,0] + +**Constraints:** + +* 1 <= temperatures.length <= 105 +* `30 <= temperatures[i] <= 100` + +## Solution + +```typescript +function dailyTemperatures(temperatures: number[]): number[] { + let stack: number[] = [] + let result: number[] = new Array(temperatures.length).fill(0) + + for (let i = 0; i < temperatures.length; i++) { + while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) { + let index: number | undefined = stack.pop() + result[index!] = i - index! + } + stack.push(i) + } + return result +} + +export { dailyTemperatures } +``` \ No newline at end of file diff --git a/src/main/ts/g0701_0800/s0763_partition_labels/readme.md b/src/main/ts/g0701_0800/s0763_partition_labels/readme.md new file mode 100644 index 0000000..e8cd12c --- /dev/null +++ b/src/main/ts/g0701_0800/s0763_partition_labels/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) + +## 763\. Partition Labels + +Medium + +You are given a string `s`. We want to partition the string into as many parts as possible so that each letter appears in at most one part. + +Return _a list of integers representing the size of these parts_. + +**Example 1:** + +**Input:** s = "ababcbacadefegdehijhklij" + +**Output:** [9,7,8] + +**Explanation:** + + The partition is "ababcbaca", "defegde", "hijhklij". + This is a partition so that each letter appears in at most one part. + A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts. + +**Example 2:** + +**Input:** s = "eccbbbbdec" + +**Output:** [10] + +**Constraints:** + +* `1 <= s.length <= 500` +* `s` consists of lowercase English letters. + +## Solution + +```typescript +function partitionLabels(s: string): number[] { + const map = new Map() + for (let i = 0; i < s.length; i++) { + const c = s[i] + if (!map.has(c)) map.set(c, [i, i]) + else map.get(c)[1] = i + } + + const arr = Array.from(map.values()) + arr.sort((v1, v2) => v1[0] - v2[0]) + + let start = 0, + end = arr[0][1], + result: number[] = [] + for (let i = 1; i < arr.length; i++) { + if (arr[i][0] < end) end = Math.max(end, arr[i][1]) + else { + result.push(end - start + 1) + start = arr[i][0] + end = arr[i][1] + } + } + result.push(end - start + 1) + return result +} + +export { partitionLabels } +``` \ No newline at end of file diff --git a/src/main/ts/g1101_1200/s1143_longest_common_subsequence/readme.md b/src/main/ts/g1101_1200/s1143_longest_common_subsequence/readme.md new file mode 100644 index 0000000..29b6b48 --- /dev/null +++ b/src/main/ts/g1101_1200/s1143_longest_common_subsequence/readme.md @@ -0,0 +1,67 @@ +[![](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) + +## 1143\. Longest Common Subsequence + +Medium + +Given two strings `text1` and `text2`, return _the length of their longest **common subsequence**._ If there is no **common subsequence**, return `0`. + +A **subsequence** of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. + +* For example, `"ace"` is a subsequence of `"abcde"`. + +A **common subsequence** of two strings is a subsequence that is common to both strings. + +**Example 1:** + +**Input:** text1 = "abcde", text2 = "ace" + +**Output:** 3 + +**Explanation:** The longest common subsequence is "ace" and its length is 3. + +**Example 2:** + +**Input:** text1 = "abc", text2 = "abc" + +**Output:** 3 + +**Explanation:** The longest common subsequence is "abc" and its length is 3. + +**Example 3:** + +**Input:** text1 = "abc", text2 = "def" + +**Output:** 0 + +**Explanation:** There is no such common subsequence, so the result is 0. + +**Constraints:** + +* `1 <= text1.length, text2.length <= 1000` +* `text1` and `text2` consist of only lowercase English characters. + +## Solution + +```typescript +function longestCommonSubsequence(text1: string, text2: string): number { + const n: number = text1.length + const m: number = text2.length + const dp: number[][] = new Array(n + 1).fill(null).map(() => new Array(m + 1).fill(0)) + + for (let i = 1; i <= n; i++) { + for (let j = 1; j <= m; j++) { + if (text1.charAt(i - 1) === text2.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + 1 + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + } + } + } + + return dp[n][m] +} + +export { longestCommonSubsequence } +``` \ No newline at end of file