From a980667769616a38d2cec41a891f11273fc35297 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 26 Sep 2023 07:45:07 +0300 Subject: [PATCH] Added tasks 11-25 --- README.md | 214 ++++++++++-------- .../s0011_container_with_most_water/readme.md | 66 ++++++ src/main/ts/g0001_0100/s0015_3sum/readme.md | 74 ++++++ .../readme.md | 68 ++++++ .../readme.md | 67 ++++++ .../s0020_valid_parentheses/readme.md | 73 ++++++ .../s0021_merge_two_sorted_lists/readme.md | 77 +++++++ .../s0022_generate_parentheses/readme.md | 54 +++++ .../s0023_merge_k_sorted_lists/readme.md | 103 +++++++++ .../s0024_swap_nodes_in_pairs/readme.md | 87 +++++++ .../s0025_reverse_nodes_in_k_group/readme.md | 99 ++++++++ 11 files changed, 890 insertions(+), 92 deletions(-) create mode 100644 src/main/ts/g0001_0100/s0011_container_with_most_water/readme.md create mode 100644 src/main/ts/g0001_0100/s0015_3sum/readme.md create mode 100644 src/main/ts/g0001_0100/s0017_letter_combinations_of_a_phone_number/readme.md create mode 100644 src/main/ts/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md create mode 100644 src/main/ts/g0001_0100/s0020_valid_parentheses/readme.md create mode 100644 src/main/ts/g0001_0100/s0021_merge_two_sorted_lists/readme.md create mode 100644 src/main/ts/g0001_0100/s0022_generate_parentheses/readme.md create mode 100644 src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/readme.md create mode 100644 src/main/ts/g0001_0100/s0024_swap_nodes_in_pairs/readme.md create mode 100644 src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md diff --git a/README.md b/README.md index a784d22..0584796 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ [![](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) > ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews) -* [Data Structure I](#data-structure-i) * [Data Structure II](#data-structure-ii) * [Algorithm I](#algorithm-i) * [Algorithm II](#algorithm-ii) @@ -16,79 +15,7 @@ * [Level 1](#level-1) * [Level 2](#level-2) * [Udemy](#udemy) - -### Data Structure I - -#### Day 1 Array - -| | | | | | -|-|-|-|-|-|- - -#### Day 2 Array - -| | | | | | -|-|-|-|-|-|- -| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 51 | 97.89 - -#### Day 3 Array - -| | | | | | -|-|-|-|-|-|- - -#### Day 4 Array - -| | | | | | -|-|-|-|-|-|- - -#### Day 5 Array - -| | | | | | -|-|-|-|-|-|- - -#### Day 6 String - -| | | | | | -|-|-|-|-|-|- - -#### Day 7 Linked List - -| | | | | | -|-|-|-|-|-|- - -#### Day 8 Linked List - -| | | | | | -|-|-|-|-|-|- - -#### Day 9 Stack Queue - -| | | | | | -|-|-|-|-|-|- - -#### Day 10 Tree - -| | | | | | -|-|-|-|-|-|- - -#### Day 11 Tree - -| | | | | | -|-|-|-|-|-|- - -#### Day 12 Tree - -| | | | | | -|-|-|-|-|-|- - -#### Day 13 Tree - -| | | | | | -|-|-|-|-|-|- - -#### Day 14 Tree - -| | | | | | -|-|-|-|-|-|- +* [Data Structure I](#data-structure-i) ### Data Structure II @@ -96,6 +23,7 @@ | | | | | | |-|-|-|-|-|- +| 0015 |[3Sum](src/main/ts/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 148 | 92.62 #### Day 2 Array @@ -136,13 +64,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 | 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) | 480 | 35.37 #### 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 | 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)) | 88 | 93.29 #### Day 11 Linked List @@ -153,11 +81,13 @@ | | | | | | |-|-|-|-|-|- +| 0024 |[Swap Nodes in Pairs](src/main/ts/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 59 | 62.67 #### Day 13 Linked List | | | | | | |-|-|-|-|-|- +| 0025 |[Reverse Nodes in k-Group](src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 74 | 66.83 #### Day 14 Stack Queue @@ -225,12 +155,13 @@ | | | | | | |-|-|-|-|-|- +| 0019 |[Remove Nth Node From End of List](src/main/ts/g0001_0100/s0019_remove_nth_node_from_end_of_list)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 54 | 87.87 #### Day 6 Sliding Window | | | | | | |-|-|-|-|-|- -| 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 | 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) | 81 | 68.46 #### Day 7 Breadth First Search Depth First Search @@ -251,6 +182,7 @@ | | | | | | |-|-|-|-|-|- +| 0021 |[Merge Two Sorted Lists](src/main/ts/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 59 | 86.77 #### Day 11 Recursion Backtracking @@ -288,11 +220,13 @@ | | | | | | |-|-|-|-|-|- +| 0015 |[3Sum](src/main/ts/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 148 | 92.62 #### Day 4 Two Pointers | | | | | | |-|-|-|-|-|- +| 0011 |[Container With Most Water](src/main/ts/g0001_0100/s0011_container_with_most_water)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 57 | 98.23 #### Day 5 Sliding Window @@ -328,6 +262,8 @@ | | | | | | |-|-|-|-|-|- +| 0017 |[Letter Combinations of a Phone Number](src/main/ts/g0001_0100/s0017_letter_combinations_of_a_phone_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 52 | 79.11 +| 0022 |[Generate Parentheses](src/main/ts/g0001_0100/s0022_generate_parentheses)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 60 | 62.03 #### Day 12 Dynamic Programming @@ -343,7 +279,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 | 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) | 480 | 35.37 #### Day 15 Dynamic Programming @@ -630,7 +566,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 | 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) | 480 | 35.37 #### Day 18 @@ -790,7 +726,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 | 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)) | 88 | 93.29 #### Day 16 @@ -957,6 +893,7 @@ | | | | | | |-|-|-|-|-|- +| 0021 |[Merge Two Sorted Lists](src/main/ts/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 59 | 86.77 #### Day 4 Linked List @@ -1007,7 +944,7 @@ | | | | | | |-|-|-|-|-|- -| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 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) | 51 | 97.89 #### Day 14 Stack @@ -1035,6 +972,7 @@ | | | | | | |-|-|-|-|-|- +| 0019 |[Remove Nth Node From End of List](src/main/ts/g0001_0100/s0019_remove_nth_node_from_end_of_list)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 54 | 87.87 #### Day 4 Linked List @@ -1090,7 +1028,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 | 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) | 81 | 68.46 #### Day 15 Tree @@ -1135,8 +1073,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 | 81 | 68.46 -| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 480 | 35.37 +| 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 +| 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 #### Udemy Binary Search @@ -1147,12 +1086,13 @@ | | | | | | |-|-|-|-|-|- -| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 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) | 51 | 97.89 #### Udemy Two Pointers | | | | | | |-|-|-|-|-|- +| 0015 |[3Sum](src/main/ts/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 148 | 92.62 #### Udemy Famous Algorithm @@ -1173,6 +1113,9 @@ | | | | | | |-|-|-|-|-|- +| 0024 |[Swap Nodes in Pairs](src/main/ts/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 59 | 62.67 +| 0021 |[Merge Two Sorted Lists](src/main/ts/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 59 | 86.77 +| 0025 |[Reverse Nodes in k-Group](src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 74 | 66.83 #### Udemy Tree Stack Queue @@ -1193,12 +1136,14 @@ | | | | | | |-|-|-|-|-|- -| 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 | 69 | 92.65 +| 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 | | | | | | |-|-|-|-|-|- +| 0022 |[Generate Parentheses](src/main/ts/g0001_0100/s0022_generate_parentheses)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 60 | 62.03 +| 0017 |[Letter Combinations of a Phone Number](src/main/ts/g0001_0100/s0017_letter_combinations_of_a_phone_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 52 | 79.11 #### Udemy Bit Manipulation @@ -1210,18 +1155,103 @@ | | | | | | |-|-|-|-|-|- +### Data Structure I + +#### Day 1 Array + +| | | | | | +|-|-|-|-|-|- + +#### Day 2 Array + +| | | | | | +|-|-|-|-|-|- +| 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 + +#### Day 3 Array + +| | | | | | +|-|-|-|-|-|- + +#### Day 4 Array + +| | | | | | +|-|-|-|-|-|- + +#### Day 5 Array + +| | | | | | +|-|-|-|-|-|- + +#### Day 6 String + +| | | | | | +|-|-|-|-|-|- + +#### Day 7 Linked List + +| | | | | | +|-|-|-|-|-|- +| 0021 |[Merge Two Sorted Lists](src/main/ts/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 59 | 86.77 + +#### Day 8 Linked List + +| | | | | | +|-|-|-|-|-|- + +#### Day 9 Stack Queue + +| | | | | | +|-|-|-|-|-|- +| 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 + +#### Day 10 Tree + +| | | | | | +|-|-|-|-|-|- + +#### Day 11 Tree + +| | | | | | +|-|-|-|-|-|- + +#### Day 12 Tree + +| | | | | | +|-|-|-|-|-|- + +#### Day 13 Tree + +| | | | | | +|-|-|-|-|-|- + +#### Day 14 Tree + +| | | | | | +|-|-|-|-|-|- + ## Algorithms | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- -| 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, Udemy_Dynamic_Programming | 69 | 92.65 +| 0025 |[Reverse Nodes in k-Group](src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_13_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(k) | 74 | 66.83 +| 0024 |[Swap Nodes in Pairs](src/main/ts/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_12_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 59 | 62.67 +| 0023 |[Merge k Sorted Lists](src/main/ts/g0001_0100/s0023_merge_k_sorted_lists)| 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)) | 75 | 97.06 +| 0022 |[Generate Parentheses](src/main/ts/g0001_0100/s0022_generate_parentheses)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n) | 60 | 62.03 +| 0021 |[Merge Two Sorted Lists](src/main/ts/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_7_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(m+n)_Space_O(m+n) | 59 | 86.77 +| 0020 |[Valid Parentheses](src/main/ts/g0001_0100/s0020_valid_parentheses)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Data_Structure_I_Day_9_Stack_Queue, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 50 | 95.90 +| 0019 |[Remove Nth Node From End of List](src/main/ts/g0001_0100/s0019_remove_nth_node_from_end_of_list)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Algorithm_I_Day_5_Two_Pointers, Level_2_Day_3_Linked_List, Big_O_Time_O(L)_Space_O(L) | 54 | 87.87 +| 0017 |[Letter Combinations of a Phone Number](src/main/ts/g0001_0100/s0017_letter_combinations_of_a_phone_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(4^n)_Space_O(n) | 52 | 79.11 +| 0015 |[3Sum](src/main/ts/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_1_Array, Algorithm_II_Day_3_Two_Pointers, Udemy_Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 148 | 92.62 +| 0011 |[Container With Most Water](src/main/ts/g0001_0100/s0011_container_with_most_water)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers, Algorithm_II_Day_4_Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 57 | 98.23 +| 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, Udemy_Dynamic_Programming, Big_O_Time_O(m*n)_Space_O(m*n) | 69 | 92.65 | 0009 |[Palindrome Number](src/main/ts/g0001_0100/s0009_palindrome_number)| Easy | Math, Udemy_Integers | 137 | 88.64 | 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 | 480 | 35.37 -| 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 | 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 | 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 | 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 | 51 | 97.89 +| 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 +| 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 diff --git a/src/main/ts/g0001_0100/s0011_container_with_most_water/readme.md b/src/main/ts/g0001_0100/s0011_container_with_most_water/readme.md new file mode 100644 index 0000000..df9be4b --- /dev/null +++ b/src/main/ts/g0001_0100/s0011_container_with_most_water/readme.md @@ -0,0 +1,66 @@ +[![](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) + +## 11\. Container With Most Water + +Medium + +Given `n` non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). `n` vertical lines are drawn such that the two endpoints of the line `i` is at (i, ai) and `(i, 0)`. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. + +**Notice** that you may not slant the container. + +**Example 1:** + +![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg) + +**Input:** height = [1,8,6,2,5,4,8,3,7] + +**Output:** 49 + +**Explanation:** The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. + +**Example 2:** + +**Input:** height = [1,1] + +**Output:** 1 + +**Example 3:** + +**Input:** height = [4,3,2,1,4] + +**Output:** 16 + +**Example 4:** + +**Input:** height = [1,2,1] + +**Output:** 2 + +**Constraints:** + +* `n == height.length` +* 2 <= n <= 105 +* 0 <= height[i] <= 104 + +## Solution + +```typescript +function maxArea(height: number[]): number { + let maxArea = -1 + let left = 0 + let right = height.length - 1 + while (left < right) { + if (height[left] < height[right]) { + maxArea = Math.max(maxArea, height[left] * (right - left)) + left++ + } else { + maxArea = Math.max(maxArea, height[right] * (right - left)) + right-- + } + } + return maxArea +} + +export { maxArea } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0015_3sum/readme.md b/src/main/ts/g0001_0100/s0015_3sum/readme.md new file mode 100644 index 0000000..0363c6c --- /dev/null +++ b/src/main/ts/g0001_0100/s0015_3sum/readme.md @@ -0,0 +1,74 @@ +[![](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) + +## 15\. 3Sum + +Medium + +Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`. + +Notice that the solution set must not contain duplicate triplets. + +**Example 1:** + +**Input:** nums = [-1,0,1,2,-1,-4] + +**Output:** [[-1,-1,2],[-1,0,1]] + +**Example 2:** + +**Input:** nums = [] + +**Output:** [] + +**Example 3:** + +**Input:** nums = [0] + +**Output:** [] + +**Constraints:** + +* `0 <= nums.length <= 3000` +* -105 <= nums[i] <= 105 + +## Solution + +```typescript +function threeSum(nums: number[]): number[][] { //NOSONAR + nums.sort((a, b) => a - b) + const len = nums.length + const result: number[][] = [] + let l: number + let r: number + for (let i = 0; i < len - 2; i++) { + l = i + 1 + r = len - 1 + while (r > l) { + const sum = nums[i] + nums[l] + nums[r] + if (sum < 0) { + l++ + } else if (sum > 0) { + r-- + } else { + const list: number[] = [nums[i], nums[l], nums[r]] + result.push(list) + while (l < r && nums[l + 1] === nums[l]) { + l++ + } + while (r > l && nums[r - 1] === nums[r]) { + r-- + } + l++ + r-- + } + } + while (i < len - 1 && nums[i + 1] === nums[i]) { + i++ //NOSONAR + } + } + return result +} + +export { threeSum } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0017_letter_combinations_of_a_phone_number/readme.md b/src/main/ts/g0001_0100/s0017_letter_combinations_of_a_phone_number/readme.md new file mode 100644 index 0000000..7842568 --- /dev/null +++ b/src/main/ts/g0001_0100/s0017_letter_combinations_of_a_phone_number/readme.md @@ -0,0 +1,68 @@ +[![](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) + +## 17\. Letter Combinations of a Phone Number + +Medium + +Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent. Return the answer in **any order**. + +A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. + +![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png) + +**Example 1:** + +**Input:** digits = "23" + +**Output:** ["ad","ae","af","bd","be","bf","cd","ce","cf"] + +**Example 2:** + +**Input:** digits = "" + +**Output:** [] + +**Example 3:** + +**Input:** digits = "2" + +**Output:** ["a","b","c"] + +**Constraints:** + +* `0 <= digits.length <= 4` +* `digits[i]` is a digit in the range `['2', '9']`. + +## Solution + +```typescript +function letterCombinations(digits: string): string[] { + if (digits.length === 0) { + return [] + } + const letters: string[] = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'] + const ans: string[] = [] + const sb: string[] = [] + findCombinations(0, digits, letters, sb, ans) + return ans +} + +function findCombinations(start: number, nums: string, letters: string[], curr: string[], ans: string[]): void { + if (curr.length === nums.length) { + ans.push(curr.join('')) + return + } + for (let i = start; i < nums.length; i++) { + const n = parseInt(nums.charAt(i)) + for (let j = 0; j < letters[n].length; j++) { + const ch = letters[n].charAt(j) + curr.push(ch) + findCombinations(i + 1, nums, letters, curr, ans) + curr.pop() + } + } +} + +export { letterCombinations } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md b/src/main/ts/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md new file mode 100644 index 0000000..449678b --- /dev/null +++ b/src/main/ts/g0001_0100/s0019_remove_nth_node_from_end_of_list/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) + +## 19\. Remove Nth Node From End of List + +Medium + +Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg) + +**Input:** head = [1,2,3,4,5], n = 2 + +**Output:** [1,2,3,5] + +**Example 2:** + +**Input:** head = [1], n = 1 + +**Output:** [] + +**Example 3:** + +**Input:** head = [1,2], n = 1 + +**Output:** [1] + +**Constraints:** + +* The number of nodes in the list is `sz`. +* `1 <= sz <= 30` +* `0 <= Node.val <= 100` +* `1 <= n <= sz` + +**Follow up:** Could you do this in one pass? + +## Solution + +```typescript +import { ListNode } from '../../com_github_leetcode/listnode' + +let localN: number + +function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { + localN = n + const dummy = new ListNode(0) + dummy.next = head + removeNth(dummy) + return dummy.next +} + +function removeNth(node: ListNode | null): void { + if (!node || !node.next) { //NOSONAR + return + } + removeNth(node.next) + localN-- + + if (localN === 0) { + node.next = node.next?.next || null //NOSONAR + } +} + +export { removeNthFromEnd } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0020_valid_parentheses/readme.md b/src/main/ts/g0001_0100/s0020_valid_parentheses/readme.md new file mode 100644 index 0000000..fa2afb1 --- /dev/null +++ b/src/main/ts/g0001_0100/s0020_valid_parentheses/readme.md @@ -0,0 +1,73 @@ +[![](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) + +## 20\. Valid Parentheses + +Easy + +Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid. + +An input string is valid if: + +1. Open brackets must be closed by the same type of brackets. +2. Open brackets must be closed in the correct order. + +**Example 1:** + +**Input:** s = "()" + +**Output:** true + +**Example 2:** + +**Input:** s = "()[]{}" + +**Output:** true + +**Example 3:** + +**Input:** s = "(]" + +**Output:** false + +**Example 4:** + +**Input:** s = "([)]" + +**Output:** false + +**Example 5:** + +**Input:** s = "{[]}" + +**Output:** true + +**Constraints:** + +* 1 <= s.length <= 104 +* `s` consists of parentheses only `'()[]{}'`. + +## Solution + +```typescript +function isValid(s: string): boolean { + const stack: string[] = [] + for (let i = 0; i < s.length; i++) { + const c = s.charAt(i) + if (c === '(' || c === '[' || c === '{') { + stack.push(c) + } else if ( + (c === ')' && stack.length > 0 && stack[stack.length - 1] === '(') || + (c === '}' && stack.length > 0 && stack[stack.length - 1] === '{') || + (c === ']' && stack.length > 0 && stack[stack.length - 1] === '[') + ) { + stack.pop() + } else { + return false + } + } + return stack.length === 0 +} + +export { isValid } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0021_merge_two_sorted_lists/readme.md b/src/main/ts/g0001_0100/s0021_merge_two_sorted_lists/readme.md new file mode 100644 index 0000000..032657d --- /dev/null +++ b/src/main/ts/g0001_0100/s0021_merge_two_sorted_lists/readme.md @@ -0,0 +1,77 @@ +[![](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) + +## 21\. Merge Two Sorted Lists + +Easy + +Merge two sorted linked lists and return it as a **sorted** list. The list should be made by splicing together the nodes of the first two lists. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg) + +**Input:** l1 = [1,2,4], l2 = [1,3,4] + +**Output:** [1,1,2,3,4,4] + +**Example 2:** + +**Input:** l1 = [], l2 = [] + +**Output:** [] + +**Example 3:** + +**Input:** l1 = [], l2 = [0] + +**Output:** [0] + +**Constraints:** + +* The number of nodes in both lists is in the range `[0, 50]`. +* `-100 <= Node.val <= 100` +* Both `l1` and `l2` are sorted in **non-decreasing** order. + +## Solution + +```typescript +import { ListNode } from '../../com_github_leetcode/listnode' + +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ +function mergeTwoLists(l1: ListNode | null, l2: ListNode | null): ListNode | null { + let list = new ListNode(-1) + let head = list + while (l1 !== null || l2 !== null) { + if (l1 !== null && l2 !== null) { + if (l1.val <= l2.val) { + list.next = new ListNode(l1.val) + l1 = l1.next + } else { + list.next = new ListNode(l2.val) + l2 = l2.next + } + } else if (l1 !== null) { + list.next = new ListNode(l1.val) + l1 = l1.next + } else if (l2 !== null) { + list.next = new ListNode(l2.val) + l2 = l2.next + } + list = list.next! + } + return head.next +} + +export { mergeTwoLists } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0022_generate_parentheses/readme.md b/src/main/ts/g0001_0100/s0022_generate_parentheses/readme.md new file mode 100644 index 0000000..abf94e3 --- /dev/null +++ b/src/main/ts/g0001_0100/s0022_generate_parentheses/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) + +## 22\. Generate Parentheses + +Medium + +Given `n` pairs of parentheses, write a function to _generate all combinations of well-formed parentheses_. + +**Example 1:** + +**Input:** n = 3 + +**Output:** ["((()))","(()())","(())()","()(())","()()()"] + +**Example 2:** + +**Input:** n = 1 + +**Output:** ["()"] + +**Constraints:** + +* `1 <= n <= 8` + +## Solution + +```typescript +function generateParenthesis(n: number): string[] { + const sb: string[] = [] + const ans: string[] = [] + return generate(sb, ans, n, n) +} + +function generate(sb: string[], str: string[], open: number, close: number): string[] { + if (open === 0 && close === 0) { + str.push(sb.join('')) + return str + } + if (open > 0) { + sb.push('(') + generate(sb, str, open - 1, close) + sb.pop() + } + if (close > 0 && open < close) { + sb.push(')') + generate(sb, str, open, close - 1) + sb.pop() + } + return str +} + +export { generateParenthesis } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/readme.md b/src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/readme.md new file mode 100644 index 0000000..cda0772 --- /dev/null +++ b/src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/readme.md @@ -0,0 +1,103 @@ +[![](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) + +## 23\. Merge k Sorted Lists + +Hard + +You are given an array of `k` linked-lists `lists`, each linked-list is sorted in ascending order. + +_Merge all the linked-lists into one sorted linked-list and return it._ + +**Example 1:** + +**Input:** lists = \[\[1,4,5],[1,3,4],[2,6]] + +**Output:** [1,1,2,3,4,4,5,6] + +**Explanation:** The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted list: 1->1->2->3->4->4->5->6 + +**Example 2:** + +**Input:** lists = [] + +**Output:** [] + +**Example 3:** + +**Input:** lists = \[\[]] + +**Output:** [] + +**Constraints:** + +* `k == lists.length` +* `0 <= k <= 10^4` +* `0 <= lists[i].length <= 500` +* `-10^4 <= lists[i][j] <= 10^4` +* `lists[i]` is sorted in **ascending order**. +* The sum of `lists[i].length` won't exceed `10^4`. + +## Solution + +```typescript +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ +function mergeKLists(lists: Array): ListNode | null { + if (lists.length === 0) { + return null + } + return mergeKListsRecursive(lists, 0, lists.length) +} + +function mergeKListsRecursive(lists: Array, leftIndex: number, rightIndex: number): ListNode | null { + if (rightIndex > leftIndex + 1) { + const mid = Math.floor((leftIndex + rightIndex) / 2) + const left = mergeKListsRecursive(lists, leftIndex, mid) + const right = mergeKListsRecursive(lists, mid, rightIndex) + return mergeTwoLists(left, right) + } else { + return lists[leftIndex] + } +} + +function mergeTwoLists(left: ListNode | null, right: ListNode | null): ListNode | null { + if (left === null) { + return right + } + if (right === null) { + return left + } + let res: ListNode | null + if (left.val <= right.val) { + res = left + left = left.next + } else { + res = right + right = right.next + } + let node = res + while (left !== null || right !== null) { + if (right === null || left.val <= right.val) { + node.next = left + left = left.next + } else { + node.next = right + right = right.next + } + node = node.next + } + return res +} + +export { mergeKLists } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0024_swap_nodes_in_pairs/readme.md b/src/main/ts/g0001_0100/s0024_swap_nodes_in_pairs/readme.md new file mode 100644 index 0000000..046a71c --- /dev/null +++ b/src/main/ts/g0001_0100/s0024_swap_nodes_in_pairs/readme.md @@ -0,0 +1,87 @@ +[![](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) + +## 24\. Swap Nodes in Pairs + +Medium + +Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg) + +**Input:** head = [1,2,3,4] + +**Output:** [2,1,4,3] + +**Example 2:** + +**Input:** head = [] + +**Output:** [] + +**Example 3:** + +**Input:** head = [1] + +**Output:** [1] + +**Constraints:** + +* The number of nodes in the list is in the range `[0, 100]`. +* `0 <= Node.val <= 100` + +## Solution + +```typescript +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ +function swapPairs(head: ListNode | null): ListNode | null { + if (head === null) { + return null + } + const len = getLength(head) + return reverse(head, len) +} + +function getLength(curr: ListNode | null): number { + let cnt = 0 + while (curr !== null) { + cnt++ + curr = curr.next + } + return cnt +} + +// Recursive function to reverse in groups +function reverse(head: ListNode | null, len: number): ListNode | null { + // base case + if (len < 2) { + return head + } + let curr: ListNode | null = head + let prev: ListNode | null = null + let next: ListNode | null + for (let i = 0; i < 2; i++) { + // reverse linked list code + next = curr.next + curr.next = prev + prev = curr + curr = next + } + head.next = reverse(curr, len - 2) + return prev +} + +export { swapPairs } +``` \ No newline at end of file diff --git a/src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md b/src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md new file mode 100644 index 0000000..3aa9f24 --- /dev/null +++ b/src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md @@ -0,0 +1,99 @@ +[![](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) + +## 25\. Reverse Nodes in k-Group + +Hard + +Given a linked list, reverse the nodes of a linked list _k_ at a time and return its modified list. + +_k_ is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of _k_ then left-out nodes, in the end, should remain as it is. + +You may not alter the values in the list's nodes, only nodes themselves may be changed. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/10/03/reverse_ex1.jpg) + +**Input:** head = [1,2,3,4,5], k = 2 + +**Output:** [2,1,4,3,5] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg) + +**Input:** head = [1,2,3,4,5], k = 3 + +**Output:** [3,2,1,4,5] + +**Example 3:** + +**Input:** head = [1,2,3,4,5], k = 1 + +**Output:** [1,2,3,4,5] + +**Example 4:** + +**Input:** head = [1], k = 1 + +**Output:** [1] + +**Constraints:** + +* The number of nodes in the list is in the range `sz`. +* `1 <= sz <= 5000` +* `0 <= Node.val <= 1000` +* `1 <= k <= sz` + +**Follow-up:** Can you solve the problem in O(1) extra memory space? + +## Solution + +```typescript +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ +function reverseKGroup(head: ListNode | null, k: number): ListNode | null { + if (head === null || head.next === null || k === 1) { + return head + } + let j = 0 + let len = head + // Loop for checking the length of the linked list. If the linked list is less than k, then return as it is. + while (j < k) { + if (len === null) { + return head + } + len = len.next + j++ + } + // Reverse linked list logic applied here. + let c = head + let n = null + let prev = null + let i = 0 + // Traverse the while loop for K times to reverse the nodes in K groups. + while (i !== k) { + n = c.next + c.next = prev + prev = c + c = n + i++ + } + // 'head' now points to the last node of the reversed K-group. + // Recursion for further remaining linked list. + head.next = reverseKGroup(n, k) + return prev +} + +export { reverseKGroup } +``` \ No newline at end of file