From 14a847f862ccb274d4ee4fa67403cd170fa7ce21 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 17 Dec 2023 07:16:57 +0200 Subject: [PATCH] Added tasks 138-160 --- README.md | 34 ++++++ .../php/com_github_leetcode/random/Node.php | 46 ++++++++ .../Solution.php | 68 +++++++++++ .../readme.md | 56 +++++++++ .../g0101_0200/s0139_word_break/Solution.php | 48 ++++++++ .../php/g0101_0200/s0139_word_break/readme.md | 37 ++++++ .../s0141_linked_list_cycle/Solution.php | 37 ++++++ .../s0141_linked_list_cycle/readme.md | 47 ++++++++ .../s0142_linked_list_cycle_ii/Solution.php | 45 +++++++ .../s0142_linked_list_cycle_ii/readme.md | 47 ++++++++ .../g0101_0200/s0146_lru_cache/LRUCache.php | 111 ++++++++++++++++++ .../php/g0101_0200/s0146_lru_cache/readme.md | 39 ++++++ .../g0101_0200/s0148_sort_list/Solution.php | 42 +++++++ .../php/g0101_0200/s0148_sort_list/readme.md | 34 ++++++ .../Solution.php | 34 ++++++ .../s0152_maximum_product_subarray/readme.md | 31 +++++ .../Solution.php | 38 ++++++ .../readme.md | 46 ++++++++ .../g0101_0200/s0155_min_stack/MinStack.php | 64 ++++++++++ .../php/g0101_0200/s0155_min_stack/readme.md | 39 ++++++ .../Solution.php | 32 +++++ .../readme.md | 68 +++++++++++ .../SolutionTest.php | 50 ++++++++ .../s0139_word_break/SolutionTest.php | 20 ++++ .../s0141_linked_list_cycle/SolutionTest.php | 29 +++++ .../SolutionTest.php | 29 +++++ .../s0146_lru_cache/LRUCacheTest.php | 29 +++++ .../s0148_sort_list/SolutionTest.php | 29 +++++ .../SolutionTest.php | 15 +++ .../SolutionTest.php | 19 +++ .../s0155_min_stack/MinStackTest.php | 21 ++++ .../SolutionTest.php | 21 ++++ 32 files changed, 1305 insertions(+) create mode 100644 src/main/php/com_github_leetcode/random/Node.php create mode 100644 src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/Solution.php create mode 100644 src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/readme.md create mode 100644 src/main/php/g0101_0200/s0139_word_break/Solution.php create mode 100644 src/main/php/g0101_0200/s0139_word_break/readme.md create mode 100644 src/main/php/g0101_0200/s0141_linked_list_cycle/Solution.php create mode 100644 src/main/php/g0101_0200/s0141_linked_list_cycle/readme.md create mode 100644 src/main/php/g0101_0200/s0142_linked_list_cycle_ii/Solution.php create mode 100644 src/main/php/g0101_0200/s0142_linked_list_cycle_ii/readme.md create mode 100644 src/main/php/g0101_0200/s0146_lru_cache/LRUCache.php create mode 100644 src/main/php/g0101_0200/s0146_lru_cache/readme.md create mode 100644 src/main/php/g0101_0200/s0148_sort_list/Solution.php create mode 100644 src/main/php/g0101_0200/s0148_sort_list/readme.md create mode 100644 src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php create mode 100644 src/main/php/g0101_0200/s0152_maximum_product_subarray/readme.md create mode 100644 src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.php create mode 100644 src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/readme.md create mode 100644 src/main/php/g0101_0200/s0155_min_stack/MinStack.php create mode 100644 src/main/php/g0101_0200/s0155_min_stack/readme.md create mode 100644 src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.php create mode 100644 src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/readme.md create mode 100644 src/test/php/g0101_0200/s0138_copy_list_with_random_pointer/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0139_word_break/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0141_linked_list_cycle/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0142_linked_list_cycle_ii/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0146_lru_cache/LRUCacheTest.php create mode 100644 src/test/php/g0101_0200/s0148_sort_list/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0152_maximum_product_subarray/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/SolutionTest.php create mode 100644 src/test/php/g0101_0200/s0155_min_stack/MinStackTest.php create mode 100644 src/test/php/g0101_0200/s0160_intersection_of_two_linked_lists/SolutionTest.php diff --git a/README.md b/README.md index 3707876..fab7185 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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, Big_O_Time_O(log_N)_Space_O(log_N) | 9 | 73.47 ### Binary Search II @@ -229,6 +230,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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 #### Day 7 @@ -245,6 +247,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 | 0042 |[Trapping Rain Water](src/main/php/g0001_0100/s0042_trapping_rain_water/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 29 | 69.15 #### Day 10 @@ -448,6 +451,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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 #### Day 15 @@ -469,6 +473,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 #### Day 19 @@ -626,6 +631,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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 #### Day 5 Greedy @@ -710,6 +716,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0148 |[Sort List](src/main/php/g0101_0200/s0148_sort_list/Solution.php)| 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)) | 63 | 97.30 #### Day 5 Greedy @@ -757,6 +764,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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 #### Day 14 Sliding Window/Two Pointer @@ -775,6 +783,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 #### Day 17 Interval @@ -823,6 +832,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- | 0033 |[Search in Rotated Sorted Array](src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 7 | 83.17 +| 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, Big_O_Time_O(log_N)_Space_O(log_N) | 9 | 73.47 #### Udemy Arrays @@ -867,8 +877,13 @@ Php-based LeetCode algorithm problem solutions, regularly updated. |-|-|-|-|-|- | 0114 |[Flatten Binary Tree to Linked List](src/main/php/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.php)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 3 | 100.00 | 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 | 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 | 0025 |[Reverse Nodes in k-Group](src/main/php/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.php)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 3 | 100.00 +| 0146 |[LRU Cache](src/main/php/g0101_0200/s0146_lru_cache/LRUCache.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 402 | 41.18 #### Udemy Tree Stack Queue @@ -894,6 +909,8 @@ 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 | 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 @@ -918,6 +935,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 ### Data Structure I @@ -959,6 +977,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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 | 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 #### Day 8 Linked List @@ -1061,11 +1080,13 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- | 0002 |[Add Two Numbers](src/main/php/g0001_0100/s0002_add_two_numbers/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 13 | 73.83 +| 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 #### Day 11 Linked List | | | | | | |-|-|-|-|-|- +| 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 #### Day 12 Linked List @@ -1083,6 +1104,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 #### Day 15 Tree @@ -1213,6 +1235,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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, Big_O_Time_O(log_N)_Space_O(log_N) | 9 | 73.47 #### Day 3 Two Pointers @@ -1289,6 +1312,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 #### Day 16 Dynamic Programming @@ -1325,6 +1349,16 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|--------- +| 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 +| 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, Dynamic_Programming_I_Day_6, Level_2_Day_13_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 15 | 92.86 +| 0148 |[Sort List](src/main/php/g0101_0200/s0148_sort_list/Solution.php)| 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)) | 63 | 97.30 +| 0146 |[LRU Cache](src/main/php/g0101_0200/s0146_lru_cache/LRUCache.php)| 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) | 402 | 41.18 +| 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, Data_Structure_II_Day_10_Linked_List, Level_1_Day_4_Linked_List, Udemy_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, Data_Structure_I_Day_7_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 8 | 90.60 +| 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, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_9, Udemy_Dynamic_Programming, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 7 | 75.00 +| 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, Programming_Skills_II_Day_14, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 2300 | 60.00 | 0136 |[Single Number](src/main/php/g0101_0200/s0136_single_number/Solution.php)| 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) | 33 | 80.08 | 0131 |[Palindrome Partitioning](src/main/php/g0101_0200/s0131_palindrome_partitioning/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 159 | 61.90 | 0128 |[Longest Consecutive Sequence](src/main/php/g0101_0200/s0128_longest_consecutive_sequence/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 154 | 75.61 diff --git a/src/main/php/com_github_leetcode/random/Node.php b/src/main/php/com_github_leetcode/random/Node.php new file mode 100644 index 0000000..376cae5 --- /dev/null +++ b/src/main/php/com_github_leetcode/random/Node.php @@ -0,0 +1,46 @@ +val = $val; + $this->next = $next; + $this->random = $random; + } + + function __toString() { + $result = array(); + $result2 = array(); + array_push($result2, strval($this->val)); + if ($this->random == null) { + array_push($result2, "null"); + } else { + array_push($result2, strval($this->random->val)); + } + array_push($result, "[" . implode(",", $result2) . "]"); + $curr = $this->next; + while ($curr != null) { + $result3 = array(); + array_push($result3, strval($curr->val)); + if ($curr->random == null) { + array_push($result3, "null"); + } else { + $randomIndex = 0; + $curr2 = $this; + while ($curr2->next != null && $curr2 != $curr->random) { + $randomIndex += 1; + $curr2 = $curr2->next; + } + array_push($result3, strval($randomIndex)); + } + array_push($result, "[" . implode(",", $result3) . "]"); + $curr = $curr->next; + } + return "[" . implode(",", $result) . "]"; + } +} diff --git a/src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/Solution.php b/src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/Solution.php new file mode 100644 index 0000000..ce70a39 --- /dev/null +++ b/src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/Solution.php @@ -0,0 +1,68 @@ +val = $val; + * $this->next = null; + * $this->random = null; + * } + * } + */ +class Solution { + /** + * @param Node $head + * @return Node + */ + public function copyRandomList($head) { + if ($head == null) { + return null; + } + $curr = $head; + while ($curr != null) { + $clonedNode = new Node($curr->val); + $clonedNode->next = $curr->next; + $curr->next = $clonedNode; + $curr = $clonedNode->next; + } + $curr = $head; + while ($curr != null) { + if ($curr->random != null) { + $curr->next->random = $curr->random->next; + } else { + $curr->next->random = null; + } + $curr = $curr->next->next; + } + $curr = $head; + $newHead = null; + while ($curr != null) { + if ($newHead == null) { + $clonedNode = $curr->next; + $newHead = $clonedNode; + } else { + $clonedNode = $curr->next; + } + $curr->next = $clonedNode->next; + if ($curr->next != null) { + $clonedNode->next = $curr->next->next; + } else { + $clonedNode->next = null; + } + $curr = $curr->next; + } + return $newHead; + } +} diff --git a/src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/readme.md b/src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/readme.md new file mode 100644 index 0000000..9e2f939 --- /dev/null +++ b/src/main/php/g0101_0200/s0138_copy_list_with_random_pointer/readme.md @@ -0,0 +1,56 @@ +138\. Copy List with Random Pointer + +Medium + +A linked list of length `n` is given such that each node contains an additional random pointer, which could point to any node in the list, or `null`. + +Construct a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) of the list. The deep copy should consist of exactly `n` **brand new** nodes, where each new node has its value set to the value of its corresponding original node. Both the `next` and `random` pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. **None of the pointers in the new list should point to nodes in the original list**. + +For example, if there are two nodes `X` and `Y` in the original list, where `X.random --> Y`, then for the corresponding two nodes `x` and `y` in the copied list, `x.random --> y`. + +Return _the head of the copied linked list_. + +The linked list is represented in the input/output as a list of `n` nodes. Each node is represented as a pair of `[val, random_index]` where: + +* `val`: an integer representing `Node.val` +* `random_index`: the index of the node (range from `0` to `n-1`) that the `random` pointer points to, or `null` if it does not point to any node. + +Your code will **only** be given the `head` of the original linked list. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2019/12/18/e1.png) + +**Input:** head = [[7,null],[13,0],[11,4],[10,2],[1,0]] + +**Output:** [[7,null],[13,0],[11,4],[10,2],[1,0]] + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2019/12/18/e2.png) + +**Input:** head = [[1,1],[2,1]] + +**Output:** [[1,1],[2,1]] + +**Example 3:** + +**![](https://assets.leetcode.com/uploads/2019/12/18/e3.png)** + +**Input:** head = [[3,null],[3,0],[3,null]] + +**Output:** [[3,null],[3,0],[3,null]] + +**Example 4:** + +**Input:** head = [] + +**Output:** [] + +**Explanation:** The given linked list is empty (null pointer), so return null. + +**Constraints:** + +* `0 <= n <= 1000` +* `-10000 <= Node.val <= 10000` +* `Node.random` is `null` or is pointing to some node in the linked list. \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0139_word_break/Solution.php b/src/main/php/g0101_0200/s0139_word_break/Solution.php new file mode 100644 index 0000000..ac0f672 --- /dev/null +++ b/src/main/php/g0101_0200/s0139_word_break/Solution.php @@ -0,0 +1,48 @@ +dfs($s, 0, $i, $max, $set, $flag)) { + return true; + } + } + return false; + } + + private function dfs($s, $start, $end, $max, $set, &$flag) { + if (!$flag[$end] && isset($set[substr($s, $start, $end - $start)])) { + $flag[$end] = true; + if ($end == strlen($s)) { + return true; + } + for ($i = 1; $i <= $max; $i++) { + if ($end + $i <= strlen($s) && $this->dfs($s, $end, $end + $i, $max, $set, $flag)) { + return true; + } + } + } + return false; + } +} diff --git a/src/main/php/g0101_0200/s0139_word_break/readme.md b/src/main/php/g0101_0200/s0139_word_break/readme.md new file mode 100644 index 0000000..7e152e6 --- /dev/null +++ b/src/main/php/g0101_0200/s0139_word_break/readme.md @@ -0,0 +1,37 @@ +139\. Word Break + +Medium + +Given a string `s` and a dictionary of strings `wordDict`, return `true` if `s` can be segmented into a space-separated sequence of one or more dictionary words. + +**Note** that the same word in the dictionary may be reused multiple times in the segmentation. + +**Example 1:** + +**Input:** s = "leetcode", wordDict = ["leet","code"] + +**Output:** true + +**Explanation:** Return true because "leetcode" can be segmented as "leet code". + +**Example 2:** + +**Input:** s = "applepenapple", wordDict = ["apple","pen"] + +**Output:** true + +**Explanation:** Return true because "applepenapple" can be segmented as "apple pen apple". Note that you are allowed to reuse a dictionary word. + +**Example 3:** + +**Input:** s = "catsandog", wordDict = ["cats","dog","sand","and","cat"] + +**Output:** false + +**Constraints:** + +* `1 <= s.length <= 300` +* `1 <= wordDict.length <= 1000` +* `1 <= wordDict[i].length <= 20` +* `s` and `wordDict[i]` consist of only lowercase English letters. +* All the strings of `wordDict` are **unique**. \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0141_linked_list_cycle/Solution.php b/src/main/php/g0101_0200/s0141_linked_list_cycle/Solution.php new file mode 100644 index 0000000..41470ea --- /dev/null +++ b/src/main/php/g0101_0200/s0141_linked_list_cycle/Solution.php @@ -0,0 +1,37 @@ +val = $val; } + * } + */ +class Solution { + /** + * @param ListNode $head + * @return Boolean + */ + public function hasCycle($head) { + if ($head == null) { + return false; + } + $fast = $head->next; + $slow = $head; + while ($fast != null && $fast->next != null) { + if ($fast === $slow) { + return true; + } + $fast = $fast->next->next; + $slow = $slow->next; + } + return false; + } +} diff --git a/src/main/php/g0101_0200/s0141_linked_list_cycle/readme.md b/src/main/php/g0101_0200/s0141_linked_list_cycle/readme.md new file mode 100644 index 0000000..da791d6 --- /dev/null +++ b/src/main/php/g0101_0200/s0141_linked_list_cycle/readme.md @@ -0,0 +1,47 @@ +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? \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0142_linked_list_cycle_ii/Solution.php b/src/main/php/g0101_0200/s0142_linked_list_cycle_ii/Solution.php new file mode 100644 index 0000000..1684872 --- /dev/null +++ b/src/main/php/g0101_0200/s0142_linked_list_cycle_ii/Solution.php @@ -0,0 +1,45 @@ +val = $val; } + * } + */ +class Solution { + /** + * @param ListNode $head + * @return ListNode + */ + public function detectCycle($head) { + if ($head == null || $head->next == null) { + return null; + } + $slow = $head; + $fast = $head; + while ($fast != null && $fast->next != null) { + $fast = $fast->next->next; + $slow = $slow->next; + if ($slow === $fast) { + break; + } + } + if ($fast == null || $fast->next == null) { + return null; + } + $slow = $head; + while ($slow !== $fast) { + $slow = $slow->next; + $fast = $fast->next; + } + return $slow; + } +} diff --git a/src/main/php/g0101_0200/s0142_linked_list_cycle_ii/readme.md b/src/main/php/g0101_0200/s0142_linked_list_cycle_ii/readme.md new file mode 100644 index 0000000..8aeb2e4 --- /dev/null +++ b/src/main/php/g0101_0200/s0142_linked_list_cycle_ii/readme.md @@ -0,0 +1,47 @@ +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? \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0146_lru_cache/LRUCache.php b/src/main/php/g0101_0200/s0146_lru_cache/LRUCache.php new file mode 100644 index 0000000..9fe0991 --- /dev/null +++ b/src/main/php/g0101_0200/s0146_lru_cache/LRUCache.php @@ -0,0 +1,111 @@ +key = $k; + $this->value = $v; + } +} + +class LRUCache { + private $capacity; + private $cacheMap; + private $head; + private $tail; + + /** + * @param Integer $capacity + */ + function __construct($cap) { + $this->capacity = $cap; + $this->cacheMap = array(); + } + + /** + * @param Integer $key + * @return Integer + */ + public function get($key) { + if (!isset($this->cacheMap[$key])) { + return -1; + } + $val = $this->cacheMap[$key]; + $this->moveToHead($val); + return $val->value; + } + + /** + * @param Integer $key + * @param Integer $value + * @return NULL + */ + public function put($key, $value) { + if (isset($this->cacheMap[$key])) { + $valNode = $this->cacheMap[$key]; + $valNode->value = $value; + $this->moveToHead($valNode); + } else { + if (count($this->cacheMap) < $this->capacity) { + if (count($this->cacheMap) == 0) { + $node = new LruCacheNode($key, $value); + $this->cacheMap[$key] = $node; + $this->head = $node; + $this->tail = $node; + } else { + $node = new LruCacheNode($key, $value); + $this->cacheMap[$key] = $node; + $node->next = $this->head; + $this->head->prev = $node; + $this->head = $node; + } + } else { + $last = $this->tail; + $this->tail = $last->prev; + if ($this->tail != null) { + $this->tail->next = null; + } + unset($this->cacheMap[$last->key]); + if (count($this->cacheMap) == 0) { + $this->head = null; + } + $this->put($key, $value); + } + } + } + + private function moveToHead($node) { + if ($node === $this->head) { + return; + } + if ($node === $this->tail) { + $this->tail = $node->prev; + } + $prev = $node->prev; + $next = $node->next; + $prev->next = $next; + if ($next != null) { + $next->prev = $prev; + } + $node->prev = null; + $node->next = $this->head; + $this->head->prev = $node; + $this->head = $node; + } +} +/** + * Your LRUCache object will be instantiated and called as such: + * $obj = LRUCache($capacity); + * $ret_1 = $obj->get($key); + * $obj->put($key, $value); + */ diff --git a/src/main/php/g0101_0200/s0146_lru_cache/readme.md b/src/main/php/g0101_0200/s0146_lru_cache/readme.md new file mode 100644 index 0000000..8bc0954 --- /dev/null +++ b/src/main/php/g0101_0200/s0146_lru_cache/readme.md @@ -0,0 +1,39 @@ +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`. diff --git a/src/main/php/g0101_0200/s0148_sort_list/Solution.php b/src/main/php/g0101_0200/s0148_sort_list/Solution.php new file mode 100644 index 0000000..2a0e00c --- /dev/null +++ b/src/main/php/g0101_0200/s0148_sort_list/Solution.php @@ -0,0 +1,42 @@ +val = $val; + * $this->next = $next; + * } + * } + */ +class Solution { + /** + * @param ListNode $head + * @return ListNode + */ + public function sortList($head) { + $sortArray = []; + $sortedHead = $head; + while (!is_null($sortedHead)) { + $sortArray[] = $sortedHead->val; + $sortedHead = $sortedHead->next; + } + unset($sortedHead); + sort($sortArray); + $newHead = $head; + for ($i = 0; $i < count($sortArray); $i++) { + $newHead->val = $sortArray[$i]; + $newHead = $newHead->next; + } + unset($newHead); + return $head; + } +} diff --git a/src/main/php/g0101_0200/s0148_sort_list/readme.md b/src/main/php/g0101_0200/s0148_sort_list/readme.md new file mode 100644 index 0000000..94f637d --- /dev/null +++ b/src/main/php/g0101_0200/s0148_sort_list/readme.md @@ -0,0 +1,34 @@ +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)? \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php b/src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php new file mode 100644 index 0000000..b41b6b1 --- /dev/null +++ b/src/main/php/g0101_0200/s0152_maximum_product_subarray/Solution.php @@ -0,0 +1,34 @@ += 0; $i--) { + $cprod = $cprod * $arr[$i]; + $ans = max($ans, $cprod); + if ($cprod == 0) { + $cprod = 1; + } + } + return $ans; + } +} diff --git a/src/main/php/g0101_0200/s0152_maximum_product_subarray/readme.md b/src/main/php/g0101_0200/s0152_maximum_product_subarray/readme.md new file mode 100644 index 0000000..13f4e95 --- /dev/null +++ b/src/main/php/g0101_0200/s0152_maximum_product_subarray/readme.md @@ -0,0 +1,31 @@ +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. \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.php b/src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.php new file mode 100644 index 0000000..b373dd9 --- /dev/null +++ b/src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.php @@ -0,0 +1,38 @@ +findMinUtil($nums, $l, $r); + } + + private function findMinUtil($nums, $l, $r) { + if ($l == $r) { + return $nums[$l]; + } + $mid = intval(($l + $r) / 2); + if ($mid == $l && $nums[$mid] < $nums[$r]) { + return $nums[$l]; + } + if ($mid - 1 >= 0 && $nums[$mid - 1] > $nums[$mid]) { + return $nums[$mid]; + } + if ($nums[$mid] < $nums[$l]) { + return $this->findMinUtil($nums, $l, $mid - 1); + } elseif ($nums[$mid] > $nums[$r]) { + return $this->findMinUtil($nums, $mid + 1, $r); + } + return $this->findMinUtil($nums, $l, $mid - 1); + } +} diff --git a/src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/readme.md b/src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/readme.md new file mode 100644 index 0000000..db0231d --- /dev/null +++ b/src/main/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/readme.md @@ -0,0 +1,46 @@ +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. \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0155_min_stack/MinStack.php b/src/main/php/g0101_0200/s0155_min_stack/MinStack.php new file mode 100644 index 0000000..c22bae2 --- /dev/null +++ b/src/main/php/g0101_0200/s0155_min_stack/MinStack.php @@ -0,0 +1,64 @@ +min = $min; + $this->data = $data; + $this->previousNode = $previousNode; + $this->nextNode = $nextNode; + } +} + +class MinStack { + private $currentNode; + + function __construct() { + // no initialization needed. + } + + /** + * @param Integer $val + * @return NULL + */ + public function push($val) { + if ($this->currentNode == null) { + $this->currentNode = new Node($val, $val, null, null); + } else { + $this->currentNode->nextNode = new Node(min($this->currentNode->min, $val), $val, $this->currentNode, null); + $this->currentNode = $this->currentNode->nextNode; + } + } + + /** + * @return NULL + */ + public function pop() { + $this->currentNode = $this->currentNode->previousNode; + } + + /** + * @return Integer + */ + public function top() { + return $this->currentNode->data; + } + + /** + * @return Integer + */ + function getMin() { + return $this->currentNode->min; + } +} diff --git a/src/main/php/g0101_0200/s0155_min_stack/readme.md b/src/main/php/g0101_0200/s0155_min_stack/readme.md new file mode 100644 index 0000000..f3a3af2 --- /dev/null +++ b/src/main/php/g0101_0200/s0155_min_stack/readme.md @@ -0,0 +1,39 @@ +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`. \ No newline at end of file diff --git a/src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.php b/src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.php new file mode 100644 index 0000000..50f1291 --- /dev/null +++ b/src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.php @@ -0,0 +1,32 @@ +val = $val; } + * } + */ +class Solution { + /** + * @param ListNode $headA + * @param ListNode $headB + * @return ListNode + */ + public function getIntersectionNode($headA, $headB) { + $node1 = $headA; + $node2 = $headB; + while ($node1 !== $node2) { + $node1 = $node1 == null ? $headB : $node1->next; + $node2 = $node2 == null ? $headA : $node2->next; + } + return $node1; + } +} diff --git a/src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/readme.md b/src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/readme.md new file mode 100644 index 0000000..de0e1d8 --- /dev/null +++ b/src/main/php/g0101_0200/s0160_intersection_of_two_linked_lists/readme.md @@ -0,0 +1,68 @@ +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? \ No newline at end of file diff --git a/src/test/php/g0101_0200/s0138_copy_list_with_random_pointer/SolutionTest.php b/src/test/php/g0101_0200/s0138_copy_list_with_random_pointer/SolutionTest.php new file mode 100644 index 0000000..2a767db --- /dev/null +++ b/src/test/php/g0101_0200/s0138_copy_list_with_random_pointer/SolutionTest.php @@ -0,0 +1,50 @@ +next = $node13; + $node13->next = $node11; + $node11->next = $node10; + $node10->next = $node1; + $node1->next = null; + $node7->random = null; + $node13->random = $node7; + $node11->random = $node1; + $node10->random = $node11; + $node1->random = $node7; + $this->assertEquals("[[7,null],[13,0],[11,4],[10,2],[1,0]]", (new Solution())->copyRandomList($node7)); + } + + public function testCopyRandomList2() { + $node1 = new Node(1); + $node2 = new Node(2); + $node1->next = $node2; + $node1->random = $node1; + $node2->next = null; + $node2->random = $node2; + $this->assertEquals("[[1,1],[2,1]]", (new Solution())->copyRandomList($node1)); + } + + public function testCopyRandomList3() { + $node31 = new Node(3); + $node32 = new Node(3); + $node33 = new Node(3); + $node31->next = $node32; + $node31->random = null; + $node32->next = $node33; + $node32->random = $node31; + $node33->next = null; + $node33->random = null; + $this->assertEquals("[[3,null],[3,0],[3,null]]", (new Solution())->copyRandomList($node31)); + } +} diff --git a/src/test/php/g0101_0200/s0139_word_break/SolutionTest.php b/src/test/php/g0101_0200/s0139_word_break/SolutionTest.php new file mode 100644 index 0000000..4801ba5 --- /dev/null +++ b/src/test/php/g0101_0200/s0139_word_break/SolutionTest.php @@ -0,0 +1,20 @@ +assertEquals(true, (new Solution())->wordBreak("leetcode", array("leet", "code"))); + } + + public function testWordBreak2() { + $this->assertEquals(true, (new Solution())->wordBreak("applepenapple", array("apple", "pen"))); + } + + public function testWordBreak3() { + $this->assertEquals(false, + (new Solution())->wordBreak("catsandog", array("cats", "dog", "sand", "and", "cat"))); + } +} diff --git a/src/test/php/g0101_0200/s0141_linked_list_cycle/SolutionTest.php b/src/test/php/g0101_0200/s0141_linked_list_cycle/SolutionTest.php new file mode 100644 index 0000000..8371e8c --- /dev/null +++ b/src/test/php/g0101_0200/s0141_linked_list_cycle/SolutionTest.php @@ -0,0 +1,29 @@ +next = new ListNode(2); + $listNode1->next->next = new ListNode(0); + $listNode1->next->next->next = new ListNode(-4); + $listNode1->next->next->next->next = $listNode1->next; + $this->assertEquals(true, (new Solution())->hasCycle($listNode1)); + } + + public function testHasCycle2() { + $listNode1 = new ListNode(1); + $listNode1->next = new ListNode(2); + $listNode1->next->next = $listNode1; + $this->assertEquals(true, (new Solution())->hasCycle($listNode1)); + } + + public function testHasCycle3() { + $listNode1 = new ListNode(1); + $this->assertEquals(false, (new Solution())->hasCycle($listNode1)); + } +} diff --git a/src/test/php/g0101_0200/s0142_linked_list_cycle_ii/SolutionTest.php b/src/test/php/g0101_0200/s0142_linked_list_cycle_ii/SolutionTest.php new file mode 100644 index 0000000..b0e6e6a --- /dev/null +++ b/src/test/php/g0101_0200/s0142_linked_list_cycle_ii/SolutionTest.php @@ -0,0 +1,29 @@ +next = new ListNode(2); + $listNode1->next->next = new ListNode(0); + $listNode1->next->next->next = new ListNode(-4); + $listNode1->next->next->next->next = $listNode1->next; + $this->assertEquals(true, (new Solution())->detectCycle($listNode1) == $listNode1->next); + } + + public function testDetectCycle2() { + $listNode1 = new ListNode(1); + $listNode1->next = new ListNode(2); + $listNode1->next->next = $listNode1; + $this->assertEquals(true, (new Solution())->detectCycle($listNode1) == $listNode1); + } + + public function testDetectCycle3() { + $listNode1 = new ListNode(1); + $this->assertEquals(null, (new Solution())->detectCycle($listNode1)); + } +} diff --git a/src/test/php/g0101_0200/s0146_lru_cache/LRUCacheTest.php b/src/test/php/g0101_0200/s0146_lru_cache/LRUCacheTest.php new file mode 100644 index 0000000..06bedb4 --- /dev/null +++ b/src/test/php/g0101_0200/s0146_lru_cache/LRUCacheTest.php @@ -0,0 +1,29 @@ +put(1, 1); + // cache is {1=1, 2=2} + $lruCache->put(2, 2); + // return 1 + $this->assertEquals(1, $lruCache->get(1)); + // LRU key was 2, evicts key 2, cache is {1=1, 3=3} + $lruCache->put(3, 3); + // returns -1 (not found) + $this->assertEquals(-1, $lruCache->get(2)); + // LRU key was 1, evicts key 1, cache is {4=4, 3=3} + $lruCache->put(4, 4); + // return -1 (not found) + $this->assertEquals(-1, $lruCache->get(1)); + // return 3 + $this->assertEquals(3, $lruCache->get(3)); + // return 4 + $this->assertEquals(4, $lruCache->get(4)); + } +} diff --git a/src/test/php/g0101_0200/s0148_sort_list/SolutionTest.php b/src/test/php/g0101_0200/s0148_sort_list/SolutionTest.php new file mode 100644 index 0000000..a8a78d6 --- /dev/null +++ b/src/test/php/g0101_0200/s0148_sort_list/SolutionTest.php @@ -0,0 +1,29 @@ +next = new ListNode(2); + $listNode1->next->next = new ListNode(1); + $listNode1->next->next->next = new ListNode(3); + $this->assertEquals("1, 2, 3, 4", (new Solution())->sortList($listNode1)->toString()); + } + + public function testSortList2() { + $listNode1 = new ListNode(-1); + $listNode1->next = new ListNode(5); + $listNode1->next->next = new ListNode(3); + $listNode1->next->next->next = new ListNode(4); + $listNode1->next->next->next->next = new ListNode(0); + $this->assertEquals("-1, 0, 3, 4, 5", (new Solution())->sortList($listNode1)->toString()); + } + + public function testSortList3() { + $this->assertEquals(null, (new Solution())->sortList(null)); + } +} diff --git a/src/test/php/g0101_0200/s0152_maximum_product_subarray/SolutionTest.php b/src/test/php/g0101_0200/s0152_maximum_product_subarray/SolutionTest.php new file mode 100644 index 0000000..2f0b0b8 --- /dev/null +++ b/src/test/php/g0101_0200/s0152_maximum_product_subarray/SolutionTest.php @@ -0,0 +1,15 @@ +assertEquals(6, (new Solution())->maxProduct(array(2, 3, -2, 4))); + } + + public function testMaxProduct2() { + $this->assertEquals(0, (new Solution())->maxProduct(array(-2, 0, -1))); + } +} diff --git a/src/test/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/SolutionTest.php b/src/test/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/SolutionTest.php new file mode 100644 index 0000000..f011648 --- /dev/null +++ b/src/test/php/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/SolutionTest.php @@ -0,0 +1,19 @@ +assertEquals(1, (new Solution())->findMin(array(3, 4, 5, 1, 2))); + } + + public function testFindMin2() { + $this->assertEquals(0, (new Solution())->findMin(array(4, 5, 6, 7, 0, 1, 2))); + } + + public function testFindMin3() { + $this->assertEquals(11, (new Solution())->findMin(array(11, 13, 15, 17))); + } +} diff --git a/src/test/php/g0101_0200/s0155_min_stack/MinStackTest.php b/src/test/php/g0101_0200/s0155_min_stack/MinStackTest.php new file mode 100644 index 0000000..6e3ee07 --- /dev/null +++ b/src/test/php/g0101_0200/s0155_min_stack/MinStackTest.php @@ -0,0 +1,21 @@ +push(-2); + $minStack->push(0); + $minStack->push(-3); + // return -3 + $this->assertEquals(-3, $minStack->getMin()); + $minStack->pop(); + // return 0 + $this->assertEquals(0, $minStack->top()); + // return -2 + $this->assertEquals(-2, $minStack->getMin()); + } +} diff --git a/src/test/php/g0101_0200/s0160_intersection_of_two_linked_lists/SolutionTest.php b/src/test/php/g0101_0200/s0160_intersection_of_two_linked_lists/SolutionTest.php new file mode 100644 index 0000000..7bce2a7 --- /dev/null +++ b/src/test/php/g0101_0200/s0160_intersection_of_two_linked_lists/SolutionTest.php @@ -0,0 +1,21 @@ +assertEquals(8, (new Solution())->getIntersectionNode($nodeA, $nodeB)->val); + } + + public function testGetIntersectionNode2() { + $nodeA = new ListNode(4, new ListNode(1, new ListNode(2))); + $nodeB = new ListNode(5, new ListNode(6, new ListNode(1, new ListNode(2)))); + $this->assertEquals(null, (new Solution())->getIntersectionNode($nodeA, $nodeB)); + } +}