From 995841415b9564dcad832b8bba61d6c789066595 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 26 Nov 2023 03:03:24 +0200 Subject: [PATCH] Added tasks 142-338 --- README.md | 95 ++++++++++++ .../g0001_0100/s0079_word_search/readme.md | 1 - .../s0141_linked_list_cycle/readme.md | 72 +++++++++ .../s0142_linked_list_cycle_ii/readme.md | 90 +++++++++++ .../ruby/g0101_0200/s0146_lru_cache/readme.md | 140 ++++++++++++++++++ .../ruby/g0101_0200/s0148_sort_list/readme.md | 71 +++++++++ .../s0152_maximum_product_subarray/readme.md | 51 +++++++ .../readme.md | 78 ++++++++++ .../ruby/g0101_0200/s0155_min_stack/readme.md | 103 +++++++++++++ .../readme.md | 99 +++++++++++++ .../s0169_majority_element/readme.md | 52 +++++++ .../g0101_0200/s0189_rotate_array/readme.md | 67 +++++++++ .../g0101_0200/s0198_house_robber/readme.md | 59 ++++++++ .../s0200_number_of_islands/readme.md | 76 ++++++++++ .../s0206_reverse_linked_list/readme.md | 63 ++++++++ .../s0207_course_schedule/readme.md | 79 ++++++++++ .../readme.md | 120 +++++++++++++++ .../readme.md | 40 +++++ .../g0201_0300/s0221_maximal_square/readme.md | 68 +++++++++ .../s0226_invert_binary_tree/readme.md | 60 ++++++++ .../readme.md | 75 ++++++++++ .../s0234_palindrome_linked_list/readme.md | 80 ++++++++++ .../readme.md | 74 +++++++++ .../readme.md | 57 +++++++ .../s0239_sliding_window_maximum/readme.md | 96 ++++++++++++ .../s0240_search_a_2d_matrix_ii/readme.md | 63 ++++++++ .../g0201_0300/s0283_move_zeroes/readme.md | 54 +++++++ .../s0287_find_the_duplicate_number/readme.md | 65 ++++++++ .../readme.md | 90 +++++++++++ .../readme.md | 80 ++++++++++ .../g0301_0400/s0322_coin_change/readme.md | 65 ++++++++ .../g0301_0400/s0338_counting_bits/readme.md | 70 +++++++++ 32 files changed, 2352 insertions(+), 1 deletion(-) create mode 100644 src/main/ruby/g0101_0200/s0141_linked_list_cycle/readme.md create mode 100644 src/main/ruby/g0101_0200/s0142_linked_list_cycle_ii/readme.md create mode 100644 src/main/ruby/g0101_0200/s0146_lru_cache/readme.md create mode 100644 src/main/ruby/g0101_0200/s0148_sort_list/readme.md create mode 100644 src/main/ruby/g0101_0200/s0152_maximum_product_subarray/readme.md create mode 100644 src/main/ruby/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/readme.md create mode 100644 src/main/ruby/g0101_0200/s0155_min_stack/readme.md create mode 100644 src/main/ruby/g0101_0200/s0160_intersection_of_two_linked_lists/readme.md create mode 100644 src/main/ruby/g0101_0200/s0169_majority_element/readme.md create mode 100644 src/main/ruby/g0101_0200/s0189_rotate_array/readme.md create mode 100644 src/main/ruby/g0101_0200/s0198_house_robber/readme.md create mode 100644 src/main/ruby/g0101_0200/s0200_number_of_islands/readme.md create mode 100644 src/main/ruby/g0201_0300/s0206_reverse_linked_list/readme.md create mode 100644 src/main/ruby/g0201_0300/s0207_course_schedule/readme.md create mode 100644 src/main/ruby/g0201_0300/s0208_implement_trie_prefix_tree/readme.md create mode 100644 src/main/ruby/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md create mode 100644 src/main/ruby/g0201_0300/s0221_maximal_square/readme.md create mode 100644 src/main/ruby/g0201_0300/s0226_invert_binary_tree/readme.md create mode 100644 src/main/ruby/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md create mode 100644 src/main/ruby/g0201_0300/s0234_palindrome_linked_list/readme.md create mode 100644 src/main/ruby/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md create mode 100644 src/main/ruby/g0201_0300/s0238_product_of_array_except_self/readme.md create mode 100644 src/main/ruby/g0201_0300/s0239_sliding_window_maximum/readme.md create mode 100644 src/main/ruby/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md create mode 100644 src/main/ruby/g0201_0300/s0283_move_zeroes/readme.md create mode 100644 src/main/ruby/g0201_0300/s0287_find_the_duplicate_number/readme.md create mode 100644 src/main/ruby/g0201_0300/s0295_find_median_from_data_stream/readme.md create mode 100644 src/main/ruby/g0201_0300/s0300_longest_increasing_subsequence/readme.md create mode 100644 src/main/ruby/g0301_0400/s0322_coin_change/readme.md create mode 100644 src/main/ruby/g0301_0400/s0338_counting_bits/readme.md diff --git a/README.md b/README.md index 85269c1..4efc12f 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ | | | | | | |-|-|-|-|-|- +| 0153 |[Find Minimum in Rotated Sorted Array](src/main/ruby/g0101_0200/s0153_find_minimum_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 58 | 78.74 #### Day 3 Two Pointers @@ -53,6 +54,7 @@ | | | | | | |-|-|-|-|-|- +| 0200 |[Number of Islands](src/main/ruby/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 141 | 72.68 #### Day 7 Breadth First Search Depth First Search @@ -112,6 +114,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 17 Dynamic Programming @@ -123,6 +126,7 @@ | | | | | | |-|-|-|-|-|- | 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 +| 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, Big_O_Time_O(m\*n)_Space_O(amount) | 655 | 84.96 #### Day 19 Bit Manipulation @@ -204,6 +208,7 @@ | | | | | | |-|-|-|-|-|- +| 0153 |[Find Minimum in Rotated Sorted Array](src/main/ruby/g0101_0200/s0153_find_minimum_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 58 | 78.74 ### Binary Search II @@ -221,6 +226,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 4 @@ -231,6 +237,7 @@ | | | | | | |-|-|-|-|-|- +| 0287 |[Find the Duplicate Number](src/main/ruby/g0201_0300/s0287_find_the_duplicate_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Big_O_Time_O(n)_Space_O(n) | 107 | 81.25 #### Day 6 @@ -246,6 +253,7 @@ | | | | | | |-|-|-|-|-|- +| 0240 |[Search a 2D Matrix II](src/main/ruby/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 154 | 100.00 #### Day 9 @@ -324,6 +332,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 4 @@ -342,6 +351,7 @@ | | | | | | |-|-|-|-|-|- +| 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 7 @@ -397,6 +407,7 @@ | | | | | | |-|-|-|-|-|- | 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 +| 0221 |[Maximal Square](src/main/ruby/g0201_0300/s0221_maximal_square)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 184 | 80.00 #### Day 17 @@ -408,6 +419,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 19 @@ -419,6 +431,7 @@ | | | | | | |-|-|-|-|-|- +| 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, Big_O_Time_O(m\*n)_Space_O(amount) | 655 | 84.96 #### Day 21 @@ -456,6 +469,7 @@ | | | | | | |-|-|-|-|-|- +| 0283 |[Move Zeroes](src/main/ruby/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 95 | 78.01 #### Day 7 Array @@ -582,6 +596,7 @@ | | | | | | |-|-|-|-|-|- +| 0155 |[Min Stack](src/main/ruby/g0101_0200/s0155_min_stack)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 76 | 79.40 #### Day 19 @@ -599,6 +614,7 @@ | | | | | | |-|-|-|-|-|- +| 0200 |[Number of Islands](src/main/ruby/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 141 | 72.68 #### Day 2 Matrix Related Problems @@ -734,11 +750,13 @@ | | | | | | |-|-|-|-|-|- | 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 +| 0206 |[Reverse Linked List](src/main/ruby/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 52 | 93.99 #### Day 4 Linked List | | | | | | |-|-|-|-|-|- +| 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, Big_O_Time_O(N)_Space_O(1) | 62 | 93.94 #### Day 5 Greedy @@ -767,6 +785,7 @@ | | | | | | |-|-|-|-|-|- +| 0200 |[Number of Islands](src/main/ruby/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 141 | 72.68 #### Day 10 Dynamic Programming @@ -818,11 +837,13 @@ | | | | | | |-|-|-|-|-|- | 0019 |[Remove Nth Node From End of List](src/main/ruby/g0001_0100/s0019_remove_nth_node_from_end_of_list)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 51 | 95.27 +| 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 #### Day 4 Linked List | | | | | | |-|-|-|-|-|- +| 0148 |[Sort List](src/main/ruby/g0101_0200/s0148_sort_list)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(log(N))_Space_O(log(N)) | 188 | 84.21 #### Day 5 Greedy @@ -833,6 +854,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 7 Tree @@ -850,6 +872,7 @@ | | | | | | |-|-|-|-|-|- +| 0230 |[Kth Smallest Element in a BST](src/main/ruby/g0201_0300/s0230_kth_smallest_element_in_a_bst)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 68 | 89.41 #### Day 10 Graph/BFS/DFS @@ -865,11 +888,14 @@ | | | | | | |-|-|-|-|-|- +| 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 +| 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, Big_O_Time_O(m\*n)_Space_O(amount) | 655 | 84.96 #### Day 13 Dynamic Programming | | | | | | |-|-|-|-|-|- +| 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 @@ -888,6 +914,8 @@ | | | | | | |-|-|-|-|-|- +| 0155 |[Min Stack](src/main/ruby/g0101_0200/s0155_min_stack)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 76 | 79.40 +| 0208 |[Implement Trie (Prefix Tree)](src/main/ruby/g0201_0300/s0208_implement_trie_prefix_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 167 | 69.81 #### Day 17 Interval @@ -936,16 +964,21 @@ | | | | | | |-|-|-|-|-|- | 0033 |[Search in Rotated Sorted Array](src/main/ruby/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 59 | 74.85 +| 0153 |[Find Minimum in Rotated Sorted Array](src/main/ruby/g0101_0200/s0153_find_minimum_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 58 | 78.74 #### Udemy Arrays | | | | | | |-|-|-|-|-|- | 0121 |[Best Time to Buy and Sell Stock](src/main/ruby/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 147 | 80.21 +| 0283 |[Move Zeroes](src/main/ruby/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 95 | 78.01 | 0001 |[Two Sum](src/main/ruby/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 57 | 89.38 +| 0189 |[Rotate Array](src/main/ruby/g0101_0200/s0189_rotate_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 100 | 92.40 | 0055 |[Jump Game](src/main/ruby/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 88 | 95.88 | 0075 |[Sort Colors](src/main/ruby/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 54 | 88.46 +| 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 | 0041 |[First Missing Positive](src/main/ruby/g0001_0100/s0041_first_missing_positive)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 130 | 65.63 +| 0239 |[Sliding Window Maximum](src/main/ruby/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Big_O_Time_O(n\*k)_Space_O(n+k) | 511 | 77.78 #### Udemy Two Pointers @@ -959,6 +992,7 @@ | | | | | | |-|-|-|-|-|- | 0053 |[Maximum Subarray](src/main/ruby/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 125 | 81.22 +| 0169 |[Majority Element](src/main/ruby/g0101_0200/s0169_majority_element)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 75 | 64.60 #### Udemy Sorting Algorithms @@ -980,8 +1014,14 @@ |-|-|-|-|-|- | 0114 |[Flatten Binary Tree to Linked List](src/main/ruby/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 47 | 100.00 | 0024 |[Swap Nodes in Pairs](src/main/ruby/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 59 | 76.00 +| 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, 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, Big_O_Time_O(N)_Space_O(1) | 58 | 97.65 +| 0206 |[Reverse Linked List](src/main/ruby/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 52 | 93.99 | 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 | 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 #### Udemy Tree Stack Queue @@ -989,26 +1029,33 @@ |-|-|-|-|-|- | 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 +| 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 | 0098 |[Validate Binary Search Tree](src/main/ruby/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 68 | 70.23 +| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/ruby/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 75 | 86.57 #### Udemy Trie and Heap | | | | | | |-|-|-|-|-|- +| 0208 |[Implement Trie (Prefix Tree)](src/main/ruby/g0201_0300/s0208_implement_trie_prefix_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 167 | 69.81 #### Udemy Graph | | | | | | |-|-|-|-|-|- +| 0200 |[Number of Islands](src/main/ruby/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 141 | 72.68 #### Udemy Dynamic Programming | | | | | | |-|-|-|-|-|- +| 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 | 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 @@ -1026,11 +1073,13 @@ | | | | | | |-|-|-|-|-|- +| 0338 |[Counting Bits](src/main/ruby/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 90 | 96.34 #### Udemy Design | | | | | | |-|-|-|-|-|- +| 0155 |[Min Stack](src/main/ruby/g0101_0200/s0155_min_stack)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 76 | 79.40 ### Data Structure I @@ -1072,12 +1121,14 @@ | | | | | | |-|-|-|-|-|- +| 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, Big_O_Time_O(N)_Space_O(1) | 58 | 97.65 | 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 #### Day 8 Linked List | | | | | | |-|-|-|-|-|- +| 0206 |[Reverse Linked List](src/main/ruby/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 52 | 93.99 #### Day 9 Stack Queue @@ -1103,6 +1154,7 @@ | | | | | | |-|-|-|-|-|- +| 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 #### Day 13 Tree @@ -1122,6 +1174,7 @@ | | | | | | |-|-|-|-|-|- | 0136 |[Single Number](src/main/ruby/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 68 | 78.44 +| 0169 |[Majority Element](src/main/ruby/g0101_0200/s0169_majority_element)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 75 | 64.60 | 0015 |[3Sum](src/main/ruby/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 659 | 83.74 #### Day 2 Array @@ -1141,11 +1194,13 @@ | | | | | | |-|-|-|-|-|- +| 0240 |[Search a 2D Matrix II](src/main/ruby/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 154 | 100.00 #### Day 5 Array | | | | | | |-|-|-|-|-|- +| 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 #### Day 6 String @@ -1174,11 +1229,13 @@ | | | | | | |-|-|-|-|-|- | 0002 |[Add Two Numbers](src/main/ruby/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 68 | 97.48 +| 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, Big_O_Time_O(N)_Space_O(1) | 62 | 93.94 #### Day 11 Linked List | | | | | | |-|-|-|-|-|- +| 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 #### Day 12 Linked List @@ -1196,6 +1253,7 @@ | | | | | | |-|-|-|-|-|- +| 0155 |[Min Stack](src/main/ruby/g0101_0200/s0155_min_stack)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 76 | 79.40 #### Day 15 Tree @@ -1212,11 +1270,13 @@ | | | | | | |-|-|-|-|-|- +| 0230 |[Kth Smallest Element in a BST](src/main/ruby/g0201_0300/s0230_kth_smallest_element_in_a_bst)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 68 | 89.41 #### Day 18 Tree | | | | | | |-|-|-|-|-|- +| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/ruby/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 75 | 86.57 #### Day 19 Graph @@ -1227,6 +1287,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 #### Day 21 Heap Priority Queue @@ -1245,11 +1306,13 @@ | | | | | | |-|-|-|-|-|- +| 0189 |[Rotate Array](src/main/ruby/g0101_0200/s0189_rotate_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 100 | 92.40 #### Day 3 Two Pointers | | | | | | |-|-|-|-|-|- +| 0283 |[Move Zeroes](src/main/ruby/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 95 | 78.01 #### Day 4 Two Pointers @@ -1288,6 +1351,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 +| 0206 |[Reverse Linked List](src/main/ruby/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 52 | 93.99 #### Day 11 Recursion Backtracking @@ -1300,6 +1364,7 @@ | | | | | | |-|-|-|-|-|- | 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 +| 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 #### Day 13 Bit Manipulation @@ -1316,6 +1381,36 @@ | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- +| 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 +| 0295 |[Find Median from Data Stream](src/main/ruby/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, Big_O_Time_O(n\*log_n)_Space_O(n) | 314 | 95.00 +| 0287 |[Find the Duplicate Number](src/main/ruby/g0201_0300/s0287_find_the_duplicate_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 107 | 81.25 +| 0283 |[Move Zeroes](src/main/ruby/g0201_0300/s0283_move_zeroes)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 95 | 78.01 +| 0240 |[Search a 2D Matrix II](src/main/ruby/g0201_0300/s0240_search_a_2d_matrix_ii)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8, Big_O_Time_O(n+m)_Space_O(1) | 154 | 100.00 +| 0239 |[Sliding Window Maximum](src/main/ruby/g0201_0300/s0239_sliding_window_maximum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n\*k)_Space_O(n+k) | 511 | 77.78 +| 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, Data_Structure_II_Day_5_Array, Udemy_Arrays, Big_O_Time_O(n^2)_Space_O(n) | 123 | 76.59 +| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/ruby/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 75 | 86.57 +| 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, Level_2_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 297 | 78.26 +| 0230 |[Kth Smallest Element in a BST](src/main/ruby/g0201_0300/s0230_kth_smallest_element_in_a_bst)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_17_Tree, Level_2_Day_9_Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 68 | 89.41 +| 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, Data_Structure_I_Day_12_Tree, Level_2_Day_6_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 54 | 89.43 +| 0221 |[Maximal Square](src/main/ruby/g0201_0300/s0221_maximal_square)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Big_O_Time_O(m\*n)_Space_O(m\*n) | 184 | 80.00 +| 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, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 123 | 93.55 +| 0208 |[Implement Trie (Prefix Tree)](src/main/ruby/g0201_0300/s0208_implement_trie_prefix_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 167 | 69.81 +| 0207 |[Course Schedule](src/main/ruby/g0201_0300/s0207_course_schedule)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 60 | 95.42 +| 0206 |[Reverse Linked List](src/main/ruby/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 52 | 93.99 +| 0200 |[Number of Islands](src/main/ruby/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_1_Matrix_Related_Problems, Level_1_Day_9_Graph/BFS/DFS, Udemy_Graph, Big_O_Time_O(M\*N)_Space_O(M\*N) | 141 | 72.68 +| 0198 |[House Robber](src/main/ruby/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 41 | 99.28 +| 0189 |[Rotate Array](src/main/ruby/g0101_0200/s0189_rotate_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 100 | 92.40 +| 0169 |[Majority Element](src/main/ruby/g0101_0200/s0169_majority_element)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Data_Structure_II_Day_1_Array, Udemy_Famous_Algorithm, Big_O_Time_O(n)_Space_O(1) | 75 | 64.60 +| 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, Data_Structure_II_Day_11_Linked_List, Udemy_Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 108 | 75.86 +| 0155 |[Min Stack](src/main/ruby/g0101_0200/s0155_min_stack)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Data_Structure_II_Day_14_Stack_Queue, Programming_Skills_II_Day_18, Level_2_Day_16_Design, Udemy_Design, Big_O_Time_O(1)_Space_O(N) | 76 | 79.40 +| 0153 |[Find Minimum in Rotated Sorted Array](src/main/ruby/g0101_0200/s0153_find_minimum_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_I_Day_12, Udemy_Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 58 | 78.74 +| 0152 |[Maximum Product Subarray](src/main/ruby/g0101_0200/s0152_maximum_product_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Dynamic_Programming_I_Day_6, Level_2_Day_13_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 63 | 80.30 +| 0148 |[Sort List](src/main/ruby/g0101_0200/s0148_sort_list)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Level_2_Day_4_Linked_List, Big_O_Time_O(log(N))_Space_O(log(N)) | 188 | 84.21 +| 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 | 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/g0001_0100/s0079_word_search/readme.md b/src/main/ruby/g0001_0100/s0079_word_search/readme.md index 29cb934..c10a977 100644 --- a/src/main/ruby/g0001_0100/s0079_word_search/readme.md +++ b/src/main/ruby/g0001_0100/s0079_word_search/readme.md @@ -77,7 +77,6 @@ def traverse(board, word, idx, i, j) board[i][j] = '#' - # res = false res = (traverse(board, word, idx + 1, i, j + 1) || traverse(board, word, idx + 1, i, j - 1) || traverse(board, word, idx + 1, i + 1, j) || diff --git a/src/main/ruby/g0101_0200/s0141_linked_list_cycle/readme.md b/src/main/ruby/g0101_0200/s0141_linked_list_cycle/readme.md new file mode 100644 index 0000000..ce2bb68 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0141_linked_list_cycle/readme.md @@ -0,0 +1,72 @@ +[![](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) + +## 141\. Linked List Cycle + +Easy + +Given `head`, the head of a linked list, determine if the linked list has a cycle in it. + +There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to. **Note that `pos` is not passed as a parameter**. + +Return `true` _if there is a cycle in the linked list_. Otherwise, return `false`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png) + +**Input:** head = [3,2,0,-4], pos = 1 + +**Output:** true + +**Explanation:** There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed). + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png) + +**Input:** head = [1,2], pos = 0 + +**Output:** true + +**Explanation:** There is a cycle in the linked list, where the tail connects to the 0th node. + +**Example 3:** + +![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png) + +**Input:** head = [1], pos = -1 + +**Output:** false + +**Explanation:** There is no cycle in the linked list. + +**Constraints:** + +* The number of the nodes in the list is in the range [0, 104]. +* -105 <= Node.val <= 105 +* `pos` is `-1` or a **valid index** in the linked-list. + +**Follow up:** Can you solve it using `O(1)` (i.e. constant) memory? + +## Solution + +```ruby +# @param {ListNode} head +# @return {Boolean} +def hasCycle(head) + return false if head.nil? + + fast = head.next + slow = head + + while fast && fast.next + return true if fast == slow + + fast = fast.next.next + slow = slow.next + end + + false +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0142_linked_list_cycle_ii/readme.md b/src/main/ruby/g0101_0200/s0142_linked_list_cycle_ii/readme.md new file mode 100644 index 0000000..f21a255 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0142_linked_list_cycle_ii/readme.md @@ -0,0 +1,90 @@ +[![](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) + +## 142\. Linked List Cycle II + +Medium + +Given the `head` of a linked list, return _the node where the cycle begins. If there is no cycle, return_ `null`. + +There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to (**0-indexed**). It is `-1` if there is no cycle. **Note that** `pos` **is not passed as a parameter**. + +**Do not modify** the linked list. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png) + +**Input:** head = [3,2,0,-4], pos = 1 + +**Output:** tail connects to node index 1 + +**Explanation:** There is a cycle in the linked list, where tail connects to the second node. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png) + +**Input:** head = [1,2], pos = 0 + +**Output:** tail connects to node index 0 + +**Explanation:** There is a cycle in the linked list, where tail connects to the first node. + +**Example 3:** + +![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png) + +**Input:** head = [1], pos = -1 + +**Output:** no cycle + +**Explanation:** There is no cycle in the linked list. + +**Constraints:** + +* The number of the nodes in the list is in the range [0, 104]. +* -105 <= Node.val <= 105 +* `pos` is `-1` or a **valid index** in the linked-list. + +**Follow up:** Can you solve it using `O(1)` (i.e. constant) memory? + +## Solution + +```ruby +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val) +# @val = val +# @next = nil +# end +# end + +# @param {ListNode} head +# @return {ListNode} +def detectCycle(head) + return nil if head.nil? || head.next.nil? + + slow = head + fast = head + + while fast && fast.next + fast = fast.next.next + slow = slow.next + + # Intersected inside the loop. + break if slow == fast + end + + return nil if fast.nil? || fast.next.nil? + + slow = head + while slow != fast + slow = slow.next + fast = fast.next + end + + slow +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0146_lru_cache/readme.md b/src/main/ruby/g0101_0200/s0146_lru_cache/readme.md new file mode 100644 index 0000000..c927271 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0146_lru_cache/readme.md @@ -0,0 +1,140 @@ +[![](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) + +## 146\. LRU Cache + +Medium + +Design a data structure that follows the constraints of a **[Least Recently Used (LRU) cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)**. + +Implement the `LRUCache` class: + +* `LRUCache(int capacity)` Initialize the LRU cache with **positive** size `capacity`. +* `int get(int key)` Return the value of the `key` if the key exists, otherwise return `-1`. +* `void put(int key, int value)` Update the value of the `key` if the `key` exists. Otherwise, add the `key-value` pair to the cache. If the number of keys exceeds the `capacity` from this operation, **evict** the least recently used key. + +The functions `get` and `put` must each run in `O(1)` average time complexity. + +**Example 1:** + +**Input** ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] + +**Output:** [null, null, null, 1, null, -1, null, -1, 3, 4] + +**Explanation:** + + LRUCache lRUCache = new LRUCache(2); + lRUCache.put(1, 1); // cache is {1=1} + lRUCache.put(2, 2); // cache is {1=1, 2=2} + lRUCache.get(1); // return 1 + lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} + lRUCache.get(2); // returns -1 (not found) + lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} + lRUCache.get(1); // return -1 (not found) + lRUCache.get(3); // return 3 + lRUCache.get(4); // return 4 + +**Constraints:** + +* `1 <= capacity <= 3000` +* 0 <= key <= 104 +* 0 <= value <= 105 +* At most 2 * 105 calls will be made to `get` and `put`. + +## Solution + +```ruby +class LRUCache + class LruCacheNode + attr_accessor :key, :value, :prev, :next + + def initialize(k, v) + @key = k + @value = v + end + end + +=begin + :type capacity: Integer +=end + def initialize(cap) + @capacity = cap + @cache_map = {} + # insert here + @head = nil + # remove here + @tail = nil + end + +=begin + :type key: Integer + :rtype: Integer +=end + def get(key) + val_node = @cache_map[key] + return -1 if val_node.nil? + + move_to_head(val_node) + val_node.value + end + +=begin + :type key: Integer + :type value: Integer + :rtype: Void +=end + def put(key, value) + val_node = @cache_map[key] + + if val_node + val_node.value = value + move_to_head(val_node) + else + if @cache_map.size < @capacity + if @cache_map.empty? + node = LruCacheNode.new(key, value) + @cache_map[key] = node + @head = node + @tail = node + else + node = LruCacheNode.new(key, value) + @cache_map[key] = node + node.next = @head + @head.prev = node + @head = node + end + else + # remove from tail + last = @tail + @tail = last.prev + @tail.next = nil unless @tail.nil? + @cache_map.delete(last.key) + @head = nil if @cache_map.empty? + # Call recursively + put(key, value) + end + end + end + + private + + def move_to_head(node) + return if node == @head + + @tail = node.prev if node == @tail + prev = node.prev + nex = node.next + prev.next = nex + nex.prev = prev unless nex.nil? + node.prev = nil + node.next = @head + @head.prev = node + @head = node + end +end + +# Your LRUCache object will be instantiated and called as such: +# obj = LRUCache.new(capacity) +# param_1 = obj.get(key) +# obj.put(key, value) +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0148_sort_list/readme.md b/src/main/ruby/g0101_0200/s0148_sort_list/readme.md new file mode 100644 index 0000000..7af5bff --- /dev/null +++ b/src/main/ruby/g0101_0200/s0148_sort_list/readme.md @@ -0,0 +1,71 @@ +[![](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) + +## 148\. Sort List + +Medium + +Given the `head` of a linked list, return _the list after sorting it in **ascending order**_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg) + +**Input:** head = [4,2,1,3] + +**Output:** [1,2,3,4] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg) + +**Input:** head = [-1,5,3,4,0] + +**Output:** [-1,0,3,4,5] + +**Example 3:** + +**Input:** head = [] + +**Output:** [] + +**Constraints:** + +* The number of nodes in the list is in the range [0, 5 * 104]. +* -105 <= Node.val <= 105 + +**Follow up:** Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)? + +## Solution + +```ruby +require_relative '../../com_github_leetcode/list_node' + +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val = 0, _next = nil) +# @val = val +# @next = _next +# end +# end +# @param {ListNode} head +# @return {ListNode} +def sort_list(head) + list = [] + new_head = ListNode.new + + while head + list << head.val + head = head.next + end + + curr = new_head + list.sort.each do |v| + curr.next = ListNode.new(v) + curr = curr.next + end + + new_head.next +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0152_maximum_product_subarray/readme.md b/src/main/ruby/g0101_0200/s0152_maximum_product_subarray/readme.md new file mode 100644 index 0000000..c385282 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0152_maximum_product_subarray/readme.md @@ -0,0 +1,51 @@ +[![](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) + +## 152\. Maximum Product Subarray + +Medium + +Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return _the product_. + +It is **guaranteed** that the answer will fit in a **32-bit** integer. + +A **subarray** is a contiguous subsequence of the array. + +**Example 1:** + +**Input:** nums = [2,3,-2,4] + +**Output:** 6 + +**Explanation:** [2,3] has the largest product 6. + +**Example 2:** + +**Input:** nums = [-2,0,-1] + +**Output:** 0 + +**Explanation:** The result cannot be 2, because [-2,-1] is not a subarray. + +**Constraints:** + +* 1 <= nums.length <= 2 * 104 +* `-10 <= nums[i] <= 10` +* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer. + +## Solution + +```ruby +# @param {Integer[]} nums +# @return {Integer} +def max_product(nums) + result = min = max = nums.first + + nums[1..].each do |num| + min, max = [num, min * num, max * num].minmax + result = [result, max].max + end + + result +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/readme.md b/src/main/ruby/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/readme.md new file mode 100644 index 0000000..128947d --- /dev/null +++ b/src/main/ruby/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/readme.md @@ -0,0 +1,78 @@ +[![](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) + +## 153\. Find Minimum in Rotated Sorted Array + +Medium + +Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become: + +* `[4,5,6,7,0,1,2]` if it was rotated `4` times. +* `[0,1,2,4,5,6,7]` if it was rotated `7` times. + +Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`. + +Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_. + +You must write an algorithm that runs in `O(log n) time.` + +**Example 1:** + +**Input:** nums = [3,4,5,1,2] + +**Output:** 1 + +**Explanation:** The original array was [1,2,3,4,5] rotated 3 times. + +**Example 2:** + +**Input:** nums = [4,5,6,7,0,1,2] + +**Output:** 0 + +**Explanation:** The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. + +**Example 3:** + +**Input:** nums = [11,13,15,17] + +**Output:** 11 + +**Explanation:** The original array was [11,13,15,17] and it was rotated 4 times. + +**Constraints:** + +* `n == nums.length` +* `1 <= n <= 5000` +* `-5000 <= nums[i] <= 5000` +* All the integers of `nums` are **unique**. +* `nums` is sorted and rotated between `1` and `n` times. + +## Solution + +```ruby +# @param {Integer[]} nums +# @return {Integer} +def find_min_util(nums, l, r) + return nums[l] if l == r + + mid = (l + r) / 2 + + return nums[l] if mid == l && nums[mid] < nums[r] + return nums[mid] if mid - 1 >= 0 && nums[mid - 1] > nums[mid] + + if nums[mid] < nums[l] + find_min_util(nums, l, mid - 1) + elsif nums[mid] > nums[r] + find_min_util(nums, mid + 1, r) + else + find_min_util(nums, l, mid - 1) + end +end + +def find_min(nums) + l = 0 + r = nums.length - 1 + find_min_util(nums, l, r) +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0155_min_stack/readme.md b/src/main/ruby/g0101_0200/s0155_min_stack/readme.md new file mode 100644 index 0000000..d07c209 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0155_min_stack/readme.md @@ -0,0 +1,103 @@ +[![](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) + +## 155\. Min Stack + +Easy + +Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. + +Implement the `MinStack` class: + +* `MinStack()` initializes the stack object. +* `void push(int val)` pushes the element `val` onto the stack. +* `void pop()` removes the element on the top of the stack. +* `int top()` gets the top element of the stack. +* `int getMin()` retrieves the minimum element in the stack. + +**Example 1:** + +**Input** + + ["MinStack","push","push","push","getMin","pop","top","getMin"] + [[],[-2],[0],[-3],[],[],[],[]] + +**Output:** [null,null,null,null,-3,null,0,-2] + +**Explanation:** + + MinStack minStack = new MinStack(); + minStack.push(-2); + minStack.push(0); + minStack.push(-3); + minStack.getMin(); // return -3 + minStack.pop(); + minStack.top(); // return 0 + minStack.getMin(); // return -2 + +**Constraints:** + +* -231 <= val <= 231 - 1 +* Methods `pop`, `top` and `getMin` operations will always be called on **non-empty** stacks. +* At most 3 * 104 calls will be made to `push`, `pop`, `top`, and `getMin`. + +## Solution + +```ruby +# @param {String} string +# @return {String} +class MinStack + class Node + attr_accessor :min, :data, :previous_node, :next_node + + def initialize(min, data, previous_node, next_node) + @min = min + @data = data + @previous_node = previous_node + @next_node = next_node + end + end + + def initialize + # no initialization needed. + end + +=begin + :type val: Integer + :rtype: Void +=end + def push(val) + if @current_node.nil? + @current_node = Node.new(val, val, nil, nil) + else + @current_node.next_node = Node.new([@current_node.min, val].min, val, @current_node, nil) + @current_node = @current_node.next_node + end + end + +=begin + :rtype: Integer +=end + def pop + @current_node = @current_node.previous_node + end + + def top + @current_node.data + end + +=begin + :rtype: Integer +=end + def get_min + @current_node.min + end +end + +# Your MinStack object will be instantiated and called as such: +# obj = MinStack.new() +# obj.push(val) +# obj.pop() +# param_3 = obj.top() +# param_4 = obj.get_min() +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0160_intersection_of_two_linked_lists/readme.md b/src/main/ruby/g0101_0200/s0160_intersection_of_two_linked_lists/readme.md new file mode 100644 index 0000000..9739199 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0160_intersection_of_two_linked_lists/readme.md @@ -0,0 +1,99 @@ +[![](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) + +## 160\. Intersection of Two Linked Lists + +Easy + +Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`. + +For example, the following two linked lists begin to intersect at node `c1`: + +![](https://assets.leetcode.com/uploads/2021/03/05/160_statement.png) + +The test cases are generated such that there are no cycles anywhere in the entire linked structure. + +**Note** that the linked lists must **retain their original structure** after the function returns. + +**Custom Judge:** + +The inputs to the **judge** are given as follows (your program is **not** given these inputs): + +* `intersectVal` - The value of the node where the intersection occurs. This is `0` if there is no intersected node. +* `listA` - The first linked list. +* `listB` - The second linked list. +* `skipA` - The number of nodes to skip ahead in `listA` (starting from the head) to get to the intersected node. +* `skipB` - The number of nodes to skip ahead in `listB` (starting from the head) to get to the intersected node. + +The judge will then create the linked structure based on these inputs and pass the two heads, `headA` and `headB` to your program. If you correctly return the intersected node, then your solution will be **accepted**. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png) + +**Input:** intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 + +**Output:** Intersected at '8' + +**Explanation:** The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png) + +**Input:** intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 + +**Output:** Intersected at '2' + +**Explanation:** The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B. + +**Example 3:** + +![](https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png) + +**Input:** intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 + +**Output:** No intersection + +**Explanation:** From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values. Explanation: The two lists do not intersect, so return null. + +**Constraints:** + +* The number of nodes of `listA` is in the `m`. +* The number of nodes of `listB` is in the `n`. +* 0 <= m, n <= 3 * 104 +* 1 <= Node.val <= 105 +* `0 <= skipA <= m` +* `0 <= skipB <= n` +* `intersectVal` is `0` if `listA` and `listB` do not intersect. +* `intersectVal == listA[skipA] == listB[skipB]` if `listA` and `listB` intersect. + +**Follow up:** Could you write a solution that runs in `O(n)` time and use only `O(1)` memory? + +## Solution + +```ruby +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val) +# @val = val +# @next = nil +# end +# end + +# @param {ListNode} headA +# @param {ListNode} headB +# @return {ListNode} +def getIntersectionNode(headA, headB) + node1 = headA + node2 = headB + + while node1 != node2 + node1 = node1.nil? ? headB : node1.next + node2 = node2.nil? ? headA : node2.next + end + + node1 +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0169_majority_element/readme.md b/src/main/ruby/g0101_0200/s0169_majority_element/readme.md new file mode 100644 index 0000000..92eb8d4 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0169_majority_element/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) + +## 169\. Majority Element + +Easy + +Given an array `nums` of size `n`, return _the majority element_. + +The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array. + +**Example 1:** + +**Input:** nums = [3,2,3] + +**Output:** 3 + +**Example 2:** + +**Input:** nums = [2,2,1,1,1,2,2] + +**Output:** 2 + +**Constraints:** + +* `n == nums.length` +* 1 <= n <= 5 * 104 +* -231 <= nums[i] <= 231 - 1 + +**Follow-up:** Could you solve the problem in linear time and in `O(1)` space? + +## Solution + +```ruby +# @param {Integer[]} nums +# @return {Integer} +def majority_element(nums) + candidate_count = 0 + candidate_letter = nil + + nums.each_with_object({}) do |num, hash| + hash[num] = 1 + hash[num] ||= 0 + end.each do |k, v| + next if candidate_count >= v + + candidate_letter = k + candidate_count = v + end + + candidate_letter +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0189_rotate_array/readme.md b/src/main/ruby/g0101_0200/s0189_rotate_array/readme.md new file mode 100644 index 0000000..78c308f --- /dev/null +++ b/src/main/ruby/g0101_0200/s0189_rotate_array/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) + +## 189\. Rotate Array + +Medium + +Given an array, rotate the array to the right by `k` steps, where `k` is non-negative. + +**Example 1:** + +**Input:** nums = [1,2,3,4,5,6,7], k = 3 + +**Output:** [5,6,7,1,2,3,4] + +**Explanation:** + + rotate 1 steps to the right: [7,1,2,3,4,5,6] + rotate 2 steps to the right: [6,7,1,2,3,4,5] + rotate 3 steps to the right: [5,6,7,1,2,3,4] + +**Example 2:** + +**Input:** nums = [-1,-100,3,99], k = 2 + +**Output:** [3,99,-1,-100] + +**Explanation:** + + rotate 1 steps to the right: [99,-1,-100,3] + rotate 2 steps to the right: [3,99,-1,-100] + +**Constraints:** + +* 1 <= nums.length <= 105 +* -231 <= nums[i] <= 231 - 1 +* 0 <= k <= 105 + +**Follow up:** + +* Try to come up with as many solutions as you can. There are at least **three** different ways to solve this problem. +* Could you do it in-place with `O(1)` extra space? + +## Solution + +```ruby +# @param {Integer[]} nums +# @param {Integer} k +# @return {Void} Do not return anything, modify nums in-place instead. +def rotate(nums, k) + n = nums.length + t = n - (k % n) + reverse_rotate(nums, 0, t - 1) + reverse_rotate(nums, t, n - 1) + reverse_rotate(nums, 0, n - 1) +end + +def reverse_rotate(nums, l, r) + while l <= r + temp = nums[l] + nums[l] = nums[r] + nums[r] = temp + l += 1 + r -= 1 + end +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0198_house_robber/readme.md b/src/main/ruby/g0101_0200/s0198_house_robber/readme.md new file mode 100644 index 0000000..130c805 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0198_house_robber/readme.md @@ -0,0 +1,59 @@ +[![](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) + +## 198\. House Robber + +Medium + +You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**. + +Given an integer array `nums` representing the amount of money of each house, return _the maximum amount of money you can rob tonight **without alerting the police**_. + +**Example 1:** + +**Input:** nums = [1,2,3,1] + +**Output:** 4 + +**Explanation:** + + Rob house 1 (money = 1) and then rob house 3 (money = 3). + Total amount you can rob = 1 + 3 = 4. + +**Example 2:** + +**Input:** nums = [2,7,9,3,1] + +**Output:** 12 + +**Explanation:** + + Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). + Total amount you can rob = 2 + 9 + 1 = 12. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `0 <= nums[i] <= 400` + +## Solution + +```ruby +# @param {Integer[]} nums +# @return {Integer} +def rob(nums) + return 0 if nums.empty? + return nums[0] if nums.length == 1 + return [nums[0], nums[1]].max if nums.length == 2 + + profit = Array.new(nums.length) + profit[0] = nums[0] + profit[1] = [nums[1], nums[0]].max + + (2...nums.length).each do |i| + profit[i] = [profit[i - 1], nums[i] + profit[i - 2]].max + end + + profit[nums.length - 1] +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0101_0200/s0200_number_of_islands/readme.md b/src/main/ruby/g0101_0200/s0200_number_of_islands/readme.md new file mode 100644 index 0000000..4df58c8 --- /dev/null +++ b/src/main/ruby/g0101_0200/s0200_number_of_islands/readme.md @@ -0,0 +1,76 @@ +[![](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) + +## 200\. Number of Islands + +Medium + +Given an `m x n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return _the number of islands_. + +An **island** is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. + +**Example 1:** + +**Input:** + + grid = [ + ["1","1","1","1","0"], + ["1","1","0","1","0"], + ["1","1","0","0","0"], + ["0","0","0","0","0"] + ] + +**Output:** 1 + +**Example 2:** + +**Input:** + + grid = [ + ["1","1","0","0","0"], + ["1","1","0","0","0"], + ["0","0","1","0","0"], + ["0","0","0","1","1"] + ] + +**Output:** 3 + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `1 <= m, n <= 300` +* `grid[i][j]` is `'0'` or `'1'`. + +## Solution + +```ruby +# @param {Character[][]} grid +# @return {Integer} +def num_islands(grid) + islands = 0 + if grid && !grid.empty? && !grid[0].empty? + (0...grid.length).each do |i| + (0...grid[0].length).each do |j| + if grid[i][j] == '1' + dfs_islands(grid, i, j) + islands += 1 + end + end + end + end + islands +end + +private + +def dfs_islands(grid, x, y) + return if x.negative? || grid.length <= x || y.negative? || grid[0].length <= y || grid[x][y] != '1' + + grid[x][y] = 'x' + dfs_islands(grid, x + 1, y) + dfs_islands(grid, x - 1, y) + dfs_islands(grid, x, y + 1) + dfs_islands(grid, x, y - 1) +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0206_reverse_linked_list/readme.md b/src/main/ruby/g0201_0300/s0206_reverse_linked_list/readme.md new file mode 100644 index 0000000..5275681 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0206_reverse_linked_list/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) + +## 206\. Reverse Linked List + +Easy + +Given the `head` of a singly linked list, reverse the list, and return _the reversed list_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg) + +**Input:** head = [1,2,3,4,5] + +**Output:** [5,4,3,2,1] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg) + +**Input:** head = [1,2] + +**Output:** [2,1] + +**Example 3:** + +**Input:** head = [] + +**Output:** [] + +**Constraints:** + +* The number of nodes in the list is the range `[0, 5000]`. +* `-5000 <= Node.val <= 5000` + +**Follow up:** A linked list can be reversed either iteratively or recursively. Could you implement both? + +## Solution + +```ruby +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val = 0, _next = nil) +# @val = val +# @next = _next +# end +# end +# @param {ListNode} head +# @return {ListNode} +def reverse_list(head) + prev = nil + curr = head + while curr + next_node = curr.next + curr.next = prev + prev = curr + curr = next_node + end + prev +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0207_course_schedule/readme.md b/src/main/ruby/g0201_0300/s0207_course_schedule/readme.md new file mode 100644 index 0000000..bfdbd8c --- /dev/null +++ b/src/main/ruby/g0201_0300/s0207_course_schedule/readme.md @@ -0,0 +1,79 @@ +[![](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) + +## 207\. Course Schedule + +Medium + +There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where prerequisites[i] = [ai, bi] indicates that you **must** take course bi first if you want to take course ai. + +* For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`. + +Return `true` if you can finish all courses. Otherwise, return `false`. + +**Example 1:** + +**Input:** numCourses = 2, prerequisites = \[\[1,0]] + +**Output:** true + +**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible. + +**Example 2:** + +**Input:** numCourses = 2, prerequisites = \[\[1,0],[0,1]] + +**Output:** false + +**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible. + +**Constraints:** + +* 1 <= numCourses <= 105 +* `0 <= prerequisites.length <= 5000` +* `prerequisites[i].length == 2` +* 0 <= ai, bi < numCourses +* All the pairs prerequisites[i] are **unique**. + +## Solution + +```ruby +WHITE = 0 +GRAY = 1 +BLACK = 2 + +# @param {Integer} num_courses +# @param {Integer[][]} prerequisites +# @return {Boolean} +def can_finish(num_courses, prerequisites) + adj = Array.new(num_courses) {[]} + + prerequisites.each do |pre| + adj[pre[1]] << pre[0] + end + + colors = Array.new(num_courses, WHITE) + + (0...num_courses).each do |i| + if colors[i] == WHITE && !adj[i].empty? && has_cycle(adj, i, colors) + return false + end + end + + true +end + +private + +def has_cycle(adj, node, colors) + colors[node] = GRAY + + adj[node].each do |nei| + return true if colors[nei] == GRAY + return true if colors[nei] == WHITE && has_cycle(adj, nei, colors) + end + + colors[node] = BLACK + false +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0208_implement_trie_prefix_tree/readme.md b/src/main/ruby/g0201_0300/s0208_implement_trie_prefix_tree/readme.md new file mode 100644 index 0000000..07a4ac5 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0208_implement_trie_prefix_tree/readme.md @@ -0,0 +1,120 @@ +[![](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) + +## 208\. Implement Trie (Prefix Tree) + +Medium + +A [**trie**](https://en.wikipedia.org/wiki/Trie) (pronounced as "try") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. + +Implement the Trie class: + +* `Trie()` Initializes the trie object. +* `void insert(String word)` Inserts the string `word` into the trie. +* `boolean search(String word)` Returns `true` if the string `word` is in the trie (i.e., was inserted before), and `false` otherwise. +* `boolean startsWith(String prefix)` Returns `true` if there is a previously inserted string `word` that has the prefix `prefix`, and `false` otherwise. + +**Example 1:** + +**Input** + + ["Trie", "insert", "search", "search", "startsWith", "insert", "search"] + [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]] + +**Output:** [null, null, true, false, true, null, true] + +**Explanation:** + + Trie trie = new Trie(); + trie.insert("apple"); trie.search("apple"); // return True + trie.search("app"); // return False + trie.startsWith("app"); // return True + trie.insert("app"); + trie.search("app"); // return True + +**Constraints:** + +* `1 <= word.length, prefix.length <= 2000` +* `word` and `prefix` consist only of lowercase English letters. +* At most 3 * 104 calls **in total** will be made to `insert`, `search`, and `startsWith`. + +## Solution + +```ruby +# @param {String} string +# @return {String} +class Trie + def initialize + @root = TrieNode.new + @start_with = false + end + +=begin + :type word: String + :rtype: Void +=end + def insert(word) + insert_recursive(word, @root, 0) + end + +=begin + :type word: String + :rtype: Boolean +=end + def search(word) + search_recursive(word, @root, 0) + end + +=begin + :type prefix: String + :rtype: Boolean +=end + def starts_with(prefix) + search(prefix) + @start_with + end + + private + + class TrieNode + attr_accessor :children, :is_word + + def initialize + @children = Array.new(26) + @is_word = false + end + end + + def insert_recursive(word, root, idx) + if idx == word.length + root.is_word = true + return + end + + index = word[idx].ord - 'a'.ord + root.children[index] ||= TrieNode.new + insert_recursive(word, root.children[index], idx + 1) + end + + def search_recursive(word, root, idx) + if idx == word.length + @start_with = true + return root.is_word + end + + index = word[idx].ord - 'a'.ord + if root.children[index].nil? + @start_with = false + return false + end + + search_recursive(word, root.children[index], idx + 1) + end +end + +# Your Trie object will be instantiated and called as such: +# obj = Trie.new() +# obj.insert(word) +# param_2 = obj.search(word) +# param_3 = obj.starts_with(prefix) +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md b/src/main/ruby/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md new file mode 100644 index 0000000..2091f94 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md @@ -0,0 +1,40 @@ +[![](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) + +## 215\. Kth Largest Element in an Array + +Medium + +Given an integer array `nums` and an integer `k`, return _the_ kth _largest element in the array_. + +Note that it is the kth largest element in the sorted order, not the kth distinct element. + +**Example 1:** + +**Input:** nums = [3,2,1,5,6,4], k = 2 + +**Output:** 5 + +**Example 2:** + +**Input:** nums = [3,2,3,1,2,4,5,5,6], k = 4 + +**Output:** 4 + +**Constraints:** + +* 1 <= k <= nums.length <= 104 +* -104 <= nums[i] <= 104 + +## Solution + +```ruby +# @param {Integer[]} nums +# @param {Integer} k +# @return {Integer} +def find_kth_largest(nums, k) + n = nums.length + nums.sort! + nums[n - k] +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0221_maximal_square/readme.md b/src/main/ruby/g0201_0300/s0221_maximal_square/readme.md new file mode 100644 index 0000000..aefd96d --- /dev/null +++ b/src/main/ruby/g0201_0300/s0221_maximal_square/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) + +## 221\. Maximal Square + +Medium + +Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_ `1`'s _and return its area_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg) + +**Input:** matrix = \[\["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] + +**Output:** 4 + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg) + +**Input:** matrix = \[\["0","1"],["1","0"]] + +**Output:** 1 + +**Example 3:** + +**Input:** matrix = \[\["0"]] + +**Output:** 0 + +**Constraints:** + +* `m == matrix.length` +* `n == matrix[i].length` +* `1 <= m, n <= 300` +* `matrix[i][j]` is `'0'` or `'1'`. + +## Solution + +```ruby +# @param {Character[][]} matrix +# @return {Integer} +def maximal_square(matrix) + m = matrix.length + return 0 if m == 0 + + n = matrix[0].length + return 0 if n == 0 + + dp = Array.new(m + 1) {Array.new(n + 1, 0)} + max = 0 + + (0...m).each do |i| + (0...n).each do |j| + if matrix[i][j] == '1' + # 1 + minimum from cell above, cell to the left, cell diagonal upper-left + next_val = 1 + [dp[i][j], [dp[i + 1][j], dp[i][j + 1]].min].min + # keep track of the maximum value seen + max = [max, next_val].max + dp[i + 1][j + 1] = next_val + end + end + end + + max * max +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0226_invert_binary_tree/readme.md b/src/main/ruby/g0201_0300/s0226_invert_binary_tree/readme.md new file mode 100644 index 0000000..4a7a939 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0226_invert_binary_tree/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) + +## 226\. Invert Binary Tree + +Easy + +Given the `root` of a binary tree, invert the tree, and return _its root_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg) + +**Input:** root = [4,2,7,1,3,6,9] + +**Output:** [4,7,2,9,6,3,1] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/03/14/invert2-tree.jpg) + +**Input:** root = [2,1,3] + +**Output:** [2,3,1] + +**Example 3:** + +**Input:** root = [] + +**Output:** [] + +**Constraints:** + +* The number of nodes in the tree is in the range `[0, 100]`. +* `-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 {TreeNode} +def invert_tree(root) + return nil if root.nil? + + temp = root.left + root.left = invert_tree(root.right) + root.right = invert_tree(temp) + + root +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md b/src/main/ruby/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md new file mode 100644 index 0000000..9bd5671 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0230_kth_smallest_element_in_a_bst/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) + +## 230\. Kth Smallest Element in a BST + +Medium + +Given the `root` of a binary search tree, and an integer `k`, return _the_ kth _smallest value (**1-indexed**) of all the values of the nodes in the tree_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg) + +**Input:** root = [3,1,4,null,2], k = 1 + +**Output:** 1 + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg) + +**Input:** root = [5,3,6,2,4,null,null,1], k = 3 + +**Output:** 3 + +**Constraints:** + +* The number of nodes in the tree is `n`. +* 1 <= k <= n <= 104 +* 0 <= Node.val <= 104 + +**Follow up:** If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize? + +## 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} k +# @return {Integer} +def kth_smallest(root, k) + @k = k + @count = 0 + @val = 0 + calculate(root) + @val +end + +private + +def calculate(node) + return unless node + + if node.left.nil? && node.right.nil? + @count += 1 + @val = node.val if @count == @k + return + end + + calculate(node.left) if node.left + @count += 1 + @val = node.val if @count == @k + return if @count == @k + + calculate(node.right) if node.right +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0234_palindrome_linked_list/readme.md b/src/main/ruby/g0201_0300/s0234_palindrome_linked_list/readme.md new file mode 100644 index 0000000..1862d95 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0234_palindrome_linked_list/readme.md @@ -0,0 +1,80 @@ +[![](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) + +## 234\. Palindrome Linked List + +Easy + +Given the `head` of a singly linked list, return `true` if it is a palindrome. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg) + +**Input:** head = [1,2,2,1] + +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg) + +**Input:** head = [1,2] + +**Output:** false + +**Constraints:** + +* The number of nodes in the list is in the range [1, 105]. +* `0 <= Node.val <= 9` + +**Follow up:** Could you do it in `O(n)` time and `O(1)` space? + +## Solution + +```ruby +require_relative '../../com_github_leetcode/list_node' + +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val = 0, _next = nil) +# @val = val +# @next = _next +# end +# end +# @param {ListNode} head +# @return {Boolean} +def is_palindrome2(head) + # find mid, reverse second half of list, compare the nodes of 'two' lists + slow = fast = head + while fast && fast.next do + slow = slow.next + fast = fast.next.next + end + mid_head = slow + + curr = mid_head + prev = nil + while curr do + next_node = curr.next + curr.next = prev + prev = curr + curr = next_node + end + prev + + curr = head + mid_curr= prev + while curr && mid_curr do + if curr.val != mid_curr.val + return false + end + + curr = curr.next + mid_curr = mid_curr.next + end + + true +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md b/src/main/ruby/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md new file mode 100644 index 0000000..3a8965d --- /dev/null +++ b/src/main/ruby/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/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) + +## 236\. Lowest Common Ancestor of a Binary Tree + +Medium + +Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. + +According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/Lowest_common_ancestor): “The lowest common ancestor is defined between two nodes `p` and `q` as the lowest node in `T` that has both `p` and `q` as descendants (where we allow **a node to be a descendant of itself**).” + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png) + +**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 + +**Output:** 3 + +**Explanation:** The LCA of nodes 5 and 1 is 3. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png) + +**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 + +**Output:** 5 + +**Explanation:** The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition. + +**Example 3:** + +**Input:** root = [1,2], p = 1, q = 2 + +**Output:** 1 + +**Constraints:** + +* The number of nodes in the tree is in the range [2, 105]. +* -109 <= Node.val <= 109 +* All `Node.val` are **unique**. +* `p != q` +* `p` and `q` will exist in the tree. + +## Solution + +```ruby +# Definition for a binary tree node. +# class TreeNode +# attr_accessor :val, :left, :right +# def initialize(val) +# @val = val +# @left, @right = nil, nil +# end +# end + +# @param {TreeNode} root +# @param {TreeNode} p +# @param {TreeNode} q +# @return {TreeNode} +def lowest_common_ancestor(root, p, q) + return nil if root.nil? + + return root if root.val == p.val || root.val == q.val + + left = lowest_common_ancestor(root.left, p, q) + right = lowest_common_ancestor(root.right, p, q) + + return root if left && right + return left if left + right +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0238_product_of_array_except_self/readme.md b/src/main/ruby/g0201_0300/s0238_product_of_array_except_self/readme.md new file mode 100644 index 0000000..e4fd34a --- /dev/null +++ b/src/main/ruby/g0201_0300/s0238_product_of_array_except_self/readme.md @@ -0,0 +1,57 @@ +[![](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) + +## 238\. Product of Array Except Self + +Medium + +Given an integer array `nums`, return _an array_ `answer` _such that_ `answer[i]` _is equal to the product of all the elements of_ `nums` _except_ `nums[i]`. + +The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer. + +You must write an algorithm that runs in `O(n)` time and without using the division operation. + +**Example 1:** + +**Input:** nums = [1,2,3,4] + +**Output:** [24,12,8,6] + +**Example 2:** + +**Input:** nums = [-1,1,0,-3,3] + +**Output:** [0,0,9,0,0] + +**Constraints:** + +* 2 <= nums.length <= 105 +* `-30 <= nums[i] <= 30` +* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer. + +**Follow up:** Can you solve the problem in `O(1) `extra space complexity? (The output array **does not** count as extra space for space complexity analysis.) + +## Solution + +```ruby +# @param {Integer[]} nums +# @return {Integer[]} +def product_except_self(nums) + product = 1 + ans = Array.new(nums.length, 1) + + nums.each {|num| product *= num} + + nums.each_with_index do |num, i| + if num != 0 + ans[i] = product / num + else + p = 1 + nums.each_with_index {|n, j| p *= n unless j == i} + ans[i] = p + end + end + + ans +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0239_sliding_window_maximum/readme.md b/src/main/ruby/g0201_0300/s0239_sliding_window_maximum/readme.md new file mode 100644 index 0000000..f64e635 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0239_sliding_window_maximum/readme.md @@ -0,0 +1,96 @@ +[![](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) + +## 239\. Sliding Window Maximum + +Hard + +You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position. + +Return _the max sliding window_. + +**Example 1:** + +**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3 + +**Output:** [3,3,5,5,6,7] + +**Explanation:** + + Window position Max + --------------- ----- + [1 3 -1] -3 5 3 6 7 3 + 1 [3 -1 -3] 5 3 6 7 3 + 1 3 [-1 -3 5] 3 6 7 5 + 1 3 -1 [-3 5 3] 6 7 5 + 1 3 -1 -3 [5 3 6] 7 6 + 1 3 -1 -3 5 [3 6 7] 7 + +**Example 2:** + +**Input:** nums = [1], k = 1 + +**Output:** [1] + +**Example 3:** + +**Input:** nums = [1,-1], k = 1 + +**Output:** [1,-1] + +**Example 4:** + +**Input:** nums = [9,11], k = 2 + +**Output:** [11] + +**Example 5:** + +**Input:** nums = [4,-2], k = 2 + +**Output:** [4] + +**Constraints:** + +* 1 <= nums.length <= 105 +* -104 <= nums[i] <= 104 +* `1 <= k <= nums.length` + +## Solution + +```ruby +# @param {Integer[]} nums +# @param {Integer} k +# @return {Integer[]} +def max_sliding_window(nums, k) + n = nums.length + res = [] + x = 0 + dq = [] + i = 0 + j = 0 + + while j < n + while !dq.empty? && dq.last < nums[j] + dq.pop + end + + dq.push(nums[j]) + + if j - i + 1 == k + res[x] = dq.first + x += 1 + + if dq.first == nums[i] + dq.shift + end + + i += 1 + end + + j += 1 + end + + res +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md b/src/main/ruby/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md new file mode 100644 index 0000000..3028cbe --- /dev/null +++ b/src/main/ruby/g0201_0300/s0240_search_a_2d_matrix_ii/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) + +## 240\. Search a 2D Matrix II + +Medium + +Write an efficient algorithm that searches for a `target` value in an `m x n` integer `matrix`. The `matrix` has the following properties: + +* Integers in each row are sorted in ascending from left to right. +* Integers in each column are sorted in ascending from top to bottom. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg) + +**Input:** matrix = \[\[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5 + +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg) + +**Input:** matrix = \[\[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20 + +**Output:** false + +**Constraints:** + +* `m == matrix.length` +* `n == matrix[i].length` +* `1 <= n, m <= 300` +* -109 <= matrix[i][j] <= 109 +* All the integers in each row are **sorted** in ascending order. +* All the integers in each column are **sorted** in ascending order. +* -109 <= target <= 109 + +## Solution + +```ruby +# @param {Integer[][]} matrix +# @param {Integer} target +# @return {Boolean} +def search_matrix(matrix, target) + return false if matrix.empty? || matrix[0].empty? + + r = 0 + c = matrix[0].length - 1 + + while r < matrix.length && c >= 0 + if matrix[r][c] == target + return true + elsif matrix[r][c] > target + c -= 1 + else + r += 1 + end + end + + false +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0283_move_zeroes/readme.md b/src/main/ruby/g0201_0300/s0283_move_zeroes/readme.md new file mode 100644 index 0000000..e498123 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0283_move_zeroes/readme.md @@ -0,0 +1,54 @@ +[![](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) + +## 283\. Move Zeroes + +Easy + +Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements. + +**Note** that you must do this in-place without making a copy of the array. + +**Example 1:** + +**Input:** nums = [0,1,0,3,12] + +**Output:** [1,3,12,0,0] + +**Example 2:** + +**Input:** nums = [0] + +**Output:** [0] + +**Constraints:** + +* 1 <= nums.length <= 104 +* -231 <= nums[i] <= 231 - 1 + +**Follow up:** Could you minimize the total number of operations done? + +## Solution + +```ruby +# @param {Integer[]} nums +# @return {Void} Do not return anything, modify nums in-place instead. +def move_zeroes(nums) + first_zero = 0 + + (0...nums.length).each do |i| + if nums[i] != 0 + swap(first_zero, i, nums) + first_zero += 1 + end + end +end + +private + +def swap(index1, index2, numbers) + val2 = numbers[index2] + numbers[index2] = numbers[index1] + numbers[index1] = val2 +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0287_find_the_duplicate_number/readme.md b/src/main/ruby/g0201_0300/s0287_find_the_duplicate_number/readme.md new file mode 100644 index 0000000..a90a986 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0287_find_the_duplicate_number/readme.md @@ -0,0 +1,65 @@ +[![](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) + +## 287\. Find the Duplicate Number + +Medium + +Given an array of integers `nums` containing `n + 1` integers where each integer is in the range `[1, n]` inclusive. + +There is only **one repeated number** in `nums`, return _this repeated number_. + +You must solve the problem **without** modifying the array `nums` and uses only constant extra space. + +**Example 1:** + +**Input:** nums = [1,3,4,2,2] + +**Output:** 2 + +**Example 2:** + +**Input:** nums = [3,1,3,4,2] + +**Output:** 3 + +**Example 3:** + +**Input:** nums = [1,1] + +**Output:** 1 + +**Example 4:** + +**Input:** nums = [1,1,2] + +**Output:** 1 + +**Constraints:** + +* 1 <= n <= 105 +* `nums.length == n + 1` +* `1 <= nums[i] <= n` +* All the integers in `nums` appear only **once** except for **precisely one integer** which appears **two or more** times. + +**Follow up:** + +* How can we prove that at least one duplicate number must exist in `nums`? +* Can you solve the problem in linear runtime complexity? + +## Solution + +```ruby +# @param {Integer[]} nums +# @return {Integer} +def find_duplicate(nums) + arr = Array.new(nums.length + 1, 0) + + nums.each do |num| + arr[num] += 1 + return num if arr[num] == 2 + end + + 0 +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0295_find_median_from_data_stream/readme.md b/src/main/ruby/g0201_0300/s0295_find_median_from_data_stream/readme.md new file mode 100644 index 0000000..976a394 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0295_find_median_from_data_stream/readme.md @@ -0,0 +1,90 @@ +[![](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) + +## 295\. Find Median from Data Stream + +Hard + +The **median** is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values. + +* For example, for `arr = [2,3,4]`, the median is `3`. +* For example, for `arr = [2,3]`, the median is `(2 + 3) / 2 = 2.5`. + +Implement the MedianFinder class: + +* `MedianFinder()` initializes the `MedianFinder` object. +* `void addNum(int num)` adds the integer `num` from the data stream to the data structure. +* `double findMedian()` returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted. + +**Example 1:** + +**Input** + + ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"] + [[], [1], [2], [], [3], []] + +**Output:** [null, null, null, 1.5, null, 2.0] + +**Explanation:** + + MedianFinder medianFinder = new MedianFinder(); + medianFinder.addNum(1); // arr = [1] + medianFinder.addNum(2); // arr = [1, 2] + medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2) + medianFinder.addNum(3); // arr[1, 2, 3] + medianFinder.findMedian(); // return 2.0 + +**Constraints:** + +* -105 <= num <= 105 +* There will be at least one element in the data structure before calling `findMedian`. +* At most 5 * 104 calls will be made to `addNum` and `findMedian`. + +**Follow up:** + +* If all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution? +* If `99%` of all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution? + +## Solution + +```ruby +class MedianFinder + def initialize() + @arr = [] + end + +=begin + :type num: Integer + :rtype: Void +=end + def add_num(num) + placement = @arr.bsearch_index {|a| num < a} + + if placement.nil? + @arr << num + elsif placement == 0 + @arr.unshift(num) + else + @arr.insert(placement, num) + end + end + +=begin + :rtype: Float +=end + def find_median() + if @arr.size % 2 == 0 + mid = @arr.size / 2 + (@arr[mid] + @arr[mid - 1]).fdiv(2) + else + mid = @arr.size / 2 + @arr[mid] + end + end +end + +# Your MedianFinder object will be instantiated and called as such: +# obj = MedianFinder.new() +# obj.add_num(num) +# param_2 = obj.find_median() +``` \ No newline at end of file diff --git a/src/main/ruby/g0201_0300/s0300_longest_increasing_subsequence/readme.md b/src/main/ruby/g0201_0300/s0300_longest_increasing_subsequence/readme.md new file mode 100644 index 0000000..22dcf50 --- /dev/null +++ b/src/main/ruby/g0201_0300/s0300_longest_increasing_subsequence/readme.md @@ -0,0 +1,80 @@ +[![](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) + +## 300\. Longest Increasing Subsequence + +Medium + +Given an integer array `nums`, return the length of the longest strictly increasing subsequence. + +A **subsequence** is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, `[3,6,2,7]` is a subsequence of the array `[0,3,1,6,2,2,7]`. + +**Example 1:** + +**Input:** nums = [10,9,2,5,3,7,101,18] + +**Output:** 4 + +**Explanation:** The longest increasing subsequence is [2,3,7,101], therefore the length is 4. + +**Example 2:** + +**Input:** nums = [0,1,0,3,2,3] + +**Output:** 4 + +**Example 3:** + +**Input:** nums = [7,7,7,7,7,7,7] + +**Output:** 1 + +**Constraints:** + +* `1 <= nums.length <= 2500` +* -104 <= nums[i] <= 104 + +**Follow up:** Can you come up with an algorithm that runs in `O(n log(n))` time complexity? + +## Solution + +```ruby +# @param {Integer[]} nums +# @return {Integer} +def length_of_lis(nums) + return 0 if nums.nil? || nums.empty? + + dp = Array.new(nums.length + 1, Float::INFINITY) + + left = 1 + right = 1 + + nums.each do |curr| + start_idx = left + end_idx = right + + # Binary search: find the index where dp[index] is lower than curr + while start_idx + 1 < end_idx + mid_idx = start_idx + (end_idx - start_idx) / 2 + + if dp[mid_idx] > curr + end_idx = mid_idx + else + start_idx = mid_idx + end + end + + # Update the dp table + if dp[start_idx] > curr + dp[start_idx] = curr + elsif curr > dp[start_idx] && curr < dp[end_idx] + dp[end_idx] = curr + elsif curr > dp[end_idx] + dp[end_idx + 1] = curr + right += 1 + end + end + + right +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0301_0400/s0322_coin_change/readme.md b/src/main/ruby/g0301_0400/s0322_coin_change/readme.md new file mode 100644 index 0000000..c83c40d --- /dev/null +++ b/src/main/ruby/g0301_0400/s0322_coin_change/readme.md @@ -0,0 +1,65 @@ +[![](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) + +## 322\. Coin Change + +Medium + +You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money. + +Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`. + +You may assume that you have an infinite number of each kind of coin. + +**Example 1:** + +**Input:** coins = [1,2,5], amount = 11 + +**Output:** 3 + +**Explanation:** 11 = 5 + 5 + 1 + +**Example 2:** + +**Input:** coins = [2], amount = 3 + +**Output:** -1 + +**Example 3:** + +**Input:** coins = [1], amount = 0 + +**Output:** 0 + +**Constraints:** + +* `1 <= coins.length <= 12` +* 1 <= coins[i] <= 231 - 1 +* 0 <= amount <= 104 + +## Solution + +```ruby +# @param {Integer[]} coins +# @param {Integer} amount +# @return {Integer} +def coin_change(coins, amount) + dp = Array.new(amount + 1, 0) + dp[0] = 1 + + coins.each do |coin| + (coin..amount).each do |i| + prev = dp[i - coin] + if prev.positive? + if dp[i].zero? + dp[i] = prev + 1 + else + dp[i] = [dp[i], prev + 1].min + end + end + end + end + + dp[amount] - 1 +end +``` \ No newline at end of file diff --git a/src/main/ruby/g0301_0400/s0338_counting_bits/readme.md b/src/main/ruby/g0301_0400/s0338_counting_bits/readme.md new file mode 100644 index 0000000..5bfdb71 --- /dev/null +++ b/src/main/ruby/g0301_0400/s0338_counting_bits/readme.md @@ -0,0 +1,70 @@ +[![](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) + +## 338\. Counting Bits + +Easy + +Given an integer `n`, return _an array_ `ans` _of length_ `n + 1` _such that for each_ `i` (`0 <= i <= n`)_,_ `ans[i]` _is the **number of**_ `1`_**'s** in the binary representation of_ `i`. + +**Example 1:** + +**Input:** n = 2 + +**Output:** [0,1,1] + +**Explanation:** + + 0 --> 0 + 1 --> 1 + 2 --> 10 + +**Example 2:** + +**Input:** n = 5 + +**Output:** [0,1,1,2,1,2] + +**Explanation:** + + 0 --> 0 + 1 --> 1 + 2 --> 10 + 3 --> 11 + 4 --> 100 + 5 --> 101 + +**Constraints:** + +* 0 <= n <= 105 + +**Follow up:** + +* It is very easy to come up with a solution with a runtime of `O(n log n)`. Can you do it in linear time `O(n)` and possibly in a single pass? +* Can you do it without using any built-in function (i.e., like `__builtin_popcount` in C++)? + +## Solution + +```ruby +# @param {Integer} num +# @return {Integer[]} +def count_bits(num) + result = Array.new(num + 1, 0) + border_pos = 1 + incr_pos = 1 + + (1..num).each do |i| + # when we reach pow of 2, reset border_pos and incr_pos + if incr_pos == border_pos + result[i] = 1 + incr_pos = 1 + border_pos = i + else + result[i] = 1 + result[incr_pos] + incr_pos += 1 + end + end + + result +end +``` \ No newline at end of file