From 5ff6349b8790422efd72f799a0ec61d1b50b24f8 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 27 Sep 2022 04:07:13 +0300 Subject: [PATCH] Added tasks 59-97. --- README.md | 114 +++++++++++----- .../s0002_add_two_numbers/readme.md | 9 +- .../kotlin/g0001_0100/s0050_powx_n/readme.md | 1 + .../s0059_spiral_matrix_ii/readme.md | 64 +++++++++ .../s0060_permutation_sequence/readme.md | 77 +++++++++++ .../g0001_0100/s0061_rotate_list/readme.md | 80 +++++++++++ .../s0063_unique_paths_ii/readme.md | 85 ++++++++++++ .../g0001_0100/s0065_valid_number/readme.md | 91 +++++++++++++ .../g0001_0100/s0066_plus_one/readme.md | 86 ++++++++++++ .../g0001_0100/s0067_add_binary/readme.md | 56 ++++++++ .../s0068_text_justification/readme.md | 128 ++++++++++++++++++ .../kotlin/g0001_0100/s0069_sqrtx/readme.md | 60 ++++++++ .../g0001_0100/s0071_simplify_path/readme.md | 87 ++++++++++++ .../g0001_0100/s0077_combinations/readme.md | 65 +++++++++ .../readme.md | 73 ++++++++++ .../readme.md | 58 ++++++++ .../readme.md | 75 ++++++++++ .../readme.md | 64 +++++++++ .../s0085_maximal_rectangle/readme.md | 125 +++++++++++++++++ .../g0001_0100/s0086_partition_list/readme.md | 70 ++++++++++ .../s0087_scramble_string/readme.md | 60 ++++++++ .../s0088_merge_sorted_array/readme.md | 65 +++++++++ .../g0001_0100/s0089_gray_code/readme.md | 61 +++++++++ .../g0001_0100/s0090_subsets_ii/readme.md | 61 +++++++++ .../g0001_0100/s0091_decode_ways/readme.md | 81 +++++++++++ .../s0092_reverse_linked_list_ii/readme.md | 104 ++++++++++++++ .../s0093_restore_ip_addresses/readme.md | 75 ++++++++++ .../readme.md | 10 ++ .../readme.md | 84 ++++++++++++ .../s0097_interleaving_string/readme.md | 89 ++++++++++++ .../readme.md | 10 ++ .../g0101_0200/s0101_symmetric_tree/readme.md | 10 ++ .../readme.md | 10 ++ .../readme.md | 10 ++ .../readme.md | 10 ++ .../readme.md | 10 ++ .../s0226_invert_binary_tree/readme.md | 10 ++ .../readme.md | 10 ++ .../g0301_0400/s0338_counting_bits/readme.md | 2 +- 39 files changed, 2200 insertions(+), 40 deletions(-) create mode 100644 src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0060_permutation_sequence/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0061_rotate_list/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0063_unique_paths_ii/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0065_valid_number/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0066_plus_one/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0067_add_binary/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0068_text_justification/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0069_sqrtx/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0071_simplify_path/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0077_combinations/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0085_maximal_rectangle/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0086_partition_list/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0087_scramble_string/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0088_merge_sorted_array/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0089_gray_code/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0090_subsets_ii/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0091_decode_ways/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0092_reverse_linked_list_ii/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0093_restore_ip_addresses/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0095_unique_binary_search_trees_ii/readme.md create mode 100644 src/main/kotlin/g0001_0100/s0097_interleaving_string/readme.md diff --git a/README.md b/README.md index 55b9d9a9..fee6904f 100644 --- a/README.md +++ b/README.md @@ -37,13 +37,14 @@ | | | | | | |-|-|-|-|-|- +| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List | 241 | 91.04 | 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 1059 | 55.28 #### Day 4 Two Pointers | | | | | | |-|-|-|-|-|- -| 0011 |[Container With Most Water](src/main/kotlin/g0001_0100/s0011_container_with_most_water)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers | 635 | 64.22 +| 0011 |[Container With Most Water](src/main/kotlin/g0001_0100/s0011_container_with_most_water)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers | 474 | 89.18 #### Day 5 Sliding Window @@ -72,6 +73,7 @@ | | | | | | |-|-|-|-|-|- | 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking | 353 | 30.14 +| 0090 |[Subsets II](src/main/kotlin/g0001_0100/s0090_subsets_ii)| Medium | Array, Bit_Manipulation, Backtracking | 366 | 58.09 #### Day 10 Recursion Backtracking @@ -106,12 +108,13 @@ | | | | | | |-|-|-|-|-|- -| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 359 | 69.08 +| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48 #### Day 15 Dynamic Programming | | | | | | |-|-|-|-|-|- +| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways)| Medium | Top_Interview_Questions, String, Dynamic_Programming | 237 | 76.88 | 0139 |[Word Break](src/main/kotlin/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization | 197 | 87.17 #### Day 16 Dynamic Programming @@ -170,6 +173,7 @@ | | | | | | |-|-|-|-|-|- +| 0069 |[Sqrt(x)](src/main/kotlin/g0001_0100/s0069_sqrtx)| Easy | Top_Interview_Questions, Math, Binary_Search | 153 | 95.75 #### Day 5 @@ -279,6 +283,7 @@ | | | | | | |-|-|-|-|-|- +| 0081 |[Search in Rotated Sorted Array II](src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii)| Medium | Array, Binary_Search | 352 | 42.31 #### Day 13 @@ -380,6 +385,7 @@ | | | | | | |-|-|-|-|-|- +| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways)| Medium | Top_Interview_Questions, String, Dynamic_Programming | 237 | 76.88 #### Day 11 @@ -407,6 +413,7 @@ | | | | | | |-|-|-|-|-|- | 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics | 209 | 49.18 +| 0063 |[Unique Paths II](src/main/kotlin/g0001_0100/s0063_unique_paths_ii)| Medium | Array, Dynamic_Programming, Matrix | 187 | 84.62 #### Day 16 @@ -419,7 +426,7 @@ | | | | | | |-|-|-|-|-|- -| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 359 | 69.08 +| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48 #### Day 18 @@ -525,6 +532,7 @@ | | | | | | |-|-|-|-|-|- +| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math | 303 | 35.18 #### Day 4 @@ -536,6 +544,7 @@ | | | | | | |-|-|-|-|-|- +| 0067 |[Add Binary](src/main/kotlin/g0001_0100/s0067_add_binary)| Easy | String, Math, Bit_Manipulation, Simulation | 327 | 32.67 #### Day 6 @@ -593,12 +602,13 @@ | | | | | | |-|-|-|-|-|- -| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion | 212 | 93.71 +| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion | 417 | 50.44 #### Day 16 | | | | | | |-|-|-|-|-|- +| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List | 355 | 13.54 #### Day 17 @@ -828,7 +838,7 @@ | | | | | | |-|-|-|-|-|- -| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion | 268 | 23.13 +| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion | 224 | 64.86 #### Day 15 Heap @@ -923,7 +933,7 @@ | | | | | | |-|-|-|-|-|- -| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 212 | 91.24 +| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 258 | 91.09 | 0016 |[3Sum Closest](src/main/kotlin/g0001_0100/s0016_3sum_closest)| Medium | Array, Sorting, Two_Pointers | 219 | 89.56 | 0076 |[Minimum Window Substring](src/main/kotlin/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 346 | 85.20 @@ -972,19 +982,19 @@ | | | | | | |-|-|-|-|-|- | 0136 |[Single Number](src/main/kotlin/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation | 344 | 83.63 -| 0007 |[Reverse Integer](src/main/kotlin/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math | 230 | 57.36 -| 0009 |[Palindrome Number](src/main/kotlin/g0001_0100/s0009_palindrome_number)| Easy | Math | 208 | 94.88 -| 0050 |[Pow(x, n)](src/main/kotlin/g0001_0100/s0050_powx_n)| Medium | Top_Interview_Questions, Math, Recursion | 307 | 17.33 +| 0007 |[Reverse Integer](src/main/kotlin/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math | 245 | 60.32 +| 0009 |[Palindrome Number](src/main/kotlin/g0001_0100/s0009_palindrome_number)| Easy | Math | 238 | 96.24 +| 0050 |[Pow(x, n)](src/main/kotlin/g0001_0100/s0050_powx_n)| Medium | Top_Interview_Questions, Math, Recursion | 264 | 52.98 #### Udemy Strings | | | | | | |-|-|-|-|-|- | 0014 |[Longest Common Prefix](src/main/kotlin/g0001_0100/s0014_longest_common_prefix)| Easy | Top_Interview_Questions, String | 269 | 48.51 -| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 212 | 91.24 +| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 258 | 91.09 | 0020 |[Valid Parentheses](src/main/kotlin/g0001_0100/s0020_valid_parentheses)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack | 196 | 71.08 -| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 359 | 69.08 -| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion | 268 | 23.13 +| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48 +| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion | 224 | 64.86 | 0049 |[Group Anagrams](src/main/kotlin/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting | 506 | 86.55 #### Udemy Binary Search @@ -1002,9 +1012,11 @@ | 0283 |[Move Zeroes](src/main/kotlin/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 516 | 79.07 | 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 234 | 92.75 | 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String | 243 | 63.33 +| 0080 |[Remove Duplicates from Sorted Array II](src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii)| Medium | Array, Two_Pointers | 357 | 44.78 | 0189 |[Rotate Array](src/main/kotlin/g0101_0200/s0189_rotate_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers | 483 | 86.95 | 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 670 | 66.67 | 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66 +| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math | 303 | 35.18 | 0238 |[Product of Array Except Self](src/main/kotlin/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum | 669 | 48.96 | 0041 |[First Missing Positive](src/main/kotlin/g0001_0100/s0041_first_missing_positive)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 345 | 100.00 | 0239 |[Sliding Window Maximum](src/main/kotlin/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue | 1059 | 86.14 @@ -1043,7 +1055,8 @@ | | | | | | |-|-|-|-|-|- -| 0114 |[Flatten Binary Tree to Linked List](src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List | 305 | 26.30 +| 0114 |[Flatten Binary Tree to Linked List](src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List | 191 | 93.10 +| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List | 355 | 13.54 | 0024 |[Swap Nodes in Pairs](src/main/kotlin/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion | 149 | 99.39 | 0142 |[Linked List Cycle II](src/main/kotlin/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List | 192 | 63.39 | 0141 |[Linked List Cycle](src/main/kotlin/g0101_0200/s0141_linked_list_cycle)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List | 223 | 91.85 @@ -1059,7 +1072,7 @@ | | | | | | |-|-|-|-|-|- -| 0094 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack | 283 | 17.97 +| 0094 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack | 269 | 38.80 | 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 355 | 29.37 | 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree | 307 | 43.93 | 0226 |[Invert Binary Tree](src/main/kotlin/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 275 | 13.68 @@ -1092,7 +1105,7 @@ | 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 307 | 38.36 | 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming | 320 | 63.53 | 0044 |[Wildcard Matching](src/main/kotlin/g0001_0100/s0044_wildcard_matching)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Greedy, Recursion | 401 | 86.11 -| 0010 |[Regular Expression Matching](src/main/kotlin/g0001_0100/s0010_regular_expression_matching)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion | 234 | 85.18 +| 0010 |[Regular Expression Matching](src/main/kotlin/g0001_0100/s0010_regular_expression_matching)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion | 292 | 58.58 #### Udemy Backtracking/Recursion @@ -1108,7 +1121,7 @@ | | | | | | |-|-|-|-|-|- -| 0338 |[Longest Increasing Subsequence](src/main/kotlin/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation | 413 | 26.03 +| 0338 |[Longest Increasing Subsequence](src/main/kotlin/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation | 186 | 99.26 | 0029 |[Divide Two Integers](src/main/kotlin/g0001_0100/s0029_divide_two_integers)| Medium | Top_Interview_Questions, Math, Bit_Manipulation | 281 | 31.67 #### Udemy Design @@ -1130,6 +1143,7 @@ | | | | | | |-|-|-|-|-|- | 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 234 | 92.75 +| 0088 |[Merge Sorted Array](src/main/kotlin/g0001_0100/s0088_merge_sorted_array)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers | 311 | 33.40 #### Day 3 Array @@ -1166,6 +1180,7 @@ | | | | | | |-|-|-|-|-|- | 0206 |[Reverse Linked List](src/main/kotlin/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion | 309 | 15.90 +| 0083 |[Remove Duplicates from Sorted List](src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list)| Easy | Linked_List | 274 | 77.82 #### Day 9 Stack Queue @@ -1177,7 +1192,7 @@ | | | | | | |-|-|-|-|-|- -| 0094 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack | 283 | 17.97 +| 0094 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack | 269 | 38.80 #### Day 11 Tree @@ -1225,6 +1240,7 @@ | | | | | | |-|-|-|-|-|- | 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix | 287 | 46.50 +| 0059 |[Spiral Matrix II](src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii)| Medium | Array, Matrix, Simulation | 153 | 100.00 #### Day 4 Array @@ -1261,13 +1277,13 @@ | | | | | | |-|-|-|-|-|- -| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 359 | 69.08 +| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48 #### Day 10 Linked List | | | | | | |-|-|-|-|-|- -| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion | 212 | 93.71 +| 0002 |[Add Two Numbers](src/main/kotlin/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion | 417 | 50.44 | 0142 |[Linked List Cycle II](src/main/kotlin/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List | 192 | 63.39 #### Day 11 Linked List @@ -1275,6 +1291,7 @@ | | | | | | |-|-|-|-|-|- | 0160 |[Intersection of Two Linked Lists](src/main/kotlin/g0101_0200/s0160_intersection_of_two_linked_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List | 262 | 83.50 +| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List | 241 | 91.04 #### Day 12 Linked List @@ -1370,7 +1387,7 @@ | | | | | | |-|-|-|-|-|- -| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 212 | 91.24 +| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 258 | 91.09 #### Day 7 Breadth First Search Depth First Search @@ -1399,6 +1416,7 @@ | | | | | | |-|-|-|-|-|- +| 0077 |[Combinations](src/main/kotlin/g0001_0100/s0077_combinations)| Medium | Backtracking | 244 | 100.00 | 0046 |[Permutations](src/main/kotlin/g0001_0100/s0046_permutations)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking | 186 | 100.00 #### Day 12 Dynamic Programming @@ -1435,10 +1453,10 @@ | 0438 |[Find All Anagrams in a String](src/main/kotlin/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 | 561 | 54.68 | 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12 | 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56 -| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings | 268 | 23.13 +| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings | 224 | 64.86 | 0378 |[Kth Smallest Element in a Sorted Matrix](src/main/kotlin/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix)| Medium | Top_Interview_Questions, Array, Sorting, Binary_Search, Matrix, Heap_Priority_Queue | 522 | 59.78 | 0347 |[Top K Frequent Elements](src/main/kotlin/g0301_0400/s0347_top_k_frequent_elements)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue | 268 | 99.74 -| 0338 |[Longest Increasing Subsequence](src/main/kotlin/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation | 413 | 26.03 +| 0338 |[Longest Increasing Subsequence](src/main/kotlin/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation | 186 | 99.26 | 0322 |[Coin Change](src/main/kotlin/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming | 332 | 50.68 | 0300 |[Longest Increasing Subsequence](src/main/kotlin/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming | 318 | 82.28 | 0295 |[Find Median from Data Stream](src/main/kotlin/g0201_0300/s0295_find_median_from_data_stream)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream | 2289 | 33.60 @@ -1476,24 +1494,50 @@ | 0128 |[Longest Consecutive Sequence](src/main/kotlin/g0101_0200/s0128_longest_consecutive_sequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find | 460 | 97.77 | 0124 |[Binary Tree Maximum Path Sum](src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue | 331 | 74.42 | 0121 |[Best Time to Buy and Sell Stock](src/main/kotlin/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays | 609 | 94.06 -| 0114 |[Flatten Binary Tree to Linked List](src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List | 305 | 26.30 +| 0114 |[Flatten Binary Tree to Linked List](src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List | 191 | 93.10 | 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree | 370 | 58.31 | 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal)| 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 | 355 | 29.37 | 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree)| 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 | 290 | 26.98 | 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| 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 | 330 | 41.38 +| 0097 |[Interleaving String](src/main/kotlin/g0001_0100/s0097_interleaving_string)| Medium | String, Dynamic_Programming | 240 | 57.50 | 0096 |[Unique Binary Search Trees](src/main/kotlin/g0001_0100/s0096_unique_binary_search_trees)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11 | 237 | 26.76 -| 0094 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue | 283 | 17.97 +| 0095 |[Unique Binary Search Trees II](src/main/kotlin/g0001_0100/s0095_unique_binary_search_trees_ii)| Medium | Dynamic_Programming, Tree, Binary_Tree, Backtracking, Binary_Search_Tree | 360 | 41.38 +| 0094 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue | 269 | 38.80 +| 0093 |[Restore IP Addresses](src/main/kotlin/g0001_0100/s0093_restore_ip_addresses)| Medium | String, Backtracking | 304 | 73.33 +| 0092 |[Reverse Linked List II](src/main/kotlin/g0001_0100/s0092_reverse_linked_list_ii)| Medium | Linked_List | 191 | 82.35 +| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways)| Medium | Top_Interview_Questions, String, Dynamic_Programming, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_10 | 237 | 76.88 +| 0090 |[Subsets II](src/main/kotlin/g0001_0100/s0090_subsets_ii)| Medium | Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking | 366 | 58.09 +| 0089 |[Gray Code](src/main/kotlin/g0001_0100/s0089_gray_code)| Medium | Math, Bit_Manipulation, Backtracking | 273 | 100.00 +| 0088 |[Merge Sorted Array](src/main/kotlin/g0001_0100/s0088_merge_sorted_array)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_I_Day_2_Array | 311 | 33.40 +| 0087 |[Partition List](src/main/kotlin/g0001_0100/s0087_scramble_string)| Hard | String, Dynamic_Programming | 366 | 85.00 +| 0086 |[Partition List](src/main/kotlin/g0001_0100/s0086_partition_list)| Medium | Two_Pointers, Linked_List | 331 | 5.88 +| 0085 |[Maximal Rectangle](src/main/kotlin/g0001_0100/s0085_maximal_rectangle)| Hard | Array, Dynamic_Programming, Matrix, Stack, Monotonic_Stack | 463 | 55.17 | 0084 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0084_largest_rectangle_in_histogram)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Stack, Monotonic_Stack | 525 | 100.00 +| 0083 |[Remove Duplicates from Sorted List](src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list)| Easy | Linked_List, Data_Structure_I_Day_8_Linked_List | 274 | 77.82 +| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Algorithm_II_Day_3_Two_Pointers | 241 | 91.04 +| 0081 |[Search in Rotated Sorted Array II](src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii)| Medium | Array, Binary_Search, Binary_Search_II_Day_12 | 352 | 42.31 +| 0080 |[Remove Duplicates from Sorted Array II](src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii)| Medium | Array, Two_Pointers, Udemy_Arrays | 357 | 44.78 | 0079 |[Word Search](src/main/kotlin/g0001_0100/s0079_word_search)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking | 463 | 68.49 | 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking, Udemy_Backtracking/Recursion | 353 | 30.14 +| 0077 |[Combinations](src/main/kotlin/g0001_0100/s0077_combinations)| Medium | Backtracking, Algorithm_I_Day_11_Recursion_Backtracking | 244 | 100.00 | 0076 |[Minimum Window Substring](src/main/kotlin/g0001_0100/s0076_minimum_window_substring)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer | 346 | 85.20 | 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_2_Array, Udemy_Arrays | 198 | 85.66 | 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Data_Structure_I_Day_5_Array, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_8, Level_2_Day_8_Binary_Search, Udemy_2D_Arrays/Matrix | 290 | 40.17 | 0073 |[Set Matrix Zeroes](src/main/kotlin/g0001_0100/s0073_set_matrix_zeroes)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Udemy_2D_Arrays/Matrix | 255 | 100.00 | 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 320 | 63.53 +| 0071 |[Simplify Path](src/main/kotlin/g0001_0100/s0071_simplify_path)| Medium | String, Stack | 318 | 64.44 | 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming | 127 | 97.06 +| 0069 |[Sqrt(x)](src/main/kotlin/g0001_0100/s0069_sqrtx)| Easy | Top_Interview_Questions, Math, Binary_Search, Binary_Search_I_Day_4 | 153 | 95.75 +| 0068 |[Text Justification](src/main/kotlin/g0001_0100/s0068_text_justification)| Hard | Array, String, Simulation | 269 | 50.00 +| 0067 |[Add Binary](src/main/kotlin/g0001_0100/s0067_add_binary)| Easy | String, Math, Bit_Manipulation, Simulation, Programming_Skills_II_Day_5 | 327 | 32.67 +| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one)| Easy | Top_Interview_Questions, Array, Math, Programming_Skills_II_Day_3, Udemy_Arrays | 303 | 35.18 +| 0065 |[Valid Number](src/main/kotlin/g0001_0100/s0065_valid_number)| Hard | String | 310 | 81.82 | 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming | 222 | 95.70 +| 0063 |[Unique Paths II](src/main/kotlin/g0001_0100/s0063_unique_paths_ii)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_15 | 187 | 84.62 | 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_15, Level_1_Day_11_Dynamic_Programming | 209 | 49.18 +| 0061 |[Rotate List](src/main/kotlin/g0001_0100/s0061_rotate_list)| Medium | Two_Pointers, Linked_List, Programming_Skills_II_Day_16, Udemy_Linked_List | 355 | 13.54 +| 0060 |[Permutation Sequence](src/main/kotlin/g0001_0100/s0060_permutation_sequence)| Hard | Math, Recursion | 293 | 27.78 +| 0059 |[Spiral Matrix II](src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii)| Medium | Array, Matrix, Simulation, Data_Structure_II_Day_3_Array | 153 | 100.00 | 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word)| Easy | String, Programming_Skills_II_Day_6, Udemy_Arrays | 243 | 63.33 | 0057 |[Insert Interval](src/main/kotlin/g0001_0100/s0057_insert_interval)| Medium | Array, Level_2_Day_17_Interval | 257 | 99.52 | 0056 |[Merge Intervals](src/main/kotlin/g0001_0100/s0056_merge_intervals)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Data_Structure_II_Day_2_Array, Level_2_Day_17_Interval, Udemy_2D_Arrays/Matrix | 323 | 99.68 @@ -1502,7 +1546,7 @@ | 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Data_Structure_I_Day_1_Array, Dynamic_Programming_I_Day_5, Udemy_Famous_Algorithm | 662 | 82.48 | 0052 |[N-Queens II](src/main/kotlin/g0001_0100/s0052_n_queens_ii)| Hard | Backtracking | 231 | 80.00 | 0051 |[N-Queens](src/main/kotlin/g0001_0100/s0051_n_queens)| Hard | Top_100_Liked_Questions, Array, Backtracking | 243 | 95.10 -| 0050 |[Pow(x, n)](src/main/kotlin/g0001_0100/s0050_powx_n)| Medium | Top_Interview_Questions, Math, Recursion, Udemy_Integers | 307 | 17.33 +| 0050 |[Pow(x, n)](src/main/kotlin/g0001_0100/s0050_powx_n)| Medium | Top_Interview_Questions, Math, Recursion, Udemy_Integers | 264 | 52.98 | 0049 |[Group Anagrams](src/main/kotlin/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Data_Structure_II_Day_8_String, Programming_Skills_II_Day_11, Udemy_Strings | 506 | 86.55 | 0048 |[Rotate Image](src/main/kotlin/g0001_0100/s0048_rotate_image)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Data_Structure_II_Day_3_Array, Programming_Skills_II_Day_7, Udemy_2D_Arrays/Matrix | 287 | 46.50 | 0047 |[Permutations II](src/main/kotlin/g0001_0100/s0047_permutations_ii)| Medium | Array, Backtracking, Algorithm_II_Day_10_Recursion_Backtracking | 406 | 76.36 @@ -1541,15 +1585,15 @@ | 0014 |[Longest Common Prefix](src/main/kotlin/g0001_0100/s0014_longest_common_prefix)| Easy | Top_Interview_Questions, String | 269 | 48.51 | 0013 |[Roman to Integer](src/main/kotlin/g0001_0100/s0013_roman_to_integer)| Easy | Top_Interview_Questions, String, Hash_Table, Math | 293 | 78.06 | 0012 |[Integer to Roman](src/main/kotlin/g0001_0100/s0012_integer_to_roman)| Medium | String, Hash_Table, Math | 300 | 72.89 -| 0011 |[Container With Most Water](src/main/kotlin/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 | 635 | 64.22 -| 0010 |[Regular Expression Matching](src/main/kotlin/g0001_0100/s0010_regular_expression_matching)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion | 234 | 85.18 -| 0009 |[Palindrome Number](src/main/kotlin/g0001_0100/s0009_palindrome_number)| Easy | Math | 208 | 94.88 -| 0008 |[String to Integer (atoi)](src/main/kotlin/g0001_0100/s0008_string_to_integer_atoi)| Medium | Top_Interview_Questions, String | 152 | 99.64 -| 0007 |[Reverse Integer](src/main/kotlin/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math | 230 | 57.36 -| 0006 |[Zigzag Conversion](src/main/kotlin/g0001_0100/s0006_zigzag_conversion)| Medium | String | 351 | 78.99 -| 0005 |[Longest Palindromic Substring](src/main/kotlin/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 | 359 | 69.08 -| 0004 |[Median of Two Sorted Arrays](src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Divide_and_Conquer | 276 | 91.12 -| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/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 | 212 | 91.24 -| 0002 |[Add Two Numbers](src/main/kotlin/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 | 212 | 93.71 +| 0011 |[Container With Most Water](src/main/kotlin/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 | 474 | 89.18 +| 0010 |[Regular Expression Matching](src/main/kotlin/g0001_0100/s0010_regular_expression_matching)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion, Udemy_Dynamic_Programming | 292 | 58.58 +| 0009 |[Palindrome Number](src/main/kotlin/g0001_0100/s0009_palindrome_number)| Easy | Math, Udemy_Integers | 238 | 96.24 +| 0008 |[String to Integer (atoi)](src/main/kotlin/g0001_0100/s0008_string_to_integer_atoi)| Medium | Top_Interview_Questions, String | 196 | 96.46 +| 0007 |[Reverse Integer](src/main/kotlin/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math, Udemy_Integers | 245 | 60.32 +| 0006 |[Zigzag Conversion](src/main/kotlin/g0001_0100/s0006_zigzag_conversion)| Medium | String | 366 | 80.62 +| 0005 |[Longest Palindromic Substring](src/main/kotlin/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 | 323 | 75.48 +| 0004 |[Median of Two Sorted Arrays](src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Divide_and_Conquer | 355 | 83.48 +| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/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 | 258 | 91.09 +| 0002 |[Add Two Numbers](src/main/kotlin/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 | 417 | 50.44 | 0001 |[Two Sum](src/main/kotlin/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 | 234 | 92.75 diff --git a/src/main/kotlin/g0001_0100/s0002_add_two_numbers/readme.md b/src/main/kotlin/g0001_0100/s0002_add_two_numbers/readme.md index 0e10de72..d5157dbb 100644 --- a/src/main/kotlin/g0001_0100/s0002_add_two_numbers/readme.md +++ b/src/main/kotlin/g0001_0100/s0002_add_two_numbers/readme.md @@ -43,11 +43,12 @@ You may assume the two numbers do not contain any leading zero, except the numbe import com_github_leetcode.ListNode /* + * Example: + * var li = ListNode(5) + * var v = li.`val` * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode(int x) { val = x; } + * class ListNode(var `val`: Int) { + * var next: ListNode? = null * } */ class Solution { diff --git a/src/main/kotlin/g0001_0100/s0050_powx_n/readme.md b/src/main/kotlin/g0001_0100/s0050_powx_n/readme.md index b95d3947..084c831e 100644 --- a/src/main/kotlin/g0001_0100/s0050_powx_n/readme.md +++ b/src/main/kotlin/g0001_0100/s0050_powx_n/readme.md @@ -37,6 +37,7 @@ Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which c ## Solution ```kotlin +@Suppress("NAME_SHADOWING") class Solution { fun myPow(x: Double, n: Int): Double { var x = x diff --git a/src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii/readme.md b/src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii/readme.md new file mode 100644 index 00000000..8a1e4200 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii/readme.md @@ -0,0 +1,64 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 59\. Spiral Matrix II + +Medium + +Given a positive integer `n`, generate an `n x n` `matrix` filled with elements from `1` to n2 in spiral order. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg) + +**Input:** n = 3 + +**Output:** [[1,2,3],[8,9,4],[7,6,5]] + +**Example 2:** + +**Input:** n = 1 + +**Output:** [[1]] + +**Constraints:** + +* `1 <= n <= 20` + +## Solution + +```kotlin +class Solution { + fun generateMatrix(n: Int): Array { + var num = 1 + var rStart = 0 + var rEnd = n - 1 + var cStart = 0 + var cEnd = n - 1 + val spiral = Array(n) { IntArray(n) } + while (rStart <= rEnd && cStart <= cEnd) { + for (k in cStart..cEnd) { + spiral[rStart][k] = num++ + } + rStart++ + for (k in rStart..rEnd) { + spiral[k][cEnd] = num++ + } + cEnd-- + if (rStart <= rEnd) { + for (k in cEnd downTo cStart) { + spiral[rEnd][k] = num++ + } + } + rEnd-- + if (cStart <= cEnd) { + for (k in rEnd downTo rStart) { + spiral[k][cStart] = num++ + } + } + cStart++ + } + return spiral + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0060_permutation_sequence/readme.md b/src/main/kotlin/g0001_0100/s0060_permutation_sequence/readme.md new file mode 100644 index 00000000..feaea3cc --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0060_permutation_sequence/readme.md @@ -0,0 +1,77 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 60\. Permutation Sequence + +Hard + +The set `[1, 2, 3, ..., n]` contains a total of `n!` unique permutations. + +By listing and labeling all of the permutations in order, we get the following sequence for `n = 3`: + +1. `"123"` +2. `"132"` +3. `"213"` +4. `"231"` +5. `"312"` +6. `"321"` + +Given `n` and `k`, return the kth permutation sequence. + +**Example 1:** + +**Input:** n = 3, k = 3 + +**Output:** "213" + +**Example 2:** + +**Input:** n = 4, k = 9 + +**Output:** "2314" + +**Example 3:** + +**Input:** n = 3, k = 1 + +**Output:** "123" + +**Constraints:** + +* `1 <= n <= 9` +* `1 <= k <= n!` + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun getPermutation(n: Int, k: Int): String { + var k = k + val res = CharArray(n) + // We want the permutation sequence to be zero-indexed + k = k - 1 + // The set bits indicate the available digits + var a = (1 shl n) - 1 + var m = 1 + for (i in 2 until n) { + // m = (n - 1)! + m *= i + } + for (i in 0 until n) { + var b = a + for (j in 0 until k / m) { + b = b and b - 1 + } + // b is the bit corresponding to the digit we want + b = b and b.inv() + 1 + res[i] = ('1'.code + Integer.bitCount(b - 1)).toChar() + // Remove b from the set of available digits + a = a and b.inv() + k %= m + m /= Math.max(1, n - i - 1) + } + return String(res) + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0061_rotate_list/readme.md b/src/main/kotlin/g0001_0100/s0061_rotate_list/readme.md new file mode 100644 index 00000000..625f1d3f --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0061_rotate_list/readme.md @@ -0,0 +1,80 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 61\. Rotate List + +Medium + +Given the `head` of a linked list, rotate the list to the right by `k` places. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg) + +**Input:** head = [1,2,3,4,5], k = 2 + +**Output:** [4,5,1,2,3] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg) + +**Input:** head = [0,1,2], k = 4 + +**Output:** [2,0,1] + +**Constraints:** + +* The number of nodes in the list is in the range `[0, 500]`. +* `-100 <= Node.val <= 100` +* 0 <= k <= 2 * 109 + +## Solution + +```kotlin +import com_github_leetcode.ListNode + +/** + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + fun rotateRight(head: ListNode?, k: Int): ListNode? { + if (head == null || k == 0) { + return head + } + var tail = head + // find the count and let tail points to last node + var count = 1 + while (tail != null && tail.next != null) { + count++ + tail = tail.next + } + // calculate number of times to rotate by count modulas + val times = k % count + if (times == 0) { + return head + } + var temp = head + // iterate and go to the K+1 th node from the end or count - K - 1 node from + // start + var i = 1 + while (i <= count - times - 1 && temp != null) { + temp = temp.next + i++ + } + var newHead: ListNode? = null + if (temp != null && tail != null) { + newHead = temp.next + temp.next = null + tail.next = head + } + return newHead + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0063_unique_paths_ii/readme.md b/src/main/kotlin/g0001_0100/s0063_unique_paths_ii/readme.md new file mode 100644 index 00000000..c3d5e947 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0063_unique_paths_ii/readme.md @@ -0,0 +1,85 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 63\. Unique Paths II + +Medium + +You are given an `m x n` integer array `grid`. There is a robot initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m-1][n-1]`). The robot can only move either down or right at any point in time. + +An obstacle and space are marked as `1` or `0` respectively in `grid`. A path that the robot takes cannot include **any** square that is an obstacle. + +Return _the number of possible unique paths that the robot can take to reach the bottom-right corner_. + +The testcases are generated so that the answer will be less than or equal to 2 * 109. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg) + +**Input:** obstacleGrid = \[\[0,0,0],[0,1,0],[0,0,0]] + +**Output:** 2 + +**Explanation:** There is one obstacle in the middle of the 3x3 grid above. + +There are two ways to reach the bottom-right corner: + +1. Right -> Right -> Down -> Down + +2. Down -> Down -> Right -> Right + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg) + +**Input:** obstacleGrid = \[\[0,1],[0,0]] + +**Output:** 1 + +**Constraints:** + +* `m == obstacleGrid.length` +* `n == obstacleGrid[i].length` +* `1 <= m, n <= 100` +* `obstacleGrid[i][j]` is `0` or `1`. + +## Solution + +```kotlin +class Solution { + fun uniquePathsWithObstacles(obstacleGrid: Array): Int { + // if start point has obstacle, there's no path + if (obstacleGrid[0][0] == 1) { + return 0 + } + obstacleGrid[0][0] = 1 + val m = obstacleGrid.size + val n: Int = obstacleGrid[0].size + for (i in 1 until m) { + if (obstacleGrid[i][0] == 1) { + obstacleGrid[i][0] = 0 + } else { + obstacleGrid[i][0] = obstacleGrid[i - 1][0] + } + } + for (j in 1 until n) { + if (obstacleGrid[0][j] == 1) { + obstacleGrid[0][j] = 0 + } else { + obstacleGrid[0][j] = obstacleGrid[0][j - 1] + } + } + for (i in 1 until m) { + for (j in 1 until n) { + if (obstacleGrid[i][j] == 1) { + obstacleGrid[i][j] = 0 + } else { + obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1] + } + } + } + return obstacleGrid[m - 1][n - 1] + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0065_valid_number/readme.md b/src/main/kotlin/g0001_0100/s0065_valid_number/readme.md new file mode 100644 index 00000000..fbbb45c0 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0065_valid_number/readme.md @@ -0,0 +1,91 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 65\. Valid Number + +Hard + +A **valid number** can be split up into these components (in order): + +1. A **decimal number** or an **integer**. +2. (Optional) An `'e'` or `'E'`, followed by an **integer**. + +A **decimal number** can be split up into these components (in order): + +1. (Optional) A sign character (either `'+'` or `'-'`). +2. One of the following formats: + 1. One or more digits, followed by a dot `'.'`. + 2. One or more digits, followed by a dot `'.'`, followed by one or more digits. + 3. A dot `'.'`, followed by one or more digits. + +An **integer** can be split up into these components (in order): + +1. (Optional) A sign character (either `'+'` or `'-'`). +2. One or more digits. + +For example, all the following are valid numbers: `["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]`, while the following are not valid numbers: `["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]`. + +Given a string `s`, return `true` _if_ `s` _is a **valid number**_. + +**Example 1:** + +**Input:** s = "0" + +**Output:** true + +**Example 2:** + +**Input:** s = "e" + +**Output:** false + +**Example 3:** + +**Input:** s = "." + +**Output:** false + +**Constraints:** + +* `1 <= s.length <= 20` +* `s` consists of only English letters (both uppercase and lowercase), digits (`0-9`), plus `'+'`, minus `'-'`, or dot `'.'`. + +## Solution + +```kotlin +class Solution { + fun isNumber(s: String): Boolean { + if (s.isEmpty()) { + return false + } + var eSeen = false + var numberSeen = false + var decimalSeen = false + for (i in 0 until s.length) { + val c = s[i] + if (c.code >= 48 && c.code <= 57) { + numberSeen = true + } else if (c == '+' || c == '-') { + if (i == s.length - 1 || + i != 0 && s[i - 1] != 'e' && s[i - 1] != 'E' + ) { + return false + } + } else if (c == '.') { + if (eSeen || decimalSeen) { + return false + } + decimalSeen = true + } else if (c == 'e' || c == 'E') { + if (i == s.length - 1 || eSeen || !numberSeen) { + return false + } + eSeen = true + } else { + return false + } + } + return numberSeen + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0066_plus_one/readme.md b/src/main/kotlin/g0001_0100/s0066_plus_one/readme.md new file mode 100644 index 00000000..8629b9ba --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0066_plus_one/readme.md @@ -0,0 +1,86 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 66\. Plus One + +Easy + +You are given a **large integer** represented as an integer array `digits`, where each `digits[i]` is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading `0`'s. + +Increment the large integer by one and return _the resulting array of digits_. + +**Example 1:** + +**Input:** digits = [1,2,3] + +**Output:** [1,2,4] + +**Explanation:** + +The array represents the integer 123. + +Incrementing by one gives 123 + 1 = 124. + +Thus, the result should be [1,2,4]. + +**Example 2:** + +**Input:** digits = [4,3,2,1] + +**Output:** [4,3,2,2] + +**Explanation:** + +The array represents the integer 4321. + +Incrementing by one gives 4321 + 1 = 4322. + +Thus, the result should be [4,3,2,2]. + +**Example 3:** + +**Input:** digits = [9] + +**Output:** [1,0] + +**Explanation:** + +The array represents the integer 9. + +Incrementing by one gives 9 + 1 = 10. + +Thus, the result should be [1,0]. + +**Constraints:** + +* `1 <= digits.length <= 100` +* `0 <= digits[i] <= 9` +* `digits` does not contain any leading `0`'s. + +## Solution + +```kotlin +class Solution { + fun plusOne(digits: IntArray): IntArray { + val num = 1 + var carry = 0 + var sum: Int + for (i in digits.indices.reversed()) { + sum = if (i == digits.size - 1) { + digits[i] + carry + num + } else { + digits[i] + carry + } + carry = sum / 10 + digits[i] = sum % 10 + } + if (carry != 0) { + val ans = IntArray(digits.size + 1) + ans[0] = carry + System.arraycopy(digits, 0, ans, 1, ans.size - 1) + return ans + } + return digits + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0067_add_binary/readme.md b/src/main/kotlin/g0001_0100/s0067_add_binary/readme.md new file mode 100644 index 00000000..e3b5fc92 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0067_add_binary/readme.md @@ -0,0 +1,56 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 67\. Add Binary + +Easy + +Given two binary strings `a` and `b`, return _their sum as a binary string_. + +**Example 1:** + +**Input:** a = "11", b = "1" + +**Output:** "100" + +**Example 2:** + +**Input:** a = "1010", b = "1011" + +**Output:** "10101" + +**Constraints:** + +* 1 <= a.length, b.length <= 104 +* `a` and `b` consist only of `'0'` or `'1'` characters. +* Each string does not contain leading zeros except for the zero itself. + +## Solution + +```kotlin +class Solution { + fun addBinary(a: String, b: String): String { + val aArray = a.toCharArray() + val bArray = b.toCharArray() + val sb = StringBuilder() + var i = aArray.size - 1 + var j = bArray.size - 1 + var carry = 0 + while (i >= 0 || j >= 0) { + val sum = (if (i >= 0) aArray[i] - '0' else 0) + (if (j >= 0) bArray[j] - '0' else 0) + carry + sb.append(sum % 2) + carry = sum / 2 + if (i >= 0) { + i-- + } + if (j >= 0) { + j-- + } + } + if (carry != 0) { + sb.append(carry) + } + return sb.reverse().toString() + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0068_text_justification/readme.md b/src/main/kotlin/g0001_0100/s0068_text_justification/readme.md new file mode 100644 index 00000000..11ac92e9 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0068_text_justification/readme.md @@ -0,0 +1,128 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 68\. Text Justification + +Hard + +Given an array of strings `words` and a width `maxWidth`, format the text such that each line has exactly `maxWidth` characters and is fully (left and right) justified. + +You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces `' '` when necessary so that each line has exactly `maxWidth` characters. + +Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. + +For the last line of text, it should be left-justified and no extra space is inserted between words. + +**Note:** + +* A word is defined as a character sequence consisting of non-space characters only. +* Each word's length is guaranteed to be greater than 0 and not exceed maxWidth. +* The input array `words` contains at least one word. + +**Example 1:** + +**Input:** words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16 + +**Output:** [ "This is an", "example of text", "justification. " ] + +**Example 2:** + +**Input:** words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16 + +**Output:** [ "What must be", "acknowledgment ", "shall be " ] + +**Explanation:** Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified. Note that the second line is also left-justified becase it contains only one word. + +**Example 3:** + +**Input:** words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20 + +**Output:** [ "Science is what we", "understand well", "enough to explain to", "a computer. Art is", "everything else we", "do " ] + +**Constraints:** + +* `1 <= words.length <= 300` +* `1 <= words[i].length <= 20` +* `words[i]` consists of only English letters and symbols. +* `1 <= maxWidth <= 100` +* `words[i].length <= maxWidth` + +## Solution + +```kotlin +class Solution { + fun fullJustify(words: Array, maxWidth: Int): List { + // Trying to gauge the number of lines so the ArrayList doesn't need to resize + val output: MutableList = ArrayList((words.size + 1) / (1 + maxWidth / 7)) + // Setting StringBuilder capacity also + var sb = StringBuilder(maxWidth) + var lineTotal = 0 + var numWordsOnLine = 0 + var startWord = 0 + // looping until the 2nd last word, since we're checking words[i + 1] for + // overflows + for (i in 0 until words.size - 1) { + lineTotal += words[i].length + // tracking line length + #words + numWordsOnLine++ + // checking if the next word causes an overflow + if (lineTotal + numWordsOnLine + words[i + 1].length > maxWidth) { + // if only one word fits on the line... + if (numWordsOnLine == 1) { + // append word + sb.append(words[i]) + // pad right with spaces + while (lineTotal++ < maxWidth) { + sb.append(' ') + } + } else { + // # of extra spaces + var extraSp = (maxWidth - lineTotal) % (numWordsOnLine - 1) + + // Creating the line + for (j in startWord until startWord + numWordsOnLine - 1) { + // appending the word + sb.append(words[j]) + if (extraSp-- > 0) { + // appending an extra space, if required + sb.append(' ') + } + // appending the rest of the required spaces + for (k in 0 until (maxWidth - lineTotal) / (numWordsOnLine - 1)) { + sb.append(' ') + } + } + // appending the last word of the line + sb.append(words[startWord + numWordsOnLine - 1]) + } + // adding to output list + output.add(sb.toString()) + // reset everything for next line + // keeping track of the first word for next line + startWord = i + 1 + // resetting these to 0 for processing next line + lineTotal = 0 + numWordsOnLine = lineTotal + // need a new StringBuilder for the next line + sb = StringBuilder(maxWidth) + } + } + // handling the final line (no justification, right padded with spaces) + lineTotal = 0 + for (i in startWord until words.size) { + lineTotal += words[i].length + sb.append(words[i]) + if (lineTotal++ < maxWidth) { + sb.append(' ') + } + } + // padding right side with spaces + while (lineTotal++ < maxWidth) { + sb.append(' ') + } + // add the final line to output list + output.add(sb.toString()) + return output + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0069_sqrtx/readme.md b/src/main/kotlin/g0001_0100/s0069_sqrtx/readme.md new file mode 100644 index 00000000..66992a21 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0069_sqrtx/readme.md @@ -0,0 +1,60 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 69\. Sqrt(x) + +Easy + +Given a non-negative integer `x`, compute and return _the square root of_ `x`. + +Since the return type is an integer, the decimal digits are **truncated**, and only **the integer part** of the result is returned. + +**Note:** You are not allowed to use any built-in exponent function or operator, such as `pow(x, 0.5)` or x ** 0.5. + +**Example 1:** + +**Input:** x = 4 + +**Output:** 2 + +**Example 2:** + +**Input:** x = 8 + +**Output:** 2 + +**Explanation:** The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned. + +**Constraints:** + +* 0 <= x <= 231 - 1 + +## Solution + +```kotlin +class Solution { + fun mySqrt(x: Int): Int { + var start = 1 + var end = x / 2 + var sqrt = start + (end - start) / 2 + if (x == 0) { + return 0 + } + while (start <= end) { + if (sqrt == x / sqrt) { + return sqrt + } else if (sqrt > x / sqrt) { + end = sqrt - 1 + } else if (sqrt < x / sqrt) { + start = sqrt + 1 + } + sqrt = start + (end - start) / 2 + } + return if (sqrt > x / sqrt) { + sqrt - 1 + } else { + sqrt + } + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0071_simplify_path/readme.md b/src/main/kotlin/g0001_0100/s0071_simplify_path/readme.md new file mode 100644 index 00000000..643e2143 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0071_simplify_path/readme.md @@ -0,0 +1,87 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 71\. Simplify Path + +Medium + +Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**. + +In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level, and any multiple consecutive slashes (i.e. `'//'`) are treated as a single slash `'/'`. For this problem, any other format of periods such as `'...'` are treated as file/directory names. + +The **canonical path** should have the following format: + +* The path starts with a single slash `'/'`. +* Any two directories are separated by a single slash `'/'`. +* The path does not end with a trailing `'/'`. +* The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period `'.'` or double period `'..'`) + +Return _the simplified **canonical path**_. + +**Example 1:** + +**Input:** path = "/home/" + +**Output:** "/home" + +**Explanation:** Note that there is no trailing slash after the last directory name. + +**Example 2:** + +**Input:** path = "/../" + +**Output:** "/" + +**Explanation:** Going one level up from the root directory is a no-op, as the root level is the highest level you can go. + +**Example 3:** + +**Input:** path = "/home//foo/" + +**Output:** "/home/foo" + +**Explanation:** In the canonical path, multiple consecutive slashes are replaced by a single one. + +**Constraints:** + +* `1 <= path.length <= 3000` +* `path` consists of English letters, digits, period `'.'`, slash `'/'` or `'_'`. +* `path` is a valid absolute Unix path. + +## Solution + +```kotlin +import java.util.ArrayDeque +import java.util.Deque + +class Solution { + fun simplifyPath(path: String): String { + val stk: Deque = ArrayDeque() + var start = 0 + while (start < path.length) { + while (start < path.length && path[start] == '/') { + start++ + } + var end = start + while (end < path.length && path[end] != '/') { + end++ + } + val s = path.substring(start, end) + if (s == "..") { + if (!stk.isEmpty()) { + stk.pop() + } + } else if (s != "." && s != "") { + stk.push(s) + } + start = end + 1 + } + val ans = StringBuilder() + while (!stk.isEmpty()) { + ans.insert(0, stk.pop()) + ans.insert(0, "/") + } + return if (ans.length > 0) ans.toString() else "/" + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0077_combinations/readme.md b/src/main/kotlin/g0001_0100/s0077_combinations/readme.md new file mode 100644 index 00000000..200f9220 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0077_combinations/readme.md @@ -0,0 +1,65 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 77\. Combinations + +Medium + +Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`. + +You may return the answer in **any order**. + +**Example 1:** + +**Input:** n = 4, k = 2 + +**Output:** [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] + +**Explanation:** There are 4 choose 2 = 6 total combinations. Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination. + +**Example 2:** + +**Input:** n = 1, k = 1 + +**Output:** [[1]] + +**Explanation:** There is 1 choose 1 = 1 total combination. + +**Constraints:** + +* `1 <= n <= 20` +* `1 <= k <= n` + +## Solution + +```kotlin +import java.util.Stack + +class Solution { + fun combine(n: Int, k: Int): List> { + val ans: MutableList> = ArrayList() + // Boundary case + if (n > 20 || k < 1 || k > n) { + return ans + } + backtrack(ans, n, k, 1, Stack()) + return ans + } + + private fun backtrack(ans: MutableList>, n: Int, k: Int, s: Int, stack: Stack) { + // Base case + // If k becomes 0 + if (k == 0) { + ans.add(ArrayList(stack)) + return + } + // Start with s till n-k+1 + for (i in s..n - k + 1) { + stack.push(i) + // Update start for recursion and decrease k by 1 + backtrack(ans, n, k - 1, i + 1, stack) + stack.pop() + } + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/readme.md b/src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/readme.md new file mode 100644 index 00000000..7eea82d9 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/readme.md @@ -0,0 +1,73 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 80\. Remove Duplicates from Sorted Array II + +Medium + +Given an integer array `nums` sorted in **non-decreasing order**, remove some duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears **at most twice**. The **relative order** of the elements should be kept the **same**. + +Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements. + +Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`. + +Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory. + +**Custom Judge:** + +The judge will test your solution with the following code: + +int[] nums = [...]; // Input array int[] expectedNums = [...]; // The expected answer with correct length int k = removeDuplicates(nums); // Calls your implementation assert k == expectedNums.length; for (int i = 0; i < k; i++) { assert nums[i] == expectedNums[i]; } + +If all assertions pass, then your solution will be **accepted**. + +**Example 1:** + +**Input:** nums = [1,1,1,2,2,3] + +**Output:** 5, nums = [1,1,2,2,3,\_] + +**Explanation:** Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). + +**Example 2:** + +**Input:** nums = [0,0,1,1,1,1,2,3,3] + +**Output:** 7, nums = [0,0,1,1,2,3,3,\_,\_] + +**Explanation:** Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). + +**Constraints:** + +* 1 <= nums.length <= 3 * 104 +* -104 <= nums[i] <= 104 +* `nums` is sorted in **non-decreasing** order. + +## Solution + +```kotlin +class Solution { + fun removeDuplicates(nums: IntArray): Int { + var i = 0 + var k = 0 + var count = 0 + while (i < nums.size - 1) { + ++count + if (count <= 2) { + nums[k++] = nums[i] + } + if (nums[i] != nums[i + 1]) { + count = 0 + i++ + continue + } + i++ + } + ++count + if (count <= 2) { + nums[k++] = nums[i] + } + return k + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii/readme.md b/src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii/readme.md new file mode 100644 index 00000000..6386730d --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii/readme.md @@ -0,0 +1,58 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 81\. Search in Rotated Sorted Array II + +Medium + +There is an integer array `nums` sorted in non-decreasing order (not necessarily with **distinct** values). + +Before being passed to your function, `nums` is **rotated** at an unknown pivot index `k` (`0 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,4,4,5,6,6,7]` might be rotated at pivot index `5` and become `[4,5,6,6,7,0,1,2,4,4]`. + +Given the array `nums` **after** the rotation and an integer `target`, return `true` _if_ `target` _is in_ `nums`_, or_ `false` _if it is not in_ `nums`_._ + +You must decrease the overall operation steps as much as possible. + +**Example 1:** + +**Input:** nums = [2,5,6,0,0,1,2], target = 0 + +**Output:** true + +**Example 2:** + +**Input:** nums = [2,5,6,0,0,1,2], target = 3 + +**Output:** false + +**Constraints:** + +* `1 <= nums.length <= 5000` +* -104 <= nums[i] <= 104 +* `nums` is guaranteed to be rotated at some pivot. +* -104 <= target <= 104 + +**Follow up:** This problem is similar to [Search in Rotated Sorted Array](/problems/search-in-rotated-sorted-array/description/), but `nums` may contain **duplicates**. Would this affect the runtime complexity? How and why? + +## Solution + +```kotlin +class Solution { + fun search(nums: IntArray, target: Int): Boolean { + return binary(nums, 0, nums.size - 1, target) + } + + private fun binary(a: IntArray, i: Int, j: Int, t: Int): Boolean { + if (i > j) { + return false + } + val mid = (i + j) / 2 + if (a[mid] == t) { + return true + } + val c1 = binary(a, i, mid - 1, t) + val c2 = binary(a, mid + 1, j, t) + return c1 || c2 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/readme.md b/src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/readme.md new file mode 100644 index 00000000..3f90cf15 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/readme.md @@ -0,0 +1,75 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 82\. Remove Duplicates from Sorted List II + +Medium + +Given the `head` of a sorted linked list, _delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list_. Return _the linked list **sorted** as well_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg) + +**Input:** head = [1,2,3,3,4,4,5] + +**Output:** [1,2,5] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg) + +**Input:** head = [1,1,1,2,3] + +**Output:** [2,3] + +**Constraints:** + +* The number of nodes in the list is in the range `[0, 300]`. +* `-100 <= Node.val <= 100` +* The list is guaranteed to be **sorted** in ascending order. + +## Solution + +```kotlin +import com_github_leetcode.ListNode + +/* + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + fun deleteDuplicates(head: ListNode?): ListNode? { + if (head == null || head.next == null) { + return head + } + val dummy = ListNode(0) + var prev: ListNode? = dummy + prev!!.next = head + var curr = head.next + while (curr != null) { + var flagFoundDuplicate = false + while (curr != null && prev!!.next!!.`val` == curr.`val`) { + flagFoundDuplicate = true + curr = curr.next + } + if (flagFoundDuplicate) { + prev!!.next = curr + if (curr != null) { + curr = curr.next + } + } else { + prev = prev!!.next + prev!!.next = curr + curr = curr!!.next + } + } + return dummy.next + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list/readme.md b/src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list/readme.md new file mode 100644 index 00000000..a814e3f2 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list/readme.md @@ -0,0 +1,64 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 83\. Remove Duplicates from Sorted List + +Easy + +Given the `head` of a sorted linked list, _delete all duplicates such that each element appears only once_. Return _the linked list **sorted** as well_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/04/list1.jpg) + +**Input:** head = [1,1,2] + +**Output:** [1,2] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/01/04/list2.jpg) + +**Input:** head = [1,1,2,3,3] + +**Output:** [1,2,3] + +**Constraints:** + +* The number of nodes in the list is in the range `[0, 300]`. +* `-100 <= Node.val <= 100` +* The list is guaranteed to be **sorted** in ascending order. + +## Solution + +```kotlin +import com_github_leetcode.ListNode + +/* + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + fun deleteDuplicates(head: ListNode?): ListNode? { + if (head == null) { + return null + } + var current: ListNode = head + var next = current.next + while (null != next) { + if (current.`val` == next.`val`) { + current.next = next.next + } else { + current = next + } + next = current.next + } + return head + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0085_maximal_rectangle/readme.md b/src/main/kotlin/g0001_0100/s0085_maximal_rectangle/readme.md new file mode 100644 index 00000000..659097ac --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0085_maximal_rectangle/readme.md @@ -0,0 +1,125 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 85\. Maximal Rectangle + +Hard + +Given a `rows x cols` binary `matrix` filled with `0`'s and `1`'s, find the largest rectangle containing only `1`'s and return _its area_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg) + +**Input:** matrix = \[\["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] + +**Output:** 6 + +**Explanation:** The maximal rectangle is shown in the above picture. + +**Example 2:** + +**Input:** matrix = \[\["0"]] + +**Output:** 0 + +**Example 3:** + +**Input:** matrix = \[\["1"]] + +**Output:** 1 + +**Constraints:** + +* `rows == matrix.length` +* `cols == matrix[i].length` +* `1 <= row, cols <= 200` +* `matrix[i][j]` is `'0'` or `'1'`. + +## Solution + +```kotlin +class Solution { + fun maximalRectangle(matrix: Array): Int { + /* + * idea: using [LC84 Largest Rectangle in Histogram]. For each row of the matrix, construct + * the histogram based on the current row and the previous histogram (up to the previous + * row), then compute the largest rectangle area using LC84. + */ + val m = matrix.size + var n = 0 + if (m == 0 || matrix[0].size.also { n = it } == 0) { + return 0 + } + var i: Int + var j: Int + var res = 0 + val heights = IntArray(n) + i = 0 + while (i < m) { + j = 0 + while (j < n) { + if (matrix[i][j] == '0') { + heights[j] = 0 + } else { + heights[j] += 1 + } + j++ + } + res = Math.max(res, largestRectangleArea(heights)) + i++ + } + return res + } + + private fun largestRectangleArea(heights: IntArray): Int { + /* + * idea: scan and store if a[i-1]<=a[i] (increasing), then as long as a[i]a[i], which + * is a[j]*(i-j). And meanwhile, all these bars (a[j]'s) are already done, and thus are + * throwable (using pop() with a stack). + * + *

We can use an array nLeftGeq[] of size n to simulate a stack. nLeftGeq[i] = the number + * of elements to the left of [i] having value greater than or equal to a[i] (including a[i] + * itself). It is also the index difference between [i] and the next index on the top of the + * stack. + */ + val n = heights.size + if (n == 0) { + return 0 + } + val nLeftGeq = IntArray(n) + // the number of elements to the left + // of [i] with value >= heights[i] + nLeftGeq[0] = 1 + // preIdx=the index of stack.peek(), res=max area so far + var preIdx = 0 + var res = 0 + for (i in 1 until n) { + nLeftGeq[i] = 1 + // notice that preIdx = i - 1 = peek() + while (preIdx >= 0 && heights[i] < heights[preIdx]) { + res = Math.max(res, heights[preIdx] * (nLeftGeq[preIdx] + i - preIdx - 1)) + // pop() + nLeftGeq[i] += nLeftGeq[preIdx] + // peek() current top + preIdx = preIdx - nLeftGeq[preIdx] + } + if (preIdx >= 0 && heights[i] == heights[preIdx]) { + // pop() + nLeftGeq[i] += nLeftGeq[preIdx] + } + // otherwise nothing to do + preIdx = i + } + // compute the rest largest rectangle areas with (indices of) bases + // on stack + while (preIdx >= 0 && 0 < heights[preIdx]) { + res = Math.max(res, heights[preIdx] * (nLeftGeq[preIdx] + n - preIdx - 1)) + // peek() current top + preIdx = preIdx - nLeftGeq[preIdx] + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0086_partition_list/readme.md b/src/main/kotlin/g0001_0100/s0086_partition_list/readme.md new file mode 100644 index 00000000..534e8e1a --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0086_partition_list/readme.md @@ -0,0 +1,70 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 86\. Partition List + +Medium + +Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`. + +You should **preserve** the original relative order of the nodes in each of the two partitions. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/04/partition.jpg) + +**Input:** head = [1,4,3,2,5,2], x = 3 + +**Output:** [1,2,2,4,3,5] + +**Example 2:** + +**Input:** head = [2,1], x = 2 + +**Output:** [1,2] + +**Constraints:** + +* The number of nodes in the list is in the range `[0, 200]`. +* `-100 <= Node.val <= 100` +* `-200 <= x <= 200` + +## Solution + +```kotlin +import com_github_leetcode.ListNode + +/* + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +@Suppress("NAME_SHADOWING") +class Solution { + fun partition(head: ListNode?, x: Int): ListNode? { + var head = head + var nHead: ListNode? = ListNode(0) + var nTail: ListNode? = ListNode(0) + val ptr = nTail + val temp = nHead + while (head != null) { + val nNext = head.next + if (head.`val` < x) { + nHead!!.next = head + nHead = nHead.next + } else { + nTail!!.next = head + nTail = nTail.next + } + head = nNext + } + nTail!!.next = null + nHead!!.next = ptr!!.next + return temp!!.next + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0087_scramble_string/readme.md b/src/main/kotlin/g0001_0100/s0087_scramble_string/readme.md new file mode 100644 index 00000000..707a97f7 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0087_scramble_string/readme.md @@ -0,0 +1,60 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 86\. Partition List + +Medium + +Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`. + +You should **preserve** the original relative order of the nodes in each of the two partitions. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/04/partition.jpg) + +**Input:** head = [1,4,3,2,5,2], x = 3 + +**Output:** [1,2,2,4,3,5] + +**Example 2:** + +**Input:** head = [2,1], x = 2 + +**Output:** [1,2] + +**Constraints:** + +* The number of nodes in the list is in the range `[0, 200]`. +* `-100 <= Node.val <= 100` +* `-200 <= x <= 200` + +## Solution + +```kotlin +class Solution { + fun isScramble(s1: String, s2: String): Boolean { + val n = s1.length + val dp = Array(n) { Array(n) { BooleanArray(n + 1) } } + for (len in 1..n) { + for (i in 0..n - len) { + for (j in 0..n - len) { + if (len == 1) { + dp[i][j][len] = s1[i] == s2[j] + } else { + var k = 1 + while (k < len && !dp[i][j][len]) { + dp[i][j][len] = ( + dp[i][j][k] && dp[i + k][j + k][len - k] || + dp[i][j + len - k][k] && dp[i + k][j][len - k] + ) + k++ + } + } + } + } + } + return dp[0][0][n] + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0088_merge_sorted_array/readme.md b/src/main/kotlin/g0001_0100/s0088_merge_sorted_array/readme.md new file mode 100644 index 00000000..457b3e98 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0088_merge_sorted_array/readme.md @@ -0,0 +1,65 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 88\. Merge Sorted Array + +Easy + +You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively. + +**Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**. + +The final sorted array should not be returned by the function, but instead be _stored inside the array_ `nums1`. To accommodate this, `nums1` has a length of `m + n`, where the first `m` elements denote the elements that should be merged, and the last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`. + +**Example 1:** + +**Input:** nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 + +**Output:** [1,2,2,3,5,6] + +**Explanation:** The arrays we are merging are [1,2,3] and [2,5,6]. The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1. + +**Example 2:** + +**Input:** nums1 = [1], m = 1, nums2 = [], n = 0 + +**Output:** [1] + +**Explanation:** The arrays we are merging are [1] and []. The result of the merge is [1]. + +**Example 3:** + +**Input:** nums1 = [0], m = 0, nums2 = [1], n = 1 + +**Output:** [1] + +**Explanation:** The arrays we are merging are [] and [1]. The result of the merge is [1]. Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1. + +**Constraints:** + +* `nums1.length == m + n` +* `nums2.length == n` +* `0 <= m, n <= 200` +* `1 <= m + n <= 200` +* -109 <= nums1[i], nums2[j] <= 109 + +**Follow up:** Can you come up with an algorithm that runs in `O(m + n)` time? + +## Solution + +```kotlin +class Solution { + fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int) { + var i = m - 1 + var j = nums1.size - 1 + var p2 = n - 1 + while (p2 >= 0) { + if (i >= 0 && nums1[i] > nums2[p2]) { + nums1[j--] = nums1[i--] + } else { + nums1[j--] = nums2[p2--] + } + } + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0089_gray_code/readme.md b/src/main/kotlin/g0001_0100/s0089_gray_code/readme.md new file mode 100644 index 00000000..09b82c9d --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0089_gray_code/readme.md @@ -0,0 +1,61 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 89\. Gray Code + +Medium + +An **n-bit gray code sequence** is a sequence of 2n integers where: + +* Every integer is in the **inclusive** range [0, 2n - 1], +* The first integer is `0`, +* An integer appears **no more than once** in the sequence, +* The binary representation of every pair of **adjacent** integers differs by **exactly one bit**, and +* The binary representation of the **first** and **last** integers differs by **exactly one bit**. + +Given an integer `n`, return _any valid **n-bit gray code sequence**_. + +**Example 1:** + +**Input:** n = 2 + +**Output:** [0,1,3,2] + +**Explanation:** The binary representation of [0,1,3,2] is [00,01,11,10]. - 00 and 01 differ by one bit - 01 and 11 differ by one bit - 11 and 10 differ by one bit - 10 and 00 differ by one bit [0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01]. - 00 and 10 differ by one bit - 10 and 11 differ by one bit - 11 and 01 differ by one bit - 01 and 00 differ by one bit + +**Example 2:** + +**Input:** n = 1 + +**Output:** [0,1] + +**Constraints:** + +* `1 <= n <= 16` + +## Solution + +```kotlin +@Suppress("NAME_SHADOWING") +class Solution { + fun grayCode(n: Int): List { + var n = n + var n1 = arrayOf(0) + var shift = 1 + while (n > 0) { + val temp = arrayOfNulls(n1.size * 2) + var pos = 0 + for (integer in n1) { + temp[pos++] = integer + } + for (i in n1.indices.reversed()) { + temp[pos++] = n1[i]!! or shift + } + n1 = temp + shift = shift shl 1 + n-- + } + return listOf(*n1) + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0090_subsets_ii/readme.md b/src/main/kotlin/g0001_0100/s0090_subsets_ii/readme.md new file mode 100644 index 00000000..7de19bde --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0090_subsets_ii/readme.md @@ -0,0 +1,61 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 90\. Subsets II + +Medium + +Given an integer array `nums` that may contain duplicates, return _all possible subsets (the power set)_. + +The solution set **must not** contain duplicate subsets. Return the solution in **any order**. + +**Example 1:** + +**Input:** nums = [1,2,2] + +**Output:** [[],[1],[1,2],[1,2,2],[2],[2,2]] + +**Example 2:** + +**Input:** nums = [0] + +**Output:** [[],[0]] + +**Constraints:** + +* `1 <= nums.length <= 10` +* `-10 <= nums[i] <= 10` + +## Solution + +```kotlin +import java.util.Arrays + +class Solution { + var allComb: MutableList> = ArrayList() + var comb: MutableList = ArrayList() + lateinit var nums: IntArray + fun subsetsWithDup(nums: IntArray): List> { + Arrays.sort(nums) + this.nums = nums + dfs(0) + allComb.add(ArrayList()) + return allComb + } + + private fun dfs(start: Int) { + if (start > nums.size) { + return + } + for (i in start until nums.size) { + if (i > start && nums[i] == nums[i - 1]) { + continue + } + comb.add(nums[i]) + allComb.add(ArrayList(comb)) + dfs(i + 1) + comb.removeAt(comb.size - 1) + } + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0091_decode_ways/readme.md b/src/main/kotlin/g0001_0100/s0091_decode_ways/readme.md new file mode 100644 index 00000000..8e010655 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0091_decode_ways/readme.md @@ -0,0 +1,81 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 91\. Decode Ways + +Medium + +A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping: + +'A' -> "1" 'B' -> "2" ... 'Z' -> "26" + +To **decode** an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, `"11106"` can be mapped into: + +* `"AAJF"` with the grouping `(1 1 10 6)` +* `"KJF"` with the grouping `(11 10 6)` + +Note that the grouping `(1 11 06)` is invalid because `"06"` cannot be mapped into `'F'` since `"6"` is different from `"06"`. + +Given a string `s` containing only digits, return _the **number** of ways to **decode** it_. + +The test cases are generated so that the answer fits in a **32-bit** integer. + +**Example 1:** + +**Input:** s = "12" + +**Output:** 2 + +**Explanation:** "12" could be decoded as "AB" (1 2) or "L" (12). + +**Example 2:** + +**Input:** s = "226" + +**Output:** 3 + +**Explanation:** "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6). + +**Example 3:** + +**Input:** s = "06" + +**Output:** 0 + +**Explanation:** "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06"). + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` contains only digits and may contain leading zero(s). + +## Solution + +```kotlin +class Solution { + fun numDecodings(s: String): Int { + if (s[0] == '0') { + return 0 + } + val n = s.length + val f = IntArray(n + 1) + // Auxiliary + f[0] = 1 + f[1] = 1 + for (i in 2..n) { + // Calculate the independent number + if (s[i - 1] != '0') { + // As long as the current character is not 0, it means that the previous decoding + // number can be inherited + f[i] = f[i - 1] + } + // Calculate the number of combinations + val twodigits = (s[i - 2] - '0') * 10 + (s[i - 1] - '0') + if (twodigits >= 10 && twodigits <= 26) { + f[i] += f[i - 2] + } + } + return f[n] + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0092_reverse_linked_list_ii/readme.md b/src/main/kotlin/g0001_0100/s0092_reverse_linked_list_ii/readme.md new file mode 100644 index 00000000..27ea38fc --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0092_reverse_linked_list_ii/readme.md @@ -0,0 +1,104 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 92\. Reverse Linked List II + +Medium + +Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg) + +**Input:** head = [1,2,3,4,5], left = 2, right = 4 + +**Output:** [1,4,3,2,5] + +**Example 2:** + +**Input:** head = [5], left = 1, right = 1 + +**Output:** [5] + +**Constraints:** + +* The number of nodes in the list is `n`. +* `1 <= n <= 500` +* `-500 <= Node.val <= 500` +* `1 <= left <= right <= n` + +**Follow up:** Could you do it in one pass? + +## Solution + +```kotlin +import com_github_leetcode.ListNode + +/* + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +@Suppress("NAME_SHADOWING") +class Solution { + fun reverseBetween(head: ListNode?, left: Int, right: Int): ListNode? { + var head = head + var right = right + if (left == right) { + return head + } + var prev: ListNode? = null + var temp = head + val start: ListNode? + var k = left + while (temp != null && k > 1) { + prev = temp + temp = temp.next + k-- + } + if (left > 1 && prev != null) { + prev.next = null + } + var prev1: ListNode? = null + start = temp + while (temp != null && right - left >= 0) { + prev1 = temp + temp = temp.next + right-- + } + if (prev1 != null) { + prev1.next = null + } + if (left > 1 && prev != null) { + prev.next = reverse(start) + } else { + head = reverse(start) + prev = head + } + while (prev!!.next != null) { + prev = prev.next + } + prev.next = temp + return head + } + + fun reverse(head: ListNode?): ListNode? { + var p: ListNode? + var q: ListNode? + var r: ListNode? = null + p = head + while (p != null) { + q = p.next + p.next = r + r = p + p = q + } + return r + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0093_restore_ip_addresses/readme.md b/src/main/kotlin/g0001_0100/s0093_restore_ip_addresses/readme.md new file mode 100644 index 00000000..83128548 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0093_restore_ip_addresses/readme.md @@ -0,0 +1,75 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 93\. Restore IP Addresses + +Medium + +A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros. + +* For example, `"0.1.2.201"` and `"192.168.1.1"` are **valid** IP addresses, but `"0.011.255.245"`, `"192.168.1.312"` and `"192.168@1.1"` are **invalid** IP addresses. + +Given a string `s` containing only digits, return _all possible valid IP addresses that can be formed by inserting dots into_ `s`. You are **not** allowed to reorder or remove any digits in `s`. You may return the valid IP addresses in **any** order. + +**Example 1:** + +**Input:** s = "25525511135" + +**Output:** ["255.255.11.135","255.255.111.35"] + +**Example 2:** + +**Input:** s = "0000" + +**Output:** ["0.0.0.0"] + +**Example 3:** + +**Input:** s = "101023" + +**Output:** ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"] + +**Constraints:** + +* `1 <= s.length <= 20` +* `s` consists of digits only. + +## Solution + +```kotlin +class Solution() { + fun restoreIpAddresses(s: String): List { + val results: MutableList = ArrayList() + step(s, 0, IntArray(4), 0, results) + return results + } + + fun step(s: String, pos: Int, octets: IntArray, count: Int, results: MutableList) { + if (count == 4 && pos == s.length) { + results.add( + octets[0].toString() + '.' + + octets[1] + + '.' + + octets[2] + + '.' + + octets[3] + ) + } else if (count < 4 && pos < 12) { + var octet = 0 + for (i in 0..2) { + if (pos + i < s.length) { + val digit = s[pos + i] - '0' + octet = octet * 10 + digit + if (octet < 256) { + octets[count] = octet + step(s, pos + i + 1, octets, count + 1, results) + } + if (i == 0 && digit == 0) { + break + } + } + } + } + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal/readme.md b/src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal/readme.md index 7910e22e..c672098f 100644 --- a/src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal/readme.md +++ b/src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal/readme.md @@ -37,6 +37,16 @@ The largest rectangle is shown in the red area, which has an area = 10 units. ```kotlin import com_github_leetcode.TreeNode +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ class Solution { fun inorderTraversal(root: TreeNode?): List { if (root == null) { diff --git a/src/main/kotlin/g0001_0100/s0095_unique_binary_search_trees_ii/readme.md b/src/main/kotlin/g0001_0100/s0095_unique_binary_search_trees_ii/readme.md new file mode 100644 index 00000000..5e970bb8 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0095_unique_binary_search_trees_ii/readme.md @@ -0,0 +1,84 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 95\. Unique Binary Search Trees II + +Medium + +Given an integer `n`, return _all the structurally unique **BST'**s (binary search trees), which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`. Return the answer in **any order**. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg) + +**Input:** n = 3 + +**Output:** [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]] + +**Example 2:** + +**Input:** n = 1 + +**Output:** [[1]] + +**Constraints:** + +* `1 <= n <= 8` + +## Solution + +```kotlin +import com_github_leetcode.TreeNode + +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ +class Solution { + fun generateTrees(n: Int): List { + var result: MutableList = ArrayList() + result.add(TreeNode(1)) + for (i in 2..n) { + val nresult: MutableList = ArrayList() + for (r in result) { + var node = TreeNode(i, r, null) + nresult.add(node) + var previous: TreeNode? = r + while (previous != null) { + node = TreeNode(i) + val cr: TreeNode? = copy(r) + insert(cr, node, previous) + previous = node.left + nresult.add(cr) + } + } + result = nresult + } + return result + } + + private fun insert(dest: TreeNode?, n: TreeNode, from: TreeNode) { + if (dest != null && dest.`val` == from.`val`) { + val h: TreeNode? = dest.right + dest.right = n + n.left = h + return + } + if (dest != null) { + insert(dest.right, n, from) + } + } + + private fun copy(n: TreeNode?): TreeNode? { + return if (n == null) { + null + } else { TreeNode(n.`val`, copy(n.left), copy(n.right)) } + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0097_interleaving_string/readme.md b/src/main/kotlin/g0001_0100/s0097_interleaving_string/readme.md new file mode 100644 index 00000000..1b201016 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0097_interleaving_string/readme.md @@ -0,0 +1,89 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork) + +## 97\. Interleaving String + +Medium + +Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`. + +An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` **non-empty** substrings respectively, such that: + +* s = s1 + s2 + ... + sn +* t = t1 + t2 + ... + tm +* `|n - m| <= 1` +* The **interleaving** is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ... + +**Note:** `a + b` is the concatenation of strings `a` and `b`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg) + +**Input:** s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" + +**Output:** true + +**Explanation:** One way to obtain s3 is: Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a". Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac". Since s3 can be obtained by interleaving s1 and s2, we return true. + +**Example 2:** + +**Input:** s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" + +**Output:** false + +**Explanation:** Notice how it is impossible to interleave s2 with any other string to obtain s3. + +**Example 3:** + +**Input:** s1 = "", s2 = "", s3 = "" + +**Output:** true + +**Constraints:** + +* `0 <= s1.length, s2.length <= 100` +* `0 <= s3.length <= 200` +* `s1`, `s2`, and `s3` consist of lowercase English letters. + +**Follow up:** Could you solve it using only `O(s2.length)` additional memory space? + +## Solution + +```kotlin +class Solution { + fun isInterleave(s1: String, s2: String, s3: String): Boolean { + if (s3.length != s1.length + s2.length) { + return false + } + val cache = Array(s1.length + 1) { arrayOfNulls(s2.length + 1) } + return isInterleave(s1, s2, s3, 0, 0, 0, cache) + } + + fun isInterleave( + s1: String, + s2: String, + s3: String, + i1: Int, + i2: Int, + i3: Int, + cache: Array> + ): Boolean { + if (cache[i1][i2] != null) { + return cache[i1][i2]!! + } + if (i1 == s1.length && i2 == s2.length && i3 == s3.length) { + return true + } + var result = false + if (i1 < s1.length && s1[i1] == s3[i3]) { + result = isInterleave(s1, s2, s3, i1 + 1, i2, i3 + 1, cache) + } + if (i2 < s2.length && s2[i2] == s3[i3]) { + result = result || isInterleave(s1, s2, s3, i1, i2 + 1, i3 + 1, cache) + } + cache[i1][i2] = result + return result + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/readme.md b/src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/readme.md index f09a2de6..965aab41 100644 --- a/src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/readme.md +++ b/src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/readme.md @@ -41,6 +41,16 @@ A **valid BST** is defined as follows: ```kotlin import com_github_leetcode.TreeNode +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ class Solution { fun isValidBST(root: TreeNode?): Boolean { return solve(root, Long.MIN_VALUE, Long.MAX_VALUE) diff --git a/src/main/kotlin/g0101_0200/s0101_symmetric_tree/readme.md b/src/main/kotlin/g0101_0200/s0101_symmetric_tree/readme.md index 8e1d831a..632183b7 100644 --- a/src/main/kotlin/g0101_0200/s0101_symmetric_tree/readme.md +++ b/src/main/kotlin/g0101_0200/s0101_symmetric_tree/readme.md @@ -35,6 +35,16 @@ Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e ```kotlin import com_github_leetcode.TreeNode +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ class Solution { fun isSymmetric(root: TreeNode?): Boolean { return if (root == null) { diff --git a/src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal/readme.md b/src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal/readme.md index 8e3e145e..23ae6671 100644 --- a/src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal/readme.md +++ b/src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal/readme.md @@ -40,6 +40,16 @@ import java.util.ArrayList import java.util.LinkedList import java.util.Queue +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ class Solution { fun levelOrder(root: TreeNode?): List> { var localRoot: TreeNode? = root diff --git a/src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md b/src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md index d0f7fd87..9da626b9 100644 --- a/src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md +++ b/src/main/kotlin/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md @@ -37,6 +37,16 @@ Given two integer arrays `preorder` and `inorder` where `preorder` is the preord import com_github_leetcode.TreeNode import java.util.HashMap +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ class Solution { private var j = 0 private val map: MutableMap = HashMap() diff --git a/src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list/readme.md b/src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list/readme.md index 68bd2bb1..d46866d9 100644 --- a/src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list/readme.md +++ b/src/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list/readme.md @@ -42,6 +42,16 @@ Given the `root` of a binary tree, flatten the tree into a "linked list": ```kotlin import com_github_leetcode.TreeNode +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ class Solution { fun flatten(root: TreeNode?) { if (root != null) { diff --git a/src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum/readme.md b/src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum/readme.md index 98fc3dfb..1fa60de6 100644 --- a/src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum/readme.md +++ b/src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum/readme.md @@ -41,6 +41,16 @@ Given the `root` of a binary tree, return _the maximum **path sum** of any **non ```kotlin import com_github_leetcode.TreeNode +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ class Solution { private var max = Int.MIN_VALUE private fun helper(root: TreeNode?): Int { diff --git a/src/main/kotlin/g0201_0300/s0226_invert_binary_tree/readme.md b/src/main/kotlin/g0201_0300/s0226_invert_binary_tree/readme.md index ebd4fa89..4d862ed2 100644 --- a/src/main/kotlin/g0201_0300/s0226_invert_binary_tree/readme.md +++ b/src/main/kotlin/g0201_0300/s0226_invert_binary_tree/readme.md @@ -39,6 +39,16 @@ Given the `root` of a binary tree, invert the tree, and return _its root_. ```kotlin import com_github_leetcode.TreeNode +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ class Solution { fun invertTree(root: TreeNode?): TreeNode? { if (root == null) { diff --git a/src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md b/src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md index 7614728d..8a8d1f03 100644 --- a/src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md +++ b/src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md @@ -48,6 +48,16 @@ According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/ ```kotlin import com_github_leetcode.TreeNode +/* + * Example: + * var ti = TreeNode(5) + * var v = ti.`val` + * Definition for a binary tree node. + * class TreeNode(var `val`: Int) { + * var left: TreeNode? = null + * var right: TreeNode? = null + * } + */ class Solution { fun lowestCommonAncestor(root: TreeNode?, p: TreeNode, q: TreeNode): TreeNode? { if (root == null) { diff --git a/src/main/kotlin/g0301_0400/s0338_counting_bits/readme.md b/src/main/kotlin/g0301_0400/s0338_counting_bits/readme.md index c78d42f8..329cd374 100644 --- a/src/main/kotlin/g0301_0400/s0338_counting_bits/readme.md +++ b/src/main/kotlin/g0301_0400/s0338_counting_bits/readme.md @@ -45,7 +45,7 @@ class Solution { var borderPos = 1 var incrPos = 1 for (i in 1 until result.size) { - // when we reach pow of 2 , reset borderPos and incrPos + // when we reach pow of 2, reset borderPos and incrPos if (incrPos == borderPos) { result[i] = 1 incrPos = 1