From cbb3f42a6a0820e7ba012e182ef96cf5c59d790a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 27 Nov 2023 05:22:00 +0200 Subject: [PATCH] Added tasks 347-1143 --- README.md | 35 ++++++ .../readme.md | 110 ++++++++++++++++++ .../g0101_0200/s0139_word_break/readme.md | 60 ++++++++++ .../s0347_top_k_frequent_elements/readme.md | 46 ++++++++ .../g0301_0400/s0394_decode_string/readme.md | 91 +++++++++++++++ .../readme.md | 52 +++++++++ .../g0401_0500/s0437_path_sum_iii/readme.md | 55 +++++++++ .../readme.md | 75 ++++++++++++ .../g0401_0500/s0494_target_sum/readme.md | 74 ++++++++++++ .../s0543_diameter_of_binary_tree/readme.md | 67 +++++++++++ .../s0560_subarray_sum_equals_k/readme.md | 47 ++++++++ .../s0647_palindromic_substrings/readme.md | 63 ++++++++++ .../s0739_daily_temperatures/readme.md | 55 +++++++++ .../s0763_partition_labels/readme.md | 68 +++++++++++ .../readme.md | 68 +++++++++++ 15 files changed, 966 insertions(+) create mode 100644 src/main/ruby/g0101_0200/s0138_copy_list_with_random_pointer/readme.md create mode 100644 src/main/ruby/g0101_0200/s0139_word_break/readme.md create mode 100644 src/main/ruby/g0301_0400/s0347_top_k_frequent_elements/readme.md create mode 100644 src/main/ruby/g0301_0400/s0394_decode_string/readme.md create mode 100644 src/main/ruby/g0401_0500/s0416_partition_equal_subset_sum/readme.md create mode 100644 src/main/ruby/g0401_0500/s0437_path_sum_iii/readme.md create mode 100644 src/main/ruby/g0401_0500/s0438_find_all_anagrams_in_a_string/readme.md create mode 100644 src/main/ruby/g0401_0500/s0494_target_sum/readme.md create mode 100644 src/main/ruby/g0501_0600/s0543_diameter_of_binary_tree/readme.md create mode 100644 src/main/ruby/g0501_0600/s0560_subarray_sum_equals_k/readme.md create mode 100644 src/main/ruby/g0601_0700/s0647_palindromic_substrings/readme.md create mode 100644 src/main/ruby/g0701_0800/s0739_daily_temperatures/readme.md create mode 100644 src/main/ruby/g0701_0800/s0763_partition_labels/readme.md create mode 100644 src/main/ruby/g1101_1200/s1143_longest_common_subsequence/readme.md diff --git a/README.md b/README.md index 4efc12f..08fa561 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ | | | | | | |-|-|-|-|-|- +| 0438 |[Find All Anagrams in a String](src/main/ruby/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 186 | 82.72 #### Day 6 Breadth First Search Depth First Search @@ -109,6 +110,7 @@ | | | | | | |-|-|-|-|-|- +| 0139 |[Word Break](src/main/ruby/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 64 | 79.31 #### Day 16 Dynamic Programming @@ -120,6 +122,7 @@ | | | | | | |-|-|-|-|-|- +| 1143 |[Longest Common Subsequence](src/main/ruby/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 828 | 75.00 #### Day 18 Dynamic Programming @@ -368,6 +371,7 @@ | | | | | | |-|-|-|-|-|- +| 0139 |[Word Break](src/main/ruby/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 64 | 79.31 | 0042 |[Trapping Rain Water](src/main/ruby/g0001_0100/s0042_trapping_rain_water)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 69 | 80.74 #### Day 10 @@ -425,6 +429,7 @@ | | | | | | |-|-|-|-|-|- +| 1143 |[Longest Common Subsequence](src/main/ruby/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 828 | 75.00 | 0072 |[Edit Distance](src/main/ruby/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 139 | 73.91 #### Day 20 @@ -533,6 +538,7 @@ | | | | | | |-|-|-|-|-|- +| 0739 |[Daily Temperatures](src/main/ruby/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(n) | 490 | 84.71 #### Day 7 @@ -565,6 +571,7 @@ | | | | | | |-|-|-|-|-|- +| 0438 |[Find All Anagrams in a String](src/main/ruby/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 186 | 82.72 #### Day 13 @@ -575,6 +582,7 @@ | | | | | | |-|-|-|-|-|- +| 0138 |[Copy List with Random Pointer](src/main/ruby/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 65 | 69.84 #### Day 15 @@ -803,6 +811,7 @@ | | | | | | |-|-|-|-|-|- +| 0438 |[Find All Anagrams in a String](src/main/ruby/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n+m)_Space_O(1) | 186 | 82.72 #### Day 13 Hashmap @@ -814,6 +823,7 @@ | | | | | | |-|-|-|-|-|- +| 0394 |[Decode String](src/main/ruby/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 58 | 84.09 #### Day 15 Heap @@ -860,6 +870,8 @@ | | | | | | |-|-|-|-|-|- +| 0543 |[Diameter of Binary Tree](src/main/ruby/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 64 | 86.54 +| 0437 |[Path Sum III](src/main/ruby/g0401_0500/s0437_path_sum_iii)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 205 | 61.54 #### Day 8 Binary Search @@ -895,6 +907,7 @@ | | | | | | |-|-|-|-|-|- +| 0416 |[Partition Equal Subset Sum](src/main/ruby/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 1023 | 60.38 | 0152 |[Maximum Product Subarray](src/main/ruby/g0101_0200/s0152_maximum_product_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 63 | 80.30 #### Day 14 Sliding Window/Two Pointer @@ -957,6 +970,7 @@ | 0003 |[Longest Substring Without Repeating Characters](src/main/ruby/g0001_0100/s0003_longest_substring_without_repeating_characters)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1) | 94 | 85.62 | 0020 |[Valid Parentheses](src/main/ruby/g0001_0100/s0020_valid_parentheses)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 53 | 90.52 | 0005 |[Longest Palindromic Substring](src/main/ruby/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 117 | 98.63 +| 0394 |[Decode String](src/main/ruby/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 58 | 84.09 | 0049 |[Group Anagrams](src/main/ruby/g0001_0100/s0049_group_anagrams)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 154 | 83.69 #### Udemy Binary Search @@ -1020,6 +1034,7 @@ | 0021 |[Merge Two Sorted Lists](src/main/ruby/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 66 | 59.01 | 0160 |[Intersection of Two Linked Lists](src/main/ruby/g0101_0200/s0160_intersection_of_two_linked_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 108 | 75.86 | 0234 |[Palindrome Linked List](src/main/ruby/g0201_0300/s0234_palindrome_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 297 | 78.26 +| 0138 |[Copy List with Random Pointer](src/main/ruby/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 65 | 69.84 | 0025 |[Reverse Nodes in k-Group](src/main/ruby/g0001_0100/s0025_reverse_nodes_in_k_group)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 62 | 91.67 | 0146 |[LRU Cache](src/main/ruby/g0101_0200/s0146_lru_cache)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 290 | 78.05 @@ -1029,6 +1044,7 @@ |-|-|-|-|-|- | 0094 |[Binary Tree Inorder Traversal](src/main/ruby/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 56 | 84.00 | 0102 |[Binary Tree Level Order Traversal](src/main/ruby/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 66 | 76.43 +| 0543 |[Diameter of Binary Tree](src/main/ruby/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 64 | 86.54 | 0226 |[Invert Binary Tree](src/main/ruby/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 54 | 89.43 | 0104 |[Maximum Depth of Binary Tree](src/main/ruby/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 55 | 96.72 | 0124 |[Binary Tree Maximum Path Sum](src/main/ruby/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 71 | 98.00 @@ -1051,11 +1067,13 @@ | | | | | | |-|-|-|-|-|- +| 0139 |[Word Break](src/main/ruby/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 64 | 79.31 | 0152 |[Maximum Product Subarray](src/main/ruby/g0101_0200/s0152_maximum_product_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 63 | 80.30 | 0198 |[House Robber](src/main/ruby/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 41 | 99.28 | 0070 |[Climbing Stairs](src/main/ruby/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 48 | 96.01 | 0064 |[Minimum Path Sum](src/main/ruby/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 78 | 52.50 | 0300 |[Longest Increasing Subsequence](src/main/ruby/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 68 | 96.55 +| 1143 |[Longest Common Subsequence](src/main/ruby/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 828 | 75.00 | 0072 |[Edit Distance](src/main/ruby/g0001_0100/s0072_edit_distance)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 139 | 73.91 | 0010 |[Regular Expression Matching](src/main/ruby/g0001_0100/s0010_regular_expression_matching)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion, Big_O_Time_O(m\*n)_Space_O(m\*n) | 66 | 94.44 @@ -1201,6 +1219,7 @@ | | | | | | |-|-|-|-|-|- | 0238 |[Product of Array Except Self](src/main/ruby/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 123 | 76.59 +| 0560 |[Subarray Sum Equals K](src/main/ruby/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Big_O_Time_O(n)_Space_O(n) | 112 | 51.61 #### Day 6 String @@ -1211,6 +1230,7 @@ | | | | | | |-|-|-|-|-|- +| 0763 |[Partition Labels](src/main/ruby/g0701_0800/s0763_partition_labels)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 62 | 100.00 #### Day 8 String @@ -1288,6 +1308,7 @@ | | | | | | |-|-|-|-|-|- | 0215 |[Kth Largest Element in an Array](src/main/ruby/g0201_0300/s0215_kth_largest_element_in_an_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 123 | 93.55 +| 0347 |[Top K Frequent Elements](src/main/ruby/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, Big_O_Time_O(n\*log(n))_Space_O(k) | 67 | 83.85 #### Day 21 Heap Priority Queue @@ -1381,6 +1402,18 @@ | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- +| 1143 |[Longest Common Subsequence](src/main/ruby/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 828 | 75.00 +| 0763 |[Partition Labels](src/main/ruby/g0701_0800/s0763_partition_labels)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String, Big_O_Time_O(n)_Space_O(1) | 62 | 100.00 +| 0739 |[Daily Temperatures](src/main/ruby/g0701_0800/s0739_daily_temperatures)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6, Big_O_Time_O(n)_Space_O(n) | 490 | 84.71 +| 0647 |[Palindromic Substrings](src/main/ruby/g0601_0700/s0647_palindromic_substrings)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n) | 101 | 100.00 +| 0560 |[Subarray Sum Equals K](src/main/ruby/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array, Big_O_Time_O(n)_Space_O(n) | 112 | 51.61 +| 0543 |[Diameter of Binary Tree](src/main/ruby/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 64 | 86.54 +| 0494 |[Target Sum](src/main/ruby/g0401_0500/s0494_target_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking, Big_O_Time_O(n\*(sum+s))_Space_O(n\*(sum+s)) | 149 | 76.92 +| 0438 |[Find All Anagrams in a String](src/main/ruby/g0401_0500/s0438_find_all_anagrams_in_a_string)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer, Big_O_Time_O(n+m)_Space_O(1) | 186 | 82.72 +| 0437 |[Path Sum III](src/main/ruby/g0401_0500/s0437_path_sum_iii)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Big_O_Time_O(n)_Space_O(n) | 205 | 61.54 +| 0416 |[Partition Equal Subset Sum](src/main/ruby/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 1023 | 60.38 +| 0394 |[Decode String](src/main/ruby/g0301_0400/s0394_decode_string)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 58 | 84.09 +| 0347 |[Top K Frequent Elements](src/main/ruby/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, Big_O_Time_O(n\*log(n))_Space_O(k) | 67 | 83.85 | 0338 |[Counting Bits](src/main/ruby/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 90 | 96.34 | 0322 |[Coin Change](src/main/ruby/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, Big_O_Time_O(m\*n)_Space_O(amount) | 655 | 84.96 | 0300 |[Longest Increasing Subsequence](src/main/ruby/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, Big_O_Time_O(n\*log_n)_Space_O(n) | 68 | 96.55 @@ -1411,6 +1444,8 @@ | 0146 |[LRU Cache](src/main/ruby/g0101_0200/s0146_lru_cache)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Udemy_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 290 | 78.05 | 0142 |[Linked List Cycle II](src/main/ruby/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_10_Linked_List, Level_1_Day_4_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 62 | 93.94 | 0141 |[Linked List Cycle](src/main/ruby/g0101_0200/s0141_linked_list_cycle)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_I_Day_7_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 58 | 97.65 +| 0139 |[Word Break](src/main/ruby/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_9, Udemy_Dynamic_Programming, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 64 | 79.31 +| 0138 |[Copy List with Random Pointer](src/main/ruby/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Programming_Skills_II_Day_14, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 65 | 69.84 | 0136 |[Single Number](src/main/ruby/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Big_O_Time_O(N)_Space_O(1) | 68 | 78.44 | 0131 |[Palindrome Partitioning](src/main/ruby/g0101_0200/s0131_palindrome_partitioning)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 1192 | 63.64 | 0128 |[Longest Consecutive Sequence](src/main/ruby/g0101_0200/s0128_longest_consecutive_sequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 135 | 91.36 diff --git a/src/main/ruby/g0101_0200/s0138_copy_list_with_random_pointer/readme.md b/src/main/ruby/g0101_0200/s0138_copy_list_with_random_pointer/readme.md new file mode 100644 index 0000000..ad77c75 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0138_copy_list_with_random_pointer/readme.md @@ -0,0 +1,110 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 138\. Copy List with Random Pointer + +Medium + +A linked list of length `n` is given such that each node contains an additional random pointer, which could point to any node in the list, or `null`. + +Construct a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) of the list. The deep copy should consist of exactly `n` **brand new** nodes, where each new node has its value set to the value of its corresponding original node. Both the `next` and `random` pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. **None of the pointers in the new list should point to nodes in the original list**. + +For example, if there are two nodes `X` and `Y` in the original list, where `X.random --> Y`, then for the corresponding two nodes `x` and `y` in the copied list, `x.random --> y`. + +Return _the head of the copied linked list_. + +The linked list is represented in the input/output as a list of `n` nodes. Each node is represented as a pair of `[val, random_index]` where: + +* `val`: an integer representing `Node.val` +* `random_index`: the index of the node (range from `0` to `n-1`) that the `random` pointer points to, or `null` if it does not point to any node. + +Your code will **only** be given the `head` of the original linked list. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2019/12/18/e1.png) + +**Input:** head = \[\[7,null],[13,0],[11,4],[10,2],[1,0]] + +**Output:** [[7,null],[13,0],[11,4],[10,2],[1,0]] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2019/12/18/e2.png) + +**Input:** head = \[\[1,1],[2,1]] + +**Output:** [[1,1],[2,1]] + +**Example 3:** + +**![](https://assets.leetcode.com/uploads/2019/12/18/e3.png)** + +**Input:** head = \[\[3,null],[3,0],[3,null]] + +**Output:** [[3,null],[3,0],[3,null]] + +**Example 4:** + +**Input:** head = [] + +**Output:** [] + +**Explanation:** The given linked list is empty (null pointer), so return null. + +**Constraints:** + +* `0 <= n <= 1000` +* `-10000 <= Node.val <= 10000` +* `Node.random` is `null` or is pointing to some node in the linked list. + +## Solution + +```ruby +require_relative '../../com_github_leetcode/random/node' + +# Definition for Node. +# class Node +# attr_accessor :val, :next, :random +# def initialize(val = 0) +# @val = val +# @next = nil +# @random = nil +# end +# end + +# @param {Node} node +# @return {Node} +def copy_random_list(head) + return nil if head.nil? + + # First pass to create cloned nodes and insert them after the original nodes + curr = head + while curr + cloned_node = Node.new(curr.val) + cloned_node.next = curr.next + curr.next = cloned_node + curr = cloned_node.next + end + + # Second pass to set the random pointers of the cloned nodes + curr = head + while curr + curr.next.random = curr.random.next if curr.random + curr = curr.next.next + end + + # Third pass to separate the original and cloned nodes + curr = head + new_head = nil + while curr + cloned_node = curr.next + new_head ||= cloned_node + curr.next = cloned_node.next + cloned_node.next = curr.next.next if curr.next + curr = curr.next + end + + new_head +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0139_word_break/readme.md b/src/main/ruby/g0101_0200/s0139_word_break/readme.md new file mode 100644 index 0000000..307ee39 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0139_word_break/readme.md @@ -0,0 +1,60 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 139\. Word Break + +Medium + +Given a string `s` and a dictionary of strings `wordDict`, return `true` if `s` can be segmented into a space-separated sequence of one or more dictionary words. + +**Note** that the same word in the dictionary may be reused multiple times in the segmentation. + +**Example 1:** + +**Input:** s = "leetcode", wordDict = ["leet","code"] + +**Output:** true + +**Explanation:** Return true because "leetcode" can be segmented as "leet code". + +**Example 2:** + +**Input:** s = "applepenapple", wordDict = ["apple","pen"] + +**Output:** true + +**Explanation:** Return true because "applepenapple" can be segmented as "apple pen apple". Note that you are allowed to reuse a dictionary word. + +**Example 3:** + +**Input:** s = "catsandog", wordDict = ["cats","dog","sand","and","cat"] + +**Output:** false + +**Constraints:** + +* `1 <= s.length <= 300` +* `1 <= wordDict.length <= 1000` +* `1 <= wordDict[i].length <= 20` +* `s` and `wordDict[i]` consist of only lowercase English letters. +* All the strings of `wordDict` are **unique**. + +## Solution + +```ruby +# @param {String} s +# @param {String[]} word_dict +# @return {Boolean} +def word_break(s, word_dict) + memo = Array.new(s.size - 1, nil) + dp = lambda do |i| + return true if i < 0 + return memo[i] unless memo[i].nil? + + memo[i] = word_dict.any? do |word| + s[i - word.size + 1..i] == word && dp[i - word.size] + end + end + dp[s.size - 1] +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0301_0400/s0347_top_k_frequent_elements/readme.md b/src/main/ruby/g0301_0400/s0347_top_k_frequent_elements/readme.md new file mode 100644 index 0000000..6134a8c --- /dev/null +++ b/src/main/ruby/g0301_0400/s0347_top_k_frequent_elements/readme.md @@ -0,0 +1,46 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 347\. Top K Frequent Elements + +Medium + +Given an integer array `nums` and an integer `k`, return _the_ `k` _most frequent elements_. You may return the answer in **any order**. + +**Example 1:** + +**Input:** nums = [1,1,1,2,2,3], k = 2 + +**Output:** [1,2] + +**Example 2:** + +**Input:** nums = [1], k = 1 + +**Output:** [1] + +**Constraints:** + +* 1 <= nums.length <= 105 +* `k` is in the range `[1, the number of unique elements in the array]`. +* It is **guaranteed** that the answer is **unique**. + +**Follow up:** Your algorithm's time complexity must be better than `O(n log n)`, where n is the array's size. + +## Solution + +```ruby +# @param {Integer[]} nums +# @param {Integer} k +# @return {Integer[]} +def top_k_frequent(nums, k) + numhash = Hash.new(0) + nums.each {|num| numhash[num] += 1} + newhash = numhash.sort_by {|k, v| -v} + ret_array = [] + for i in 0...k + ret_array << newhash[i][0] + end + return ret_array +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0301_0400/s0394_decode_string/readme.md b/src/main/ruby/g0301_0400/s0394_decode_string/readme.md new file mode 100644 index 0000000..c346070 --- /dev/null +++ b/src/main/ruby/g0301_0400/s0394_decode_string/readme.md @@ -0,0 +1,91 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 394\. Decode String + +Medium + +Given an encoded string, return its decoded string. + +The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer. + +You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. + +Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, `k`. For example, there won't be input like `3a` or `2[4]`. + +**Example 1:** + +**Input:** s = "3[a]2[bc]" + +**Output:** "aaabcbc" + +**Example 2:** + +**Input:** s = "3[a2[c]]" + +**Output:** "accaccacc" + +**Example 3:** + +**Input:** s = "2[abc]3[cd]ef" + +**Output:** "abcabccdcdcdef" + +**Example 4:** + +**Input:** s = "abc3[cd]xyz" + +**Output:** "abccdcdcdxyz" + +**Constraints:** + +* `1 <= s.length <= 30` +* `s` consists of lowercase English letters, digits, and square brackets `'[]'`. +* `s` is guaranteed to be **a valid** input. +* All the integers in `s` are in the range `[1, 300]`. + +## Solution + +```ruby +# @param {String} s +# @return {String} +def decode_string(s) + @i = 0 + decode_helper(s) +end + +private + +def decode_helper(s) + count = 0 + result = "" + + while @i < s.length + c = s[@i] + @i += 1 + + if letter?(c) + result += c + elsif digit?(c) + count = count * 10 + c.to_i + elsif c == ']' + break + elsif c == '[' + # sub problem + repeat = decode_helper(s) + result += repeat * count + count = 0 + end + end + + result +end + +def letter?(c) + c =~ /[a-zA-Z]/ +end + +def digit?(c) + c =~ /\d/ +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0401_0500/s0416_partition_equal_subset_sum/readme.md b/src/main/ruby/g0401_0500/s0416_partition_equal_subset_sum/readme.md new file mode 100644 index 0000000..13b1f1b --- /dev/null +++ b/src/main/ruby/g0401_0500/s0416_partition_equal_subset_sum/readme.md @@ -0,0 +1,52 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 416\. Partition Equal Subset Sum + +Medium + +Given a **non-empty** array `nums` containing **only positive integers**, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. + +**Example 1:** + +**Input:** nums = [1,5,11,5] + +**Output:** true + +**Explanation:** The array can be partitioned as [1, 5, 5] and [11]. + +**Example 2:** + +**Input:** nums = [1,2,3,5] + +**Output:** false + +**Explanation:** The array cannot be partitioned into equal sum subsets. + +**Constraints:** + +* `1 <= nums.length <= 200` +* `1 <= nums[i] <= 100` + +## Solution + +```ruby +# @param {Integer[]} nums +# @return {Boolean} +def can_partition(nums) + sums = nums.sum + return false if sums.odd? + + sums /= 2 + dp = Array.new(sums + 1, false) + dp[0] = true + + nums.each do |num| + sums.downto(num) do |sum| + dp[sum] = dp[sum] || dp[sum - num] + end + end + + dp[sums] +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0401_0500/s0437_path_sum_iii/readme.md b/src/main/ruby/g0401_0500/s0437_path_sum_iii/readme.md new file mode 100644 index 0000000..1d68ad1 --- /dev/null +++ b/src/main/ruby/g0401_0500/s0437_path_sum_iii/readme.md @@ -0,0 +1,55 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 437\. Path Sum III + +Medium + +Given the `root` of a binary tree and an integer `targetSum`, return _the number of paths where the sum of the values along the path equals_ `targetSum`. + +The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes). + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg) + +**Input:** root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8 + +**Output:** 3 + +**Explanation:** The paths that sum to 8 are shown. + +**Example 2:** + +**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 + +**Output:** 3 + +**Constraints:** + +* The number of nodes in the tree is in the range `[0, 1000]`. +* -109 <= Node.val <= 109 +* `-1000 <= targetSum <= 1000` + +## Solution + +```ruby +# Definition for a binary tree node. +# class TreeNode +# attr_accessor :val, :left, :right +# def initialize(val = 0, left = nil, right = nil) +# @val = val +# @left = left +# @right = right +# end +# end +# @param {TreeNode} root +# @param {Integer} target_sum +# @return {Integer} +def path_sum(root, target_sum, sums = []) + return 0 unless root + sums = (sums + [0]).map {|s| s + root.val} + count = sums.count(target_sum) + return count + path_sum(root.left, target_sum, sums) + path_sum(root.right, target_sum, sums) +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0401_0500/s0438_find_all_anagrams_in_a_string/readme.md b/src/main/ruby/g0401_0500/s0438_find_all_anagrams_in_a_string/readme.md new file mode 100644 index 0000000..d7fe0f2 --- /dev/null +++ b/src/main/ruby/g0401_0500/s0438_find_all_anagrams_in_a_string/readme.md @@ -0,0 +1,75 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 438\. Find All Anagrams in a String + +Medium + +Given two strings `s` and `p`, return _an array of all the start indices of_ `p`_'s anagrams in_ `s`. You may return the answer in **any order**. + +An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. + +**Example 1:** + +**Input:** s = "cbaebabacd", p = "abc" + +**Output:** [0,6] + +**Explanation:** + + The substring with start index = 0 is "cba", which is an anagram of "abc". + The substring with start index = 6 is "bac", which is an anagram of "abc". + +**Example 2:** + +**Input:** s = "abab", p = "ab" + +**Output:** [0,1,2] + +**Explanation:** + + The substring with start index = 0 is "ab", which is an anagram of "ab". + The substring with start index = 1 is "ba", which is an anagram of "ab". + The substring with start index = 2 is "ab", which is an anagram of "ab". + +**Constraints:** + +* 1 <= s.length, p.length <= 3 * 104 +* `s` and `p` consist of lowercase English letters. + +## Solution + +```ruby +# @param {String} s +# @param {String} p +# @return {Integer[]} +def find_anagrams(s, p) + map = Array.new(26, 0) + + p.each_char {|char| map[char.ord - 'a'.ord] += 1} + + res = [] + i = 0 + j = 0 + + while i < s.length + idx = s[i].ord - 'a'.ord + map[idx] -= 1 + + if i >= p.length + map[s[j].ord - 'a'.ord] += 1 + j += 1 + end + + finish = map.all? {|count| count == 0} + + if i >= p.length - 1 && finish + res << j + end + + i += 1 + end + + res +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0401_0500/s0494_target_sum/readme.md b/src/main/ruby/g0401_0500/s0494_target_sum/readme.md new file mode 100644 index 0000000..9daec04 --- /dev/null +++ b/src/main/ruby/g0401_0500/s0494_target_sum/readme.md @@ -0,0 +1,74 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 494\. Target Sum + +Medium + +You are given an integer array `nums` and an integer `target`. + +You want to build an **expression** out of nums by adding one of the symbols `'+'` and `'-'` before each integer in nums and then concatenate all the integers. + +* For example, if `nums = [2, 1]`, you can add a `'+'` before `2` and a `'-'` before `1` and concatenate them to build the expression `"+2-1"`. + +Return the number of different **expressions** that you can build, which evaluates to `target`. + +**Example 1:** + +**Input:** nums = [1,1,1,1,1], target = 3 + +**Output:** 5 + +**Explanation:** + + There are 5 ways to assign symbols to make the sum of nums be target 3. + -1 + 1 + 1 + 1 + 1 = 3 + +1 - 1 + 1 + 1 + 1 = 3 + +1 + 1 - 1 + 1 + 1 = 3 + +1 + 1 + 1 - 1 + 1 = 3 + +1 + 1 + 1 + 1 - 1 = 3 + +**Example 2:** + +**Input:** nums = [1], target = 1 + +**Output:** 1 + +**Constraints:** + +* `1 <= nums.length <= 20` +* `0 <= nums[i] <= 1000` +* `0 <= sum(nums[i]) <= 1000` +* `-1000 <= target <= 1000` + +## Solution + +```ruby +# @param {Integer[]} nums +# @param {Integer} target +# @return {Integer} +def find_target_sum_ways(nums, s) + sum = nums.sum + s = s.abs + + # Invalid s, just return 0 + return 0 if s > sum || (sum + s).odd? + + dp = Array.new((sum + s) / 2 + 1) {Array.new(nums.length + 1, 0)} + dp[0][0] = 1 + + # empty knapsack must be processed specially + nums.each_with_index do |num, i| + dp[0][i + 1] = num.zero? ? dp[0][i] * 2 : dp[0][i] + end + + (1...dp.length).each do |i| + (0...nums.length).each do |j| + dp[i][j + 1] += dp[i][j] + dp[i][j + 1] += dp[i - nums[j]][j] if nums[j] <= i + end + end + + dp[(sum + s) / 2][nums.length] +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0501_0600/s0543_diameter_of_binary_tree/readme.md b/src/main/ruby/g0501_0600/s0543_diameter_of_binary_tree/readme.md new file mode 100644 index 0000000..7257451 --- /dev/null +++ b/src/main/ruby/g0501_0600/s0543_diameter_of_binary_tree/readme.md @@ -0,0 +1,67 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 543\. Diameter of Binary Tree + +Easy + +Given the `root` of a binary tree, return _the length of the **diameter** of the tree_. + +The **diameter** of a binary tree is the **length** of the longest path between any two nodes in a tree. This path may or may not pass through the `root`. + +The **length** of a path between two nodes is represented by the number of edges between them. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg) + +**Input:** root = [1,2,3,4,5] + +**Output:** 3 + +**Explanation:** 3 is the length of the path [4,2,1,3] or [5,2,1,3]. + +**Example 2:** + +**Input:** root = [1,2] + +**Output:** 1 + +**Constraints:** + +* The number of nodes in the tree is in the range [1, 104]. +* `-100 <= Node.val <= 100` + +## Solution + +```ruby +# Definition for a binary tree node. +# class TreeNode +# attr_accessor :val, :left, :right +# def initialize(val = 0, left = nil, right = nil) +# @val = val +# @left = left +# @right = right +# end +# end +# @param {TreeNode} root +# @return {Integer} +def diameter_of_binary_tree(root) + @diameter = 0 + diameter_of_binary_tree_util(root) + @diameter +end + +private + +def diameter_of_binary_tree_util(root) + return 0 if root.nil? + + left_length = root.left ? 1 + diameter_of_binary_tree_util(root.left) : 0 + right_length = root.right ? 1 + diameter_of_binary_tree_util(root.right) : 0 + + @diameter = [left_length + right_length, @diameter].max + + [left_length, right_length].max +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0501_0600/s0560_subarray_sum_equals_k/readme.md b/src/main/ruby/g0501_0600/s0560_subarray_sum_equals_k/readme.md new file mode 100644 index 0000000..f4fc3c4 --- /dev/null +++ b/src/main/ruby/g0501_0600/s0560_subarray_sum_equals_k/readme.md @@ -0,0 +1,47 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 560\. Subarray Sum Equals K + +Medium + +Given an array of integers `nums` and an integer `k`, return _the total number of continuous subarrays whose sum equals to `k`_. + +**Example 1:** + +**Input:** nums = [1,1,1], k = 2 + +**Output:** 2 + +**Example 2:** + +**Input:** nums = [1,2,3], k = 3 + +**Output:** 2 + +**Constraints:** + +* 1 <= nums.length <= 2 * 104 +* `-1000 <= nums[i] <= 1000` +* -107 <= k <= 107 + +## Solution + +```ruby +# @param {Integer[]} nums +# @param {Integer} k +# @return {Integer} +def subarray_sum(nums, k) + temp_sum = 0 + ret = 0 + sum_count = {0 => 1} + + nums.each do |i| + temp_sum += i + ret += sum_count[temp_sum - k] if sum_count[temp_sum - k] + sum_count[temp_sum] = sum_count[temp_sum].to_i + 1 + end + + ret +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0601_0700/s0647_palindromic_substrings/readme.md b/src/main/ruby/g0601_0700/s0647_palindromic_substrings/readme.md new file mode 100644 index 0000000..cf25895 --- /dev/null +++ b/src/main/ruby/g0601_0700/s0647_palindromic_substrings/readme.md @@ -0,0 +1,63 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 647\. Palindromic Substrings + +Medium + +Given a string `s`, return _the number of **palindromic substrings** in it_. + +A string is a **palindrome** when it reads the same backward as forward. + +A **substring** is a contiguous sequence of characters within the string. + +**Example 1:** + +**Input:** s = "abc" + +**Output:** 3 + +**Explanation:** Three palindromic strings: "a", "b", "c". + +**Example 2:** + +**Input:** s = "aaa" + +**Output:** 6 + +**Explanation:** Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". + +**Constraints:** + +* `1 <= s.length <= 1000` +* `s` consists of lowercase English letters. + +## Solution + +```ruby +# @param {String} s +# @return {Integer} +def count_substrings(s) + a = s.chars + res = [0] + + (0...a.length).each do |i| + expand_count(a, i, i, res) + expand_count(a, i, i + 1, res) + end + + res[0] +end + +def expand_count(a, l, r, res) + while l >= 0 && r < a.length + if a[l] != a[r] + return + else + res[0] += 1 + l -= 1 + r += 1 + end + end +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0701_0800/s0739_daily_temperatures/readme.md b/src/main/ruby/g0701_0800/s0739_daily_temperatures/readme.md new file mode 100644 index 0000000..e9c6193 --- /dev/null +++ b/src/main/ruby/g0701_0800/s0739_daily_temperatures/readme.md @@ -0,0 +1,55 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 739\. Daily Temperatures + +Medium + +Given an array of integers `temperatures` represents the daily temperatures, return _an array_ `answer` _such that_ `answer[i]` _is the number of days you have to wait after the_ ith _day to get a warmer temperature_. If there is no future day for which this is possible, keep `answer[i] == 0` instead. + +**Example 1:** + +**Input:** temperatures = [73,74,75,71,69,72,76,73] + +**Output:** [1,1,4,2,1,1,0,0] + +**Example 2:** + +**Input:** temperatures = [30,40,50,60] + +**Output:** [1,1,1,0] + +**Example 3:** + +**Input:** temperatures = [30,60,90] + +**Output:** [1,1,0] + +**Constraints:** + +* 1 <= temperatures.length <= 105 +* `30 <= temperatures[i] <= 100` + +## Solution + +```ruby +# @param {Integer[]} temperatures +# @return {Integer[]} +def daily_temperatures(temperatures) + sol = Array.new(temperatures.length, 0) + (temperatures.length - 2).downto(0) do |i| + j = i + 1 + while j <= temperatures.length + if temperatures[i] < temperatures[j] + sol[i] = j - i + break + else + break if sol[j].zero? + + j += sol[j] + end + end + end + sol +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0701_0800/s0763_partition_labels/readme.md b/src/main/ruby/g0701_0800/s0763_partition_labels/readme.md new file mode 100644 index 0000000..22bede8 --- /dev/null +++ b/src/main/ruby/g0701_0800/s0763_partition_labels/readme.md @@ -0,0 +1,68 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 763\. Partition Labels + +Medium + +You are given a string `s`. We want to partition the string into as many parts as possible so that each letter appears in at most one part. + +Return _a list of integers representing the size of these parts_. + +**Example 1:** + +**Input:** s = "ababcbacadefegdehijhklij" + +**Output:** [9,7,8] + +**Explanation:** + + The partition is "ababcbaca", "defegde", "hijhklij". + This is a partition so that each letter appears in at most one part. + A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts. + +**Example 2:** + +**Input:** s = "eccbbbbdec" + +**Output:** [10] + +**Constraints:** + +* `1 <= s.length <= 500` +* `s` consists of lowercase English letters. + +## Solution + +```ruby +# @param {String} s +# @return {Integer[]} +def partition_labels(s) + letters = s.chars + result = [] + position = Array.new(26, 0) + + letters.each_with_index do |char, i| + position[char.ord - 'a'.ord] = i + end + + i = 0 + prev = -1 + max = 0 + + while i < letters.length + if position[letters[i].ord - 'a'.ord] > max + max = position[letters[i].ord - 'a'.ord] + end + + if i == max + result.push(i - prev) + prev = i + end + + i += 1 + end + + result +end +``` \ No newline at end of file diff --git a/src/main/ruby/g1101_1200/s1143_longest_common_subsequence/readme.md b/src/main/ruby/g1101_1200/s1143_longest_common_subsequence/readme.md new file mode 100644 index 0000000..6697b69 --- /dev/null +++ b/src/main/ruby/g1101_1200/s1143_longest_common_subsequence/readme.md @@ -0,0 +1,68 @@ +[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) +[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) + +## 1143\. Longest Common Subsequence + +Medium + +Given two strings `text1` and `text2`, return _the length of their longest **common subsequence**._ If there is no **common subsequence**, return `0`. + +A **subsequence** of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. + +* For example, `"ace"` is a subsequence of `"abcde"`. + +A **common subsequence** of two strings is a subsequence that is common to both strings. + +**Example 1:** + +**Input:** text1 = "abcde", text2 = "ace" + +**Output:** 3 + +**Explanation:** The longest common subsequence is "ace" and its length is 3. + +**Example 2:** + +**Input:** text1 = "abc", text2 = "abc" + +**Output:** 3 + +**Explanation:** The longest common subsequence is "abc" and its length is 3. + +**Example 3:** + +**Input:** text1 = "abc", text2 = "def" + +**Output:** 0 + +**Explanation:** There is no such common subsequence, so the result is 0. + +**Constraints:** + +* `1 <= text1.length, text2.length <= 1000` +* `text1` and `text2` consist of only lowercase English characters. + +## Solution + +```ruby +# @param {String} text1 +# @param {String} text2 +# @return {Integer} +def longest_common_subsequence(text1, text2) + n = text1.length + m = text2.length + dp = Array.new(n + 1) {Array.new(m + 1, 0)} + + (1..n).each do |i| + (1..m).each do |j| + if text1[i - 1] == text2[j - 1] + dp[i][j] = dp[i - 1][j - 1] + 1 + else + dp[i][j] = [dp[i - 1][j], dp[i][j - 1]].max + end + end + end + + dp[n][m] +end +``` \ No newline at end of file