From 93dd3ed6b3ababd08eef2c0c7cb3434b8a49d290 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov <javadev75@gmail.com> Date: Thu, 21 Dec 2023 19:45:58 +0200 Subject: [PATCH] Added tasks 169-226 --- README.md | 33 +++++++ .../s0169_majority_element/Solution.php | 23 +++++ .../s0169_majority_element/readme.md | 27 ++++++ .../s0189_rotate_array/Solution.php | 32 +++++++ .../g0101_0200/s0189_rotate_array/readme.md | 39 +++++++++ .../s0198_house_robber/Solution.php | 34 ++++++++ .../g0101_0200/s0198_house_robber/readme.md | 34 ++++++++ .../s0200_number_of_islands/Solution.php | 41 +++++++++ .../s0200_number_of_islands/readme.md | 40 +++++++++ .../s0206_reverse_linked_list/Solution.php | 37 ++++++++ .../s0206_reverse_linked_list/readme.md | 34 ++++++++ .../s0207_course_schedule/Solution.php | 46 ++++++++++ .../s0207_course_schedule/readme.md | 33 +++++++ .../s0208_implement_trie_prefix_tree/Trie.php | 85 +++++++++++++++++++ .../readme.md | 36 ++++++++ .../Solution.php | 20 +++++ .../readme.md | 24 ++++++ .../s0221_maximal_square/Solution.php | 38 +++++++++ .../g0201_0300/s0221_maximal_square/readme.md | 34 ++++++++ .../s0226_invert_binary_tree/Solution.php | 36 ++++++++ .../s0226_invert_binary_tree/readme.md | 32 +++++++ .../s0169_majority_element/SolutionTest.php | 15 ++++ .../s0189_rotate_array/SolutionTest.php | 19 +++++ .../s0198_house_robber/SolutionTest.php | 15 ++++ .../s0200_number_of_islands/SolutionTest.php | 25 ++++++ .../SolutionTest.php | 27 ++++++ .../s0207_course_schedule/SolutionTest.php | 15 ++++ .../TrieTest.php | 17 ++++ .../SolutionTest.php | 15 ++++ .../s0221_maximal_square/SolutionTest.php | 27 ++++++ .../s0226_invert_binary_tree/SolutionTest.php | 18 ++++ 31 files changed, 951 insertions(+) create mode 100644 src/main/php/g0101_0200/s0169_majority_element/Solution.php create mode 100644 src/main/php/g0101_0200/s0169_majority_element/readme.md create mode 100644 src/main/php/g0101_0200/s0189_rotate_array/Solution.php create mode 100644 src/main/php/g0101_0200/s0189_rotate_array/readme.md create mode 100644 src/main/php/g0101_0200/s0198_house_robber/Solution.php create mode 100644 src/main/php/g0101_0200/s0198_house_robber/readme.md create mode 100644 src/main/php/g0101_0200/s0200_number_of_islands/Solution.php create mode 100644 src/main/php/g0101_0200/s0200_number_of_islands/readme.md create mode 100644 src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php create mode 100644 src/main/php/g0201_0300/s0206_reverse_linked_list/readme.md create mode 100644 src/main/php/g0201_0300/s0207_course_schedule/Solution.php create mode 100644 src/main/php/g0201_0300/s0207_course_schedule/readme.md create mode 100644 src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/Trie.php create mode 100644 src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/readme.md create mode 100644 src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.php create mode 100644 src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md create mode 100644 src/main/php/g0201_0300/s0221_maximal_square/Solution.php create mode 100644 src/main/php/g0201_0300/s0221_maximal_square/readme.md create mode 100644 src/main/php/g0201_0300/s0226_invert_binary_tree/Solution.php create mode 100644 src/main/php/g0201_0300/s0226_invert_binary_tree/readme.md create mode 100644 src/test/php/g0101_0200/s0169_majority_element/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0189_rotate_array/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0198_house_robber/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0200_number_of_islands/SolutionTest.php create mode 100644 src/test/php/g0201_0300/s0206_reverse_linked_list/SolutionTest.php create mode 100644 src/test/php/g0201_0300/s0207_course_schedule/SolutionTest.php create mode 100644 src/test/php/g0201_0300/s0208_implement_trie_prefix_tree/TrieTest.php create mode 100644 src/test/php/g0201_0300/s0215_kth_largest_element_in_an_array/SolutionTest.php create mode 100644 src/test/php/g0201_0300/s0221_maximal_square/SolutionTest.php create mode 100644 src/test/php/g0201_0300/s0226_invert_binary_tree/SolutionTest.php diff --git a/README.md b/README.md index fab7185..94f91a0 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0198 |[House Robber](src/main/php/g0101_0200/s0198_house_robber/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 4 | 64.29 #### Day 4 @@ -287,6 +288,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- | 0064 |[Minimum Path Sum](src/main/php/g0001_0100/s0064_minimum_path_sum/Solution.php)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 20 | 81.48 +| 0221 |[Maximal Square](src/main/php/g0201_0300/s0221_maximal_square/Solution.php)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 209 | 83.33 #### Day 17 @@ -491,6 +493,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0200 |[Number of Islands](src/main/php/g0101_0200/s0200_number_of_islands/Solution.php)| 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) | 97 | 82.05 #### Day 2 Matrix Related Problems @@ -626,6 +629,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- | 0021 |[Merge Two Sorted Lists](src/main/php/g0001_0100/s0021_merge_two_sorted_lists/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 3 | 90.57 +| 0206 |[Reverse Linked List](src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00 #### Day 4 Linked List @@ -660,6 +664,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0200 |[Number of Islands](src/main/php/g0101_0200/s0200_number_of_islands/Solution.php)| 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) | 97 | 82.05 #### Day 10 Dynamic Programming @@ -727,6 +732,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0226 |[Invert Binary Tree](src/main/php/g0201_0300/s0226_invert_binary_tree/Solution.php)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 6 | 62.79 #### Day 7 Tree @@ -759,6 +765,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0198 |[House Robber](src/main/php/g0101_0200/s0198_house_robber/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 4 | 64.29 #### Day 13 Dynamic Programming @@ -784,6 +791,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- | 0155 |[Min Stack](src/main/php/g0101_0200/s0155_min_stack/MinStack.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 19 | 100.00 +| 0208 |[Implement Trie (Prefix Tree)](src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/Trie.php)| 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) | 83 | 46.67 #### Day 17 Interval @@ -840,6 +848,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. |-|-|-|-|-|- | 0121 |[Best Time to Buy and Sell Stock](src/main/php/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 282 | 43.99 | 0001 |[Two Sum](src/main/php/g0001_0100/s0001_two_sum/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 9 | 97.47 +| 0189 |[Rotate Array](src/main/php/g0101_0200/s0189_rotate_array/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 60 | 67.03 | 0055 |[Jump Game](src/main/php/g0001_0100/s0055_jump_game/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 129 | 74.56 | 0075 |[Sort Colors](src/main/php/g0001_0100/s0075_sort_colors/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 3 | 88.78 | 0041 |[First Missing Positive](src/main/php/g0001_0100/s0041_first_missing_positive/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 111 | 90.48 @@ -856,6 +865,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- | 0053 |[Maximum Subarray](src/main/php/g0001_0100/s0053_maximum_subarray/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 185 | 95.51 +| 0169 |[Majority Element](src/main/php/g0101_0200/s0169_majority_element/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 43 | 72.03 #### Udemy Sorting Algorithms @@ -879,6 +889,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | 0024 |[Swap Nodes in Pairs](src/main/php/g0001_0100/s0024_swap_nodes_in_pairs/Solution.php)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 3 | 85.71 | 0142 |[Linked List Cycle II](src/main/php/g0101_0200/s0142_linked_list_cycle_ii/Solution.php)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 7 | 100.00 | 0141 |[Linked List Cycle](src/main/php/g0101_0200/s0141_linked_list_cycle/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 8 | 90.60 +| 0206 |[Reverse Linked List](src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00 | 0021 |[Merge Two Sorted Lists](src/main/php/g0001_0100/s0021_merge_two_sorted_lists/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 3 | 90.57 | 0160 |[Intersection of Two Linked Lists](src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 30 | 81.67 | 0138 |[Copy List with Random Pointer](src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 2300 | 60.00 @@ -891,6 +902,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. |-|-|-|-|-|- | 0094 |[Binary Tree Inorder Traversal](src/main/php/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 3 | 82.09 | 0102 |[Binary Tree Level Order Traversal](src/main/php/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 4 | 96.08 +| 0226 |[Invert Binary Tree](src/main/php/g0201_0300/s0226_invert_binary_tree/Solution.php)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 6 | 62.79 | 0104 |[Maximum Depth of Binary Tree](src/main/php/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.php)| 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) | 9 | 63.06 | 0124 |[Binary Tree Maximum Path Sum](src/main/php/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 16 | 100.00 | 0098 |[Validate Binary Search Tree](src/main/php/g0001_0100/s0098_validate_binary_search_tree/Solution.php)| 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)) | 10 | 70.97 @@ -899,11 +911,13 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0208 |[Implement Trie (Prefix Tree)](src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/Trie.php)| 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) | 83 | 46.67 #### Udemy Graph | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0200 |[Number of Islands](src/main/php/g0101_0200/s0200_number_of_islands/Solution.php)| 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) | 97 | 82.05 #### Udemy Dynamic Programming @@ -911,6 +925,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. |-|-|-|-|-|- | 0139 |[Word Break](src/main/php/g0101_0200/s0139_word_break/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 7 | 75.00 | 0152 |[Maximum Product Subarray](src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 15 | 92.86 +| 0198 |[House Robber](src/main/php/g0101_0200/s0198_house_robber/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 4 | 64.29 | 0070 |[Climbing Stairs](src/main/php/g0001_0100/s0070_climbing_stairs/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 3 | 82.81 | 0064 |[Minimum Path Sum](src/main/php/g0001_0100/s0064_minimum_path_sum/Solution.php)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 20 | 81.48 | 0072 |[Edit Distance](src/main/php/g0001_0100/s0072_edit_distance/Solution.php)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 29 | 75.00 @@ -984,6 +999,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0206 |[Reverse Linked List](src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00 #### Day 9 Stack Queue @@ -1009,6 +1025,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0226 |[Invert Binary Tree](src/main/php/g0201_0300/s0226_invert_binary_tree/Solution.php)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 6 | 62.79 #### Day 13 Tree @@ -1028,6 +1045,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- | 0136 |[Single Number](src/main/php/g0101_0200/s0136_single_number/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 33 | 80.08 +| 0169 |[Majority Element](src/main/php/g0101_0200/s0169_majority_element/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 43 | 72.03 | 0015 |[3Sum](src/main/php/g0001_0100/s0015_3sum/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1), Big_O_Time_O(n\*log(n))_Space_O(n^2) | 252 | 77.94 #### Day 2 Array @@ -1136,6 +1154,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0215 |[Kth Largest Element in an Array](src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.php)| 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)) | 222 | 49.15 #### Day 21 Heap Priority Queue @@ -1154,6 +1173,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0189 |[Rotate Array](src/main/php/g0101_0200/s0189_rotate_array/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 60 | 67.03 #### Day 3 Two Pointers @@ -1197,6 +1217,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- | 0021 |[Merge Two Sorted Lists](src/main/php/g0001_0100/s0021_merge_two_sorted_lists/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 3 | 90.57 +| 0206 |[Reverse Linked List](src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00 #### Day 11 Recursion Backtracking @@ -1209,6 +1230,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- | 0070 |[Climbing Stairs](src/main/php/g0001_0100/s0070_climbing_stairs/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 3 | 82.81 +| 0198 |[House Robber](src/main/php/g0101_0200/s0198_house_robber/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 4 | 64.29 #### Day 13 Bit Manipulation @@ -1258,6 +1280,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> |-|-|-|-|-|- +| 0200 |[Number of Islands](src/main/php/g0101_0200/s0200_number_of_islands/Solution.php)| 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) | 97 | 82.05 #### Day 7 Breadth First Search Depth First Search @@ -1349,6 +1372,16 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|--------- +| 0226 |[Invert Binary Tree](src/main/php/g0201_0300/s0226_invert_binary_tree/Solution.php)| 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) | 6 | 62.79 +| 0221 |[Maximal Square](src/main/php/g0201_0300/s0221_maximal_square/Solution.php)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Big_O_Time_O(m\*n)_Space_O(m\*n) | 209 | 83.33 +| 0215 |[Kth Largest Element in an Array](src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.php)| 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)) | 222 | 49.15 +| 0208 |[Implement Trie (Prefix Tree)](src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/Trie.php)| 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) | 83 | 46.67 +| 0207 |[Course Schedule](src/main/php/g0201_0300/s0207_course_schedule/Solution.php)| 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) | 25 | 89.29 +| 0206 |[Reverse Linked List](src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php)| 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) | 0 | 100.00 +| 0200 |[Number of Islands](src/main/php/g0101_0200/s0200_number_of_islands/Solution.php)| 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) | 97 | 82.05 +| 0198 |[House Robber](src/main/php/g0101_0200/s0198_house_robber/Solution.php)| 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) | 4 | 64.29 +| 0189 |[Rotate Array](src/main/php/g0101_0200/s0189_rotate_array/Solution.php)| 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) | 60 | 67.03 +| 0169 |[Majority Element](src/main/php/g0101_0200/s0169_majority_element/Solution.php)| 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) | 43 | 72.03 | 0160 |[Intersection of Two Linked Lists](src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.php)| 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) | 30 | 81.67 | 0155 |[Min Stack](src/main/php/g0101_0200/s0155_min_stack/MinStack.php)| 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) | 19 | 100.00 | 0153 |[Find Minimum in Rotated Sorted Array](src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.php)| 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) | 9 | 73.47 diff --git a/src/main/php/g0101_0200/s0169_majority_element/Solution.php b/src/main/php/g0101_0200/s0169_majority_element/Solution.php new file mode 100644 index 0000000..79d7255 --- /dev/null +++ b/src/main/php/g0101_0200/s0169_majority_element/Solution.php @@ -0,0 +1,23 @@ +<?php + +namespace leetcode\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) #2023_12_21_Time_43_ms_(72.03%)_Space_23_MB_(100.00%) + +class Solution { + /** + * @param Integer[] $nums + * @return Integer + */ + public function majorityElement($nums) { + $search = array_count_values($nums); + $count = count($nums) / 2; + foreach ($search as $key => $value) { + if ($value > $count) { + return $key; + } + } + } +} diff --git a/src/main/php/g0101_0200/s0169_majority_element/readme.md b/src/main/php/g0101_0200/s0169_majority_element/readme.md new file mode 100644 index 0000000..5ea5071 --- /dev/null +++ b/src/main/php/g0101_0200/s0169_majority_element/readme.md @@ -0,0 +1,27 @@ +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` +* <code>1 <= n <= 5 * 10<sup>4</sup></code> +* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code> + +**Follow-up:** Could you solve the problem in linear time and in `O(1)` space? \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0189_rotate_array/Solution.php b/src/main/php/g0101_0200/s0189_rotate_array/Solution.php new file mode 100644 index 0000000..c905267 --- /dev/null +++ b/src/main/php/g0101_0200/s0189_rotate_array/Solution.php @@ -0,0 +1,32 @@ +<?php + +namespace leetcode\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) +// #2023_12_21_Time_60_ms_(67.03%)_Space_27.9_MB_(100.00%) + +class Solution { + private function reverse(&$nums, $l, $r) { + while ($l <= $r) { + $temp = $nums[$l]; + $nums[$l] = $nums[$r]; + $nums[$r] = $temp; + $l++; + $r--; + } + } + + /** + * @param Integer[] $nums + * @param Integer $k + * @return NULL + */ + public function rotate(&$nums, $k) { + $n = count($nums); + $t = $n - ($k % $n); + $this->reverse($nums, 0, $t - 1); + $this->reverse($nums, $t, $n - 1); + $this->reverse($nums, 0, $n - 1); + } +} diff --git a/src/main/php/g0101_0200/s0189_rotate_array/readme.md b/src/main/php/g0101_0200/s0189_rotate_array/readme.md new file mode 100644 index 0000000..390f269 --- /dev/null +++ b/src/main/php/g0101_0200/s0189_rotate_array/readme.md @@ -0,0 +1,39 @@ +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:** + +* <code>1 <= nums.length <= 10<sup>5</sup></code> +* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code> +* <code>0 <= k <= 10<sup>5</sup></code> + +**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? \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0198_house_robber/Solution.php b/src/main/php/g0101_0200/s0198_house_robber/Solution.php new file mode 100644 index 0000000..0aca987 --- /dev/null +++ b/src/main/php/g0101_0200/s0198_house_robber/Solution.php @@ -0,0 +1,34 @@ +<?php + +namespace leetcode\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) +// #2023_12_21_Time_4_ms_(64.29%)_Space_19.8_MB_(9.52%) + +class Solution { + /** + * @param Integer[] $nums + * @return Integer + */ + public function rob($nums) { + $n = count($nums); + if ($n == 0) { + return 0; + } + if ($n == 1) { + return $nums[0]; + } + if ($n == 2) { + return max($nums[0], $nums[1]); + } + $profit = array(); + $profit[0] = $nums[0]; + $profit[1] = max($nums[1], $nums[0]); + for ($i = 2; $i < $n; $i++) { + $profit[$i] = max($profit[$i - 1], $nums[$i] + $profit[$i - 2]); + } + return $profit[$n - 1]; + } +} diff --git a/src/main/php/g0101_0200/s0198_house_robber/readme.md b/src/main/php/g0101_0200/s0198_house_robber/readme.md new file mode 100644 index 0000000..4a93a57 --- /dev/null +++ b/src/main/php/g0101_0200/s0198_house_robber/readme.md @@ -0,0 +1,34 @@ +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` \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0200_number_of_islands/Solution.php b/src/main/php/g0101_0200/s0200_number_of_islands/Solution.php new file mode 100644 index 0000000..2a3b34b --- /dev/null +++ b/src/main/php/g0101_0200/s0200_number_of_islands/Solution.php @@ -0,0 +1,41 @@ +<?php + +namespace leetcode\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) #2023_12_21_Time_97_ms_(82.05%)_Space_35.3_MB_(100.00%) + +class Solution { + /** + * @param String[][] $grid + * @return Integer + */ + public function numIslands($grid) { + $islands = 0; + if ($grid != null && !empty($grid) && count($grid[0]) != 0) { + for ($i = 0; $i < count($grid); $i++) { + for ($j = 0; $j < count($grid[0]); $j++) { + if ($grid[$i][$j] == '1') { + $this->dfs($grid, $i, $j); + $islands++; + } + } + } + } + return $islands; + } + + private function dfs(&$grid, $x, $y) { + if ($x < 0 || count($grid) <= $x || $y < 0 || count($grid[0]) <= $y || $grid[$x][$y] != '1') { + return; + } + $grid[$x][$y] = 'x'; + $this->dfs($grid, $x + 1, $y); + $this->dfs($grid, $x - 1, $y); + $this->dfs($grid, $x, $y + 1); + $this->dfs($grid, $x, $y - 1); + } +} diff --git a/src/main/php/g0101_0200/s0200_number_of_islands/readme.md b/src/main/php/g0101_0200/s0200_number_of_islands/readme.md new file mode 100644 index 0000000..0c4292d --- /dev/null +++ b/src/main/php/g0101_0200/s0200_number_of_islands/readme.md @@ -0,0 +1,40 @@ +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'`. \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php b/src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php new file mode 100644 index 0000000..aaae7ea --- /dev/null +++ b/src/main/php/g0201_0300/s0206_reverse_linked_list/Solution.php @@ -0,0 +1,37 @@ +<?php + +namespace leetcode\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) +// #2023_12_21_Time_0_ms_(100.00%)_Space_20.4_MB_(87.88%) + +/** + * Definition for a singly-linked list. + * class ListNode { + * public $val = 0; + * public $next = null; + * function __construct($val = 0, $next = null) { + * $this->val = $val; + * $this->next = $next; + * } + * } + */ +class Solution { + /** + * @param ListNode $head + * @return ListNode + */ + public function reverseList($head) { + $prev = null; + $curr = $head; + while ($curr != null) { + $next = $curr->next; + $curr->next = $prev; + $prev = $curr; + $curr = $next; + } + return $prev; + } +} diff --git a/src/main/php/g0201_0300/s0206_reverse_linked_list/readme.md b/src/main/php/g0201_0300/s0206_reverse_linked_list/readme.md new file mode 100644 index 0000000..a764d55 --- /dev/null +++ b/src/main/php/g0201_0300/s0206_reverse_linked_list/readme.md @@ -0,0 +1,34 @@ +206\. Reverse Linked List + +Easy + +Given the `head` of a singly linked list, reverse the list, and return _the reversed list_. + +**Example 1:** + + + +**Input:** head = [1,2,3,4,5] + +**Output:** [5,4,3,2,1] + +**Example 2:** + + + +**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? \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0207_course_schedule/Solution.php b/src/main/php/g0201_0300/s0207_course_schedule/Solution.php new file mode 100644 index 0000000..a80ba98 --- /dev/null +++ b/src/main/php/g0201_0300/s0207_course_schedule/Solution.php @@ -0,0 +1,46 @@ +<?php + +namespace leetcode\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) +// #2023_12_21_Time_25_ms_(89.29%)_Space_22.3_MB_(100.00%) + +class Solution { + const WHITE = 0; + const GRAY = 1; + const BLACK = 2; + + /** + * @param Integer $numCourses + * @param Integer[][] $prerequisites + * @return Boolean + */ + public function canFinish($numCourses, $prerequisites) { + $adj = array_fill(0, $numCourses, []); + foreach ($prerequisites as $pre) { + array_push($adj[$pre[1]], $pre[0]); + } + $colors = array_fill(0, $numCourses, self::WHITE); + for ($i = 0; $i < $numCourses; $i++) { + if ($colors[$i] == self::WHITE && !empty($adj[$i]) && $this->hasCycle($adj, $i, $colors)) { + return false; + } + } + return true; + } + + private function hasCycle($adj, $node, &$colors) { + $colors[$node] = self::GRAY; + foreach ($adj[$node] as $nei) { + if ($colors[$nei] == self::GRAY) { + return true; + } + if ($colors[$nei] == self::WHITE && $this->hasCycle($adj, $nei, $colors)) { + return true; + } + } + $colors[$node] = self::BLACK; + return false; + } +} diff --git a/src/main/php/g0201_0300/s0207_course_schedule/readme.md b/src/main/php/g0201_0300/s0207_course_schedule/readme.md new file mode 100644 index 0000000..a7b1e9c --- /dev/null +++ b/src/main/php/g0201_0300/s0207_course_schedule/readme.md @@ -0,0 +1,33 @@ +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 <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you **must** take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>. + +* 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:** + +* <code>1 <= numCourses <= 10<sup>5</sup></code> +* `0 <= prerequisites.length <= 5000` +* `prerequisites[i].length == 2` +* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code> +* All the pairs prerequisites[i] are **unique**. \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/Trie.php b/src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/Trie.php new file mode 100644 index 0000000..cee676b --- /dev/null +++ b/src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/Trie.php @@ -0,0 +1,85 @@ +<?php + +namespace leetcode\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) +// #2023_12_21_Time_83_ms_(46.67%)_Space_52.1_MB_(6.67%) + +class TrieNode { + public $children; + public $isWord; + + function __construct() { + $this->children = array_fill(0, 26, null); + $this->isWord = false; + } +} + +class Trie { + private $root; + private $startWith; + + function __construct() { + $this->root = new TrieNode(); + $this->startWith = false; + } + + /** + * @param String $word + * @return NULL + */ + public function insert($word) { + $this->insertInternal($word, $this->root, 0); + } + + private function insertInternal($word, $root, $idx) { + if ($idx == strlen($word)) { + $root->isWord = true; + return; + } + $index = ord($word[$idx]) - ord('a'); + if ($root->children[$index] == null) { + $root->children[$index] = new TrieNode(); + } + $this->insertInternal($word, $root->children[$index], $idx + 1); + } + + /** + * @param String $word + * @return Boolean + */ + public function search($word) { + return $this->searchInternal($word, $this->root, 0); + } + + private function searchInternal($word, $root, $idx) { + if ($idx == strlen($word)) { + $this->startWith = true; + return $root->isWord; + } + $index = ord($word[$idx]) - ord('a'); + if ($root->children[$index] == null) { + $this->startWith = false; + return false; + } + return $this->searchInternal($word, $root->children[$index], $idx + 1); + } + + /** + * @param String $prefix + * @return Boolean + */ + public function startsWith($prefix) { + $this->search($prefix); + return $this->startWith; + } +} +/** + * Your Trie object will be instantiated and called as such: + * $obj = Trie(); + * $obj->insert($word); + * $ret_2 = $obj->search($word); + * $ret_3 = $obj->startsWith($prefix); + */ diff --git a/src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/readme.md b/src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/readme.md new file mode 100644 index 0000000..8121de3 --- /dev/null +++ b/src/main/php/g0201_0300/s0208_implement_trie_prefix_tree/readme.md @@ -0,0 +1,36 @@ +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 <code>3 * 10<sup>4</sup></code> calls **in total** will be made to `insert`, `search`, and `startsWith`. \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.php b/src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.php new file mode 100644 index 0000000..538fe7a --- /dev/null +++ b/src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/Solution.php @@ -0,0 +1,20 @@ +<?php + +namespace leetcode\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)) #2023_12_21_Time_222_ms_(49.15%)_Space_29.8_MB_(100.00%) + +class Solution { + /** + * @param Integer[] $nums + * @param Integer $k + * @return Integer + */ + function findKthLargest($nums, $k) { + $n = count($nums); + sort($nums); + return $nums[$n - $k]; + } +} diff --git a/src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md b/src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md new file mode 100644 index 0000000..c4c64cb --- /dev/null +++ b/src/main/php/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md @@ -0,0 +1,24 @@ +215\. Kth Largest Element in an Array + +Medium + +Given an integer array `nums` and an integer `k`, return _the_ <code>k<sup>th</sup></code> _largest element in the array_. + +Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> 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:** + +* <code>1 <= k <= nums.length <= 10<sup>4</sup></code> +* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code> \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0221_maximal_square/Solution.php b/src/main/php/g0201_0300/s0221_maximal_square/Solution.php new file mode 100644 index 0000000..42388c1 --- /dev/null +++ b/src/main/php/g0201_0300/s0221_maximal_square/Solution.php @@ -0,0 +1,38 @@ +<?php + +namespace leetcode\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) +// #2023_12_21_Time_209_ms_(83.33%)_Space_40.6_MB_(100.00%) + +class Solution { + /** + * @param String[][] $matrix + * @return Integer + */ + public function maximalSquare($matrix) { + $m = count($matrix); + if ($m == 0) { + return 0; + } + $n = count($matrix[0]); + if ($n == 0) { + return 0; + } + $dp = array_fill(0, $m + 1, array_fill(0, $n + 1, 0)); + $max = 0; + for ($i = 0; $i < $m; $i++) { + for ($j = 0; $j < $n; $j++) { + if ($matrix[$i][$j] == '1') { + $next = 1 + min($dp[$i][$j], min($dp[$i + 1][$j], $dp[$i][$j + 1])); + if ($next > $max) { + $max = $next; + } + $dp[$i + 1][$j + 1] = $next; + } + } + } + return $max * $max; + } +} diff --git a/src/main/php/g0201_0300/s0221_maximal_square/readme.md b/src/main/php/g0201_0300/s0221_maximal_square/readme.md new file mode 100644 index 0000000..d9d3909 --- /dev/null +++ b/src/main/php/g0201_0300/s0221_maximal_square/readme.md @@ -0,0 +1,34 @@ +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:** + + + +**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:** + + + +**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'`. \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0226_invert_binary_tree/Solution.php b/src/main/php/g0201_0300/s0226_invert_binary_tree/Solution.php new file mode 100644 index 0000000..c1ba745 --- /dev/null +++ b/src/main/php/g0201_0300/s0226_invert_binary_tree/Solution.php @@ -0,0 +1,36 @@ +<?php + +namespace leetcode\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) #2023_12_21_Time_6_ms_(62.79%)_Space_19.6_MB_(13.18%) + +/** + * Definition for a binary tree node. + * class TreeNode { + * public $val = null; + * public $left = null; + * public $right = null; + * function __construct($val = 0, $left = null, $right = null) { + * $this->val = $val; + * $this->left = $left; + * $this->right = $right; + * } + * } + */ +class Solution { + /** + * @param TreeNode $root + * @return TreeNode + */ + public function invertTree($root) { + if ($root == null) { + return null; + } + $temp = $root->left; + $root->left = $this->invertTree($root->right); + $root->right = $this->invertTree($temp); + return $root; + } +} diff --git a/src/main/php/g0201_0300/s0226_invert_binary_tree/readme.md b/src/main/php/g0201_0300/s0226_invert_binary_tree/readme.md new file mode 100644 index 0000000..339bf4e --- /dev/null +++ b/src/main/php/g0201_0300/s0226_invert_binary_tree/readme.md @@ -0,0 +1,32 @@ +226\. Invert Binary Tree + +Easy + +Given the `root` of a binary tree, invert the tree, and return _its root_. + +**Example 1:** + + + +**Input:** root = [4,2,7,1,3,6,9] + +**Output:** [4,7,2,9,6,3,1] + +**Example 2:** + + + +**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` \ No newline at end of file diff --git a/src/test/php/g0101_0200/s0169_majority_element/SolutionTest.php b/src/test/php/g0101_0200/s0169_majority_element/SolutionTest.php new file mode 100644 index 0000000..96f2037 --- /dev/null +++ b/src/test/php/g0101_0200/s0169_majority_element/SolutionTest.php @@ -0,0 +1,15 @@ +<?php + +namespace leetcode\g0101_0200\s0169_majority_element; + +use PHPUnit\Framework\TestCase; + +class SolutionTest extends TestCase { + public function testMajorityElement() { + $this->assertEquals(3, (new Solution())->majorityElement(array(3, 2, 3))); + } + + public function testMajorityElement2() { + $this->assertEquals(2, (new Solution())->majorityElement(array(2, 2, 1, 1, 1, 2, 2))); + } +} diff --git a/src/test/php/g0101_0200/s0189_rotate_array/SolutionTest.php b/src/test/php/g0101_0200/s0189_rotate_array/SolutionTest.php new file mode 100644 index 0000000..4815a59 --- /dev/null +++ b/src/test/php/g0101_0200/s0189_rotate_array/SolutionTest.php @@ -0,0 +1,19 @@ +<?php + +namespace leetcode\g0101_0200\s0189_rotate_array; + +use PHPUnit\Framework\TestCase; + +class SolutionTest extends TestCase { + public function testRotate() { + $array = array(1, 2, 3, 4, 5, 6, 7); + (new Solution())->rotate($array, 3); + $this->assertEquals(array(5, 6, 7, 1, 2, 3, 4), $array); + } + + public function testRotate2() { + $array = array(-1, -100, 3, 99); + (new Solution())->rotate($array, 2); + $this->assertEquals(array(3, 99, -1, -100), $array); + } +} diff --git a/src/test/php/g0101_0200/s0198_house_robber/SolutionTest.php b/src/test/php/g0101_0200/s0198_house_robber/SolutionTest.php new file mode 100644 index 0000000..6332366 --- /dev/null +++ b/src/test/php/g0101_0200/s0198_house_robber/SolutionTest.php @@ -0,0 +1,15 @@ +<?php + +namespace leetcode\g0101_0200\s0198_house_robber; + +use PHPUnit\Framework\TestCase; + +class SolutionTest extends TestCase { + public function testRob() { + $this->assertEquals(4, (new Solution())->rob(array(1, 2, 3, 1))); + } + + public function testRob2() { + $this->assertEquals(12, (new Solution())->rob(array(2, 7, 9, 3, 1))); + } +} diff --git a/src/test/php/g0101_0200/s0200_number_of_islands/SolutionTest.php b/src/test/php/g0101_0200/s0200_number_of_islands/SolutionTest.php new file mode 100644 index 0000000..e913334 --- /dev/null +++ b/src/test/php/g0101_0200/s0200_number_of_islands/SolutionTest.php @@ -0,0 +1,25 @@ +<?php + +namespace leetcode\g0101_0200\s0200_number_of_islands; + +use PHPUnit\Framework\TestCase; + +class SolutionTest extends TestCase { + public function testNumIslands() { + $this->assertEquals(1, (new Solution())->numIslands(array( + array('1', '1', '1', '1', '0'), + array('1', '1', '0', '1', '0'), + array('1', '1', '0', '0', '0'), + array('0', '0', '0', '0', '0') + ))); + } + + public function testNumIslands2() { + $this->assertEquals(3, (new Solution())->numIslands(array( + array('1', '1', '0', '0', '0'), + array('1', '1', '0', '0', '0'), + array('0', '0', '1', '0', '0'), + array('0', '0', '0', '1', '1') + ))); + } +} diff --git a/src/test/php/g0201_0300/s0206_reverse_linked_list/SolutionTest.php b/src/test/php/g0201_0300/s0206_reverse_linked_list/SolutionTest.php new file mode 100644 index 0000000..919667c --- /dev/null +++ b/src/test/php/g0201_0300/s0206_reverse_linked_list/SolutionTest.php @@ -0,0 +1,27 @@ +<?php + +namespace leetcode\g0201_0300\s0206_reverse_linked_list; + +use PHPUnit\Framework\TestCase; +use leetcode\com_github_leetcode\ListNode; + +class SolutionTest extends TestCase { + public function testReverseList() { + $headActual = new ListNode(1); + $headActual->next = new ListNode(2); + $headActual->next->next = new ListNode(3); + $headActual->next->next->next = new ListNode(4); + $headActual->next->next->next->next = new ListNode(5); + $this->assertEquals("5, 4, 3, 2, 1", (new Solution())->reverseList($headActual)->toString()); + } + + public function testReverseList2() { + $headActual = new ListNode(1); + $headActual->next = new ListNode(2); + $this->assertEquals("2, 1", (new Solution())->reverseList($headActual)->toString()); + } + + public function testReverseList3() { + $this->assertEquals(null, (new Solution())->reverseList(null)); + } +} diff --git a/src/test/php/g0201_0300/s0207_course_schedule/SolutionTest.php b/src/test/php/g0201_0300/s0207_course_schedule/SolutionTest.php new file mode 100644 index 0000000..de65a83 --- /dev/null +++ b/src/test/php/g0201_0300/s0207_course_schedule/SolutionTest.php @@ -0,0 +1,15 @@ +<?php + +namespace leetcode\g0201_0300\s0207_course_schedule; + +use PHPUnit\Framework\TestCase; + +class SolutionTest extends TestCase { + public function testCanFinish() { + $this->assertEquals(true, (new Solution())->canFinish(2, array(array(1, 0)))); + } + + public function testCanFinish2() { + $this->assertEquals(false, (new Solution())->canFinish(2, array(array(1, 0), array(0, 1)))); + } +} diff --git a/src/test/php/g0201_0300/s0208_implement_trie_prefix_tree/TrieTest.php b/src/test/php/g0201_0300/s0208_implement_trie_prefix_tree/TrieTest.php new file mode 100644 index 0000000..d946203 --- /dev/null +++ b/src/test/php/g0201_0300/s0208_implement_trie_prefix_tree/TrieTest.php @@ -0,0 +1,17 @@ +<?php + +namespace leetcode\g0201_0300\s0208_implement_trie_prefix_tree; + +use PHPUnit\Framework\TestCase; + +class TrieTest extends TestCase { + public function testTrie() { + $trie = new Trie(); + $trie->insert("apple"); + $this->assertEquals(true, $trie->search("apple")); + $this->assertEquals(false, $trie->search("app")); + $this->assertEquals(true, $trie->startsWith("app")); + $trie->insert("app"); + $this->assertEquals(true, $trie->search("app")); + } +} diff --git a/src/test/php/g0201_0300/s0215_kth_largest_element_in_an_array/SolutionTest.php b/src/test/php/g0201_0300/s0215_kth_largest_element_in_an_array/SolutionTest.php new file mode 100644 index 0000000..3308f55 --- /dev/null +++ b/src/test/php/g0201_0300/s0215_kth_largest_element_in_an_array/SolutionTest.php @@ -0,0 +1,15 @@ +<?php + +namespace leetcode\g0201_0300\s0215_kth_largest_element_in_an_array; + +use PHPUnit\Framework\TestCase; + +class SolutionTest extends TestCase { + public function testFindKthLargest() { + $this->assertEquals(5, (new Solution())->findKthLargest(array(3, 2, 1, 5, 6, 4), 2)); + } + + public function testFindKthLargest2() { + $this->assertEquals(4, (new Solution())->findKthLargest(array(3, 2, 3, 1, 2, 4, 5, 5, 6), 4)); + } +} diff --git a/src/test/php/g0201_0300/s0221_maximal_square/SolutionTest.php b/src/test/php/g0201_0300/s0221_maximal_square/SolutionTest.php new file mode 100644 index 0000000..4697e34 --- /dev/null +++ b/src/test/php/g0201_0300/s0221_maximal_square/SolutionTest.php @@ -0,0 +1,27 @@ +<?php + +namespace leetcode\g0201_0300\s0221_maximal_square; + +use PHPUnit\Framework\TestCase; + +class SolutionTest extends TestCase { + public function testMaximalSquare() { + $input = array( + array('1', '0', '1', '0', '0'), + array('1', '0', '1', '1', '1'), + array('1', '1', '1', '1', '1'), + array('1', '0', '0', '1', '0') + ); + $this->assertEquals(4, (new Solution())->maximalSquare($input)); + } + + public function testMaximalSquare2() { + $input = array(array('0', '1'), array('1', '0')); + $this->assertEquals(1, (new Solution())->maximalSquare($input)); + } + + public function testMaximalSquare3() { + $input = array(array('0')); + $this->assertEquals(0, (new Solution())->maximalSquare($input)); + } +} diff --git a/src/test/php/g0201_0300/s0226_invert_binary_tree/SolutionTest.php b/src/test/php/g0201_0300/s0226_invert_binary_tree/SolutionTest.php new file mode 100644 index 0000000..b39f7b2 --- /dev/null +++ b/src/test/php/g0201_0300/s0226_invert_binary_tree/SolutionTest.php @@ -0,0 +1,18 @@ +<?php + +namespace leetcode\g0201_0300\s0226_invert_binary_tree; + +use PHPUnit\Framework\TestCase; +use leetcode\com_github_leetcode\TreeUtils; + +class SolutionTest extends TestCase { + public function testInvertTree() { + $root = TreeUtils::constructBinaryTree(array(4, 2, 7, 1, 3, 6, 9)); + $this->assertEquals("4,7,9,6,2,3,1", (new Solution())->invertTree($root)); + } + + public function testInvertTree2() { + $root = TreeUtils::constructBinaryTree(array(2, 1, 3)); + $this->assertEquals("2,3,1", (new Solution())->invertTree($root)); + } +}