diff --git a/README.md b/README.md index 94f91a0..085d1f6 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0300 |[Longest Increasing Subsequence](src/main/php/g0201_0300/s0300_longest_increasing_subsequence/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 24 | 90.91 #### Day 4 @@ -119,6 +120,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0287 |[Find the Duplicate Number](src/main/php/g0201_0300/s0287_find_the_duplicate_number/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Big_O_Time_O(n)_Space_O(n) | 174 | 76.92 #### Day 6 @@ -134,6 +136,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0240 |[Search a 2D Matrix II](src/main/php/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 47 | 95.24 #### Day 9 @@ -300,6 +303,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0300 |[Longest Increasing Subsequence](src/main/php/g0201_0300/s0300_longest_increasing_subsequence/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 24 | 90.91 #### Day 19 @@ -348,6 +352,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0283 |[Move Zeroes](src/main/php/g0201_0300/s0283_move_zeroes/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 42 | 88.34 #### Day 7 Array @@ -716,6 +721,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- | 0019 |[Remove Nth Node From End of List](src/main/php/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 4 | 81.93 +| 0234 |[Palindrome Linked List](src/main/php/g0201_0300/s0234_palindrome_linked_list/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 123 | 95.45 #### Day 4 Linked List @@ -750,6 +756,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0230 |[Kth Smallest Element in a BST](src/main/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/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(n) | 11 | 75.00 #### Day 10 Graph/BFS/DFS @@ -847,11 +854,14 @@ 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 +| 0283 |[Move Zeroes](src/main/php/g0201_0300/s0283_move_zeroes/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 42 | 88.34 | 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 +| 0238 |[Product of Array Except Self](src/main/php/g0201_0300/s0238_product_of_array_except_self/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 58 | 86.76 | 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 +| 0239 |[Sliding Window Maximum](src/main/php/g0201_0300/s0239_sliding_window_maximum/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Big_O_Time_O(n\*k)_Space_O(n+k) | 518 | 100.00 #### Udemy Two Pointers @@ -892,6 +902,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 | 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 +| 0234 |[Palindrome Linked List](src/main/php/g0201_0300/s0234_palindrome_linked_list/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 123 | 95.45 | 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 @@ -906,6 +917,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | 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 +| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 14 | 85.71 #### Udemy Trie and Heap @@ -928,6 +940,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 | 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 +| 0300 |[Longest Increasing Subsequence](src/main/php/g0201_0300/s0300_longest_increasing_subsequence/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 24 | 90.91 | 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 | 0010 |[Regular Expression Matching](src/main/php/g0001_0100/s0010_regular_expression_matching/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Recursion, Big_O_Time_O(m\*n)_Space_O(m\*n) | 10 | 64.52 @@ -1065,11 +1078,13 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0240 |[Search a 2D Matrix II](src/main/php/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Big_O_Time_O(n+m)_Space_O(1) | 47 | 95.24 #### Day 5 Array | | | | | | |-|-|-|-|-|- +| 0238 |[Product of Array Except Self](src/main/php/g0201_0300/s0238_product_of_array_except_self/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 58 | 86.76 #### Day 6 String @@ -1139,11 +1154,13 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0230 |[Kth Smallest Element in a BST](src/main/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/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(n) | 11 | 75.00 #### Day 18 Tree | | | | | | |-|-|-|-|-|- +| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 14 | 85.71 #### Day 19 Graph @@ -1179,6 +1196,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0283 |[Move Zeroes](src/main/php/g0201_0300/s0283_move_zeroes/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 42 | 88.34 #### Day 4 Two Pointers @@ -1341,6 +1359,7 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 0300 |[Longest Increasing Subsequence](src/main/php/g0201_0300/s0300_longest_increasing_subsequence/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 24 | 90.91 #### Day 17 Dynamic Programming @@ -1372,6 +1391,16 @@ Php-based LeetCode algorithm problem solutions, regularly updated. | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|--------- +| 0300 |[Longest Increasing Subsequence](src/main/php/g0201_0300/s0300_longest_increasing_subsequence/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming, Big_O_Time_O(n\*log_n)_Space_O(n) | 24 | 90.91 +| 0295 |[Find Median from Data Stream](src/main/php/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 332 | 75.00 +| 0287 |[Find the Duplicate Number](src/main/php/g0201_0300/s0287_find_the_duplicate_number/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 174 | 76.92 +| 0283 |[Move Zeroes](src/main/php/g0201_0300/s0283_move_zeroes/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 42 | 88.34 +| 0240 |[Search a 2D Matrix II](src/main/php/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8, Big_O_Time_O(n+m)_Space_O(1) | 47 | 95.24 +| 0239 |[Sliding Window Maximum](src/main/php/g0201_0300/s0239_sliding_window_maximum/Solution.php)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n\*k)_Space_O(n+k) | 518 | 100.00 +| 0238 |[Product of Array Except Self](src/main/php/g0201_0300/s0238_product_of_array_except_self/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum, Data_Structure_II_Day_5_Array, Udemy_Arrays, Big_O_Time_O(n^2)_Space_O(n) | 58 | 86.76 +| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 14 | 85.71 +| 0234 |[Palindrome Linked List](src/main/php/g0201_0300/s0234_palindrome_linked_list/Solution.php)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Stack, Linked_List, Recursion, Level_2_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 123 | 95.45 +| 0230 |[Kth Smallest Element in a BST](src/main/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.php)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_II_Day_17_Tree, Level_2_Day_9_Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 11 | 75.00 | 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 diff --git a/src/main/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.php b/src/main/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.php new file mode 100644 index 0000000..2ca6eb6 --- /dev/null +++ b/src/main/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution.php @@ -0,0 +1,58 @@ +val = $val; + * $this->left = $left; + * $this->right = $right; + * } + * } + */ +class Solution { + private $k; + private $count = 0; + private $val; + + /** + * @param TreeNode $root + * @param Integer $k + * @return Integer + */ + public function kthSmallest($root, $k) { + $this->k = $k; + $this->calculate($root); + return $this->val; + } + + private function calculate($node) { + if ($node->left == null && $node->right == null) { + $this->count++; + if ($this->count == $this->k) { + $this->val = $node->val; + } + return; + } + if ($node->left != null) { + $this->calculate($node->left); + } + $this->count++; + if ($this->count == $this->k) { + $this->val = $node->val; + return; + } + if ($node->right != null) { + $this->calculate($node->right); + } + } +} diff --git a/src/main/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md b/src/main/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md new file mode 100644 index 0000000..840d758 --- /dev/null +++ b/src/main/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/readme.md @@ -0,0 +1,29 @@ +230\. Kth Smallest Element in a BST + +Medium + +Given the `root` of a binary search tree, and an integer `k`, return _the_ kth _smallest value (**1-indexed**) of all the values of the nodes in the tree_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg) + +**Input:** root = [3,1,4,null,2], k = 1 + +**Output:** 1 + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg) + +**Input:** root = [5,3,6,2,4,null,null,1], k = 3 + +**Output:** 3 + +**Constraints:** + +* The number of nodes in the tree is `n`. +* 1 <= k <= n <= 104 +* 0 <= Node.val <= 104 + +**Follow up:** If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize? \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0234_palindrome_linked_list/Solution.php b/src/main/php/g0201_0300/s0234_palindrome_linked_list/Solution.php new file mode 100644 index 0000000..9c50b19 --- /dev/null +++ b/src/main/php/g0201_0300/s0234_palindrome_linked_list/Solution.php @@ -0,0 +1,41 @@ +val = $val; + * $this->next = $next; + * } + * } + */ +class Solution { + /** + * @param ListNode $head + * @return Boolean + */ + public function isPalindrome($head) { + $array = []; + while ($head) { + $array[] = $head->val; + $head = $head->next; + } + $cnt = count($array); + for ($x = 0; $x < $cnt / 2; $x++) { + $yKey = $cnt - 1 - $x; + + if ($array[$x] !== $array[$yKey]) { + return false; + } + } + return true; + } +} diff --git a/src/main/php/g0201_0300/s0234_palindrome_linked_list/readme.md b/src/main/php/g0201_0300/s0234_palindrome_linked_list/readme.md new file mode 100644 index 0000000..096f401 --- /dev/null +++ b/src/main/php/g0201_0300/s0234_palindrome_linked_list/readme.md @@ -0,0 +1,28 @@ +234\. Palindrome Linked List + +Easy + +Given the `head` of a singly linked list, return `true` if it is a palindrome. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg) + +**Input:** head = [1,2,2,1] + +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg) + +**Input:** head = [1,2] + +**Output:** false + +**Constraints:** + +* The number of nodes in the list is in the range [1, 105]. +* `0 <= Node.val <= 9` + +**Follow up:** Could you do it in `O(n)` time and `O(1)` space? \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.php b/src/main/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.php new file mode 100644 index 0000000..cd67968 --- /dev/null +++ b/src/main/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.php @@ -0,0 +1,42 @@ +val = $value; } + * } + */ +class Solution { + /** + * @param TreeNode $root + * @param TreeNode $p + * @param TreeNode $q + * @return TreeNode + */ + public function lowestCommonAncestor($root, $p, $q) { + if ($root == null) { + return null; + } + if ($root->val == $p->val || $root->val == $q->val) { + return $root; + } + $left = $this->lowestCommonAncestor($root->left, $p, $q); + $right = $this->lowestCommonAncestor($root->right, $p, $q); + if ($left != null && $right != null) { + return $root; + } + if ($left != null) { + return $left; + } + return $right; + } +} diff --git a/src/main/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md b/src/main/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md new file mode 100644 index 0000000..6d39dba --- /dev/null +++ b/src/main/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/readme.md @@ -0,0 +1,41 @@ +236\. Lowest Common Ancestor of a Binary Tree + +Medium + +Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. + +According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/Lowest_common_ancestor): “The lowest common ancestor is defined between two nodes `p` and `q` as the lowest node in `T` that has both `p` and `q` as descendants (where we allow **a node to be a descendant of itself**).” + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png) + +**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 + +**Output:** 3 + +**Explanation:** The LCA of nodes 5 and 1 is 3. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png) + +**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 + +**Output:** 5 + +**Explanation:** The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition. + +**Example 3:** + +**Input:** root = [1,2], p = 1, q = 2 + +**Output:** 1 + +**Constraints:** + +* The number of nodes in the tree is in the range [2, 105]. +* -109 <= Node.val <= 109 +* All `Node.val` are **unique**. +* `p != q` +* `p` and `q` will exist in the tree. \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0238_product_of_array_except_self/Solution.php b/src/main/php/g0201_0300/s0238_product_of_array_except_self/Solution.php new file mode 100644 index 0000000..412e697 --- /dev/null +++ b/src/main/php/g0201_0300/s0238_product_of_array_except_self/Solution.php @@ -0,0 +1,35 @@ +2 <= nums.length <= 105 +* `-30 <= nums[i] <= 30` +* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer. + +**Follow up:** Can you solve the problem in `O(1) `extra space complexity? (The output array **does not** count as extra space for space complexity analysis.) \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0239_sliding_window_maximum/Solution.php b/src/main/php/g0201_0300/s0239_sliding_window_maximum/Solution.php new file mode 100644 index 0000000..63c60dc --- /dev/null +++ b/src/main/php/g0201_0300/s0239_sliding_window_maximum/Solution.php @@ -0,0 +1,39 @@ +isEmpty() && $dq->top() < $nums[$j]) { + $dq->pop(); + } + $dq->push($nums[$j]); + if ($j - $i + 1 == $k) { + $res[$x] = $dq->bottom(); + ++$x; + if ($dq->bottom() == $nums[$i]) { + $dq->shift(); + } + ++$i; + } + ++$j; + } + return $res; + } +} diff --git a/src/main/php/g0201_0300/s0239_sliding_window_maximum/readme.md b/src/main/php/g0201_0300/s0239_sliding_window_maximum/readme.md new file mode 100644 index 0000000..e566874 --- /dev/null +++ b/src/main/php/g0201_0300/s0239_sliding_window_maximum/readme.md @@ -0,0 +1,54 @@ +239\. Sliding Window Maximum + +Hard + +You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position. + +Return _the max sliding window_. + +**Example 1:** + +**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3 + +**Output:** [3,3,5,5,6,7] + +**Explanation:** + + Window position Max + --------------- ----- + [1 3 -1] -3 5 3 6 7 3 + 1 [3 -1 -3] 5 3 6 7 3 + 1 3 [-1 -3 5] 3 6 7 5 + 1 3 -1 [-3 5 3] 6 7 5 + 1 3 -1 -3 [5 3 6] 7 6 + 1 3 -1 -3 5 [3 6 7] 7 + +**Example 2:** + +**Input:** nums = [1], k = 1 + +**Output:** [1] + +**Example 3:** + +**Input:** nums = [1,-1], k = 1 + +**Output:** [1,-1] + +**Example 4:** + +**Input:** nums = [9,11], k = 2 + +**Output:** [11] + +**Example 5:** + +**Input:** nums = [4,-2], k = 2 + +**Output:** [4] + +**Constraints:** + +* 1 <= nums.length <= 105 +* -104 <= nums[i] <= 104 +* `1 <= k <= nums.length` \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.php b/src/main/php/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.php new file mode 100644 index 0000000..d1f75da --- /dev/null +++ b/src/main/php/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.php @@ -0,0 +1,29 @@ += 0) { + if ($matrix[$r][$c] == $target) { + return true; + } elseif ($matrix[$r][$c] > $target) { + $c--; + } else { + $r++; + } + } + return false; + } +} diff --git a/src/main/php/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md b/src/main/php/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md new file mode 100644 index 0000000..2e111b7 --- /dev/null +++ b/src/main/php/g0201_0300/s0240_search_a_2d_matrix_ii/readme.md @@ -0,0 +1,34 @@ +240\. Search a 2D Matrix II + +Medium + +Write an efficient algorithm that searches for a `target` value in an `m x n` integer `matrix`. The `matrix` has the following properties: + +* Integers in each row are sorted in ascending from left to right. +* Integers in each column are sorted in ascending from top to bottom. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg) + +**Input:** matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5 + +**Output:** true + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg) + +**Input:** matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20 + +**Output:** false + +**Constraints:** + +* `m == matrix.length` +* `n == matrix[i].length` +* `1 <= n, m <= 300` +* -109 <= matrix[i][j] <= 109 +* All the integers in each row are **sorted** in ascending order. +* All the integers in each column are **sorted** in ascending order. +* -109 <= target <= 109 \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0283_move_zeroes/Solution.php b/src/main/php/g0201_0300/s0283_move_zeroes/Solution.php new file mode 100644 index 0000000..3fe4bfa --- /dev/null +++ b/src/main/php/g0201_0300/s0283_move_zeroes/Solution.php @@ -0,0 +1,29 @@ +swap($firstZero, $i, $nums); + $firstZero++; + } + } + } + + private function swap($index1, $index2, &$numbers) { + $val2 = $numbers[$index2]; + $numbers[$index2] = $numbers[$index1]; + $numbers[$index1] = $val2; + } +} diff --git a/src/main/php/g0201_0300/s0283_move_zeroes/readme.md b/src/main/php/g0201_0300/s0283_move_zeroes/readme.md new file mode 100644 index 0000000..a826cd7 --- /dev/null +++ b/src/main/php/g0201_0300/s0283_move_zeroes/readme.md @@ -0,0 +1,26 @@ +283\. Move Zeroes + +Easy + +Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements. + +**Note** that you must do this in-place without making a copy of the array. + +**Example 1:** + +**Input:** nums = [0,1,0,3,12] + +**Output:** [1,3,12,0,0] + +**Example 2:** + +**Input:** nums = [0] + +**Output:** [0] + +**Constraints:** + +* 1 <= nums.length <= 104 +* -231 <= nums[i] <= 231 - 1 + +**Follow up:** Could you minimize the total number of operations done? \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0287_find_the_duplicate_number/Solution.php b/src/main/php/g0201_0300/s0287_find_the_duplicate_number/Solution.php new file mode 100644 index 0000000..b72d4f9 --- /dev/null +++ b/src/main/php/g0201_0300/s0287_find_the_duplicate_number/Solution.php @@ -0,0 +1,24 @@ +1 <= n <= 105 +* `nums.length == n + 1` +* `1 <= nums[i] <= n` +* All the integers in `nums` appear only **once** except for **precisely one integer** which appears **two or more** times. + +**Follow up:** + +* How can we prove that at least one duplicate number must exist in `nums`? +* Can you solve the problem in linear runtime complexity? \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.php b/src/main/php/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.php new file mode 100644 index 0000000..f47d41b --- /dev/null +++ b/src/main/php/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.php @@ -0,0 +1,57 @@ +maxHeap = new \SplMaxHeap(); + $this->minHeap = new \SplMinHeap(); + } + + /** + * @param Integer $num + * @return NULL + */ + public function addNum($num) { + if ($this->maxHeap->isEmpty() || $this->maxHeap->top() > $num) { + $this->maxHeap->insert($num); + } else { + $this->minHeap->insert($num); + } + if (abs($this->maxHeap->count() - $this->minHeap->count()) > 1) { + $this->balance($this->maxHeap, $this->minHeap); + } + } + + function balance($maxHeap, $minHeap) { + $large = $maxHeap->count() > $minHeap->count() ? $maxHeap : $minHeap; + $small = $maxHeap->count() > $minHeap->count() ? $minHeap : $maxHeap; + $small->insert($large->extract()); + } + + /** + * @return Float + */ + public function findMedian() { + $large = $this->maxHeap->count() > $this->minHeap->count() ? $this->maxHeap : $this->minHeap; + $small = $this->maxHeap->count() > $this->minHeap->count() ? $this->minHeap : $this->maxHeap; + if ($large->count() == $small->count()) { + return ($large->top() + $small->top()) / 2.0; + } else { + return $large->top(); + } + } +} +/** + * Your MedianFinder object will be instantiated and called as such: + * $obj = MedianFinder(); + * $obj->addNum($num); + * $ret_2 = $obj->findMedian(); + */ diff --git a/src/main/php/g0201_0300/s0295_find_median_from_data_stream/readme.md b/src/main/php/g0201_0300/s0295_find_median_from_data_stream/readme.md new file mode 100644 index 0000000..3928225 --- /dev/null +++ b/src/main/php/g0201_0300/s0295_find_median_from_data_stream/readme.md @@ -0,0 +1,43 @@ +295\. Find Median from Data Stream + +Hard + +The **median** is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values. + +* For example, for `arr = [2,3,4]`, the median is `3`. +* For example, for `arr = [2,3]`, the median is `(2 + 3) / 2 = 2.5`. + +Implement the MedianFinder class: + +* `MedianFinder()` initializes the `MedianFinder` object. +* `void addNum(int num)` adds the integer `num` from the data stream to the data structure. +* `double findMedian()` returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted. + +**Example 1:** + +**Input** + + ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"] + [[], [1], [2], [], [3], []] + +**Output:** [null, null, null, 1.5, null, 2.0] + +**Explanation:** + + MedianFinder medianFinder = new MedianFinder(); + medianFinder.addNum(1); // arr = [1] + medianFinder.addNum(2); // arr = [1, 2] + medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2) + medianFinder.addNum(3); // arr[1, 2, 3] + medianFinder.findMedian(); // return 2.0 + +**Constraints:** + +* -105 <= num <= 105 +* There will be at least one element in the data structure before calling `findMedian`. +* At most 5 * 104 calls will be made to `addNum` and `findMedian`. + +**Follow up:** + +* If all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution? +* If `99%` of all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution? \ No newline at end of file diff --git a/src/main/php/g0201_0300/s0300_longest_increasing_subsequence/Solution.php b/src/main/php/g0201_0300/s0300_longest_increasing_subsequence/Solution.php new file mode 100644 index 0000000..b48b5ed --- /dev/null +++ b/src/main/php/g0201_0300/s0300_longest_increasing_subsequence/Solution.php @@ -0,0 +1,50 @@ + $curr) { + $end = $mid; + } else { + $start = $mid; + } + } + // update our dp table + if ($dp[$start] > $curr) { + $dp[$start] = $curr; + } elseif ($curr > $dp[$start] && $curr < $dp[$end]) { + $dp[$end] = $curr; + } elseif ($curr > $dp[$end]) { + $dp[++$end] = $curr; + $right++; + } + } + return $right; + } +} diff --git a/src/main/php/g0201_0300/s0300_longest_increasing_subsequence/readme.md b/src/main/php/g0201_0300/s0300_longest_increasing_subsequence/readme.md new file mode 100644 index 0000000..4b09231 --- /dev/null +++ b/src/main/php/g0201_0300/s0300_longest_increasing_subsequence/readme.md @@ -0,0 +1,34 @@ +300\. Longest Increasing Subsequence + +Medium + +Given an integer array `nums`, return the length of the longest strictly increasing subsequence. + +A **subsequence** is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, `[3,6,2,7]` is a subsequence of the array `[0,3,1,6,2,2,7]`. + +**Example 1:** + +**Input:** nums = [10,9,2,5,3,7,101,18] + +**Output:** 4 + +**Explanation:** The longest increasing subsequence is [2,3,7,101], therefore the length is 4. + +**Example 2:** + +**Input:** nums = [0,1,0,3,2,3] + +**Output:** 4 + +**Example 3:** + +**Input:** nums = [7,7,7,7,7,7,7] + +**Output:** 1 + +**Constraints:** + +* `1 <= nums.length <= 2500` +* -104 <= nums[i] <= 104 + +**Follow up:** Can you come up with an algorithm that runs in `O(n log(n))` time complexity? \ No newline at end of file diff --git a/src/test/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/SolutionTest.php b/src/test/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/SolutionTest.php new file mode 100644 index 0000000..4569c19 --- /dev/null +++ b/src/test/php/g0201_0300/s0230_kth_smallest_element_in_a_bst/SolutionTest.php @@ -0,0 +1,18 @@ +assertEquals(1, (new Solution())->kthSmallest($root, 1)); + } + + public function testKthSmallest2() { + $root = TreeNode::create(array(5, 3, 6, 2, 4, null, null, 1)); + $this->assertEquals(3, (new Solution())->kthSmallest($root, 3)); + } +} diff --git a/src/test/php/g0201_0300/s0234_palindrome_linked_list/SolutionTest.php b/src/test/php/g0201_0300/s0234_palindrome_linked_list/SolutionTest.php new file mode 100644 index 0000000..6acf000 --- /dev/null +++ b/src/test/php/g0201_0300/s0234_palindrome_linked_list/SolutionTest.php @@ -0,0 +1,22 @@ +next = new ListNode(2); + $headActual->next->next = new ListNode(2); + $headActual->next->next->next = new ListNode(1); + $this->assertEquals(true, (new Solution())->isPalindrome($headActual)); + } + + public function testIsPalindrome2() { + $headActual = new ListNode(1); + $headActual->next = new ListNode(2); + $this->assertEquals(false, (new Solution())->isPalindrome($headActual)); + } +} diff --git a/src/test/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/SolutionTest.php b/src/test/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/SolutionTest.php new file mode 100644 index 0000000..b63ccff --- /dev/null +++ b/src/test/php/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/SolutionTest.php @@ -0,0 +1,31 @@ +assertEquals(3, (new Solution())->lowestCommonAncestor($root, new TreeNode(5), new TreeNode(1))->val); + } + + public function testLowestCommonAncestor2() { + $leftNodeLeftNode = new TreeNode(6); + $leftNodeRightNode = new TreeNode(2, new TreeNode(7), new TreeNode(4)); + $leftNode = new TreeNode(5, $leftNodeLeftNode, $leftNodeRightNode); + $rightNode = new TreeNode(1, new TreeNode(0), new TreeNode(8)); + $root = new TreeNode(3, $leftNode, $rightNode); + $this->assertEquals(5, (new Solution())->lowestCommonAncestor($root, new TreeNode(5), new TreeNode(4))->val); + } + + public function testLowestCommonAncestor3() { + $this->assertEquals(2, (new Solution())->lowestCommonAncestor( + new TreeNode(2, new TreeNode(1), null), new TreeNode(2), new TreeNode(1))->val); + } +} diff --git a/src/test/php/g0201_0300/s0238_product_of_array_except_self/SolutionTest.php b/src/test/php/g0201_0300/s0238_product_of_array_except_self/SolutionTest.php new file mode 100644 index 0000000..9dbae88 --- /dev/null +++ b/src/test/php/g0201_0300/s0238_product_of_array_except_self/SolutionTest.php @@ -0,0 +1,15 @@ +assertEquals(array(24, 12, 8, 6), (new Solution())->productExceptSelf(array(1, 2, 3, 4))); + } + + public function testProductExceptSelf2() { + $this->assertEquals(array(0, 0, 9, 0, 0), (new Solution())->productExceptSelf(array(-1, 1, 0, -3, 3))); + } +} diff --git a/src/test/php/g0201_0300/s0239_sliding_window_maximum/SolutionTest.php b/src/test/php/g0201_0300/s0239_sliding_window_maximum/SolutionTest.php new file mode 100644 index 0000000..adc4333 --- /dev/null +++ b/src/test/php/g0201_0300/s0239_sliding_window_maximum/SolutionTest.php @@ -0,0 +1,16 @@ +assertEquals(array(3, 3, 5, 5, 6, 7), (new Solution())->maxSlidingWindow( + array(1, 3, -1, -3, 5, 3, 6, 7), 3)); + } + + public function testMaxSlidingWindow2() { + $this->assertEquals(array(1), (new Solution())->maxSlidingWindow(array(1), 1)); + } +} diff --git a/src/test/php/g0201_0300/s0240_search_a_2d_matrix_ii/SolutionTest.php b/src/test/php/g0201_0300/s0240_search_a_2d_matrix_ii/SolutionTest.php new file mode 100644 index 0000000..0d8030d --- /dev/null +++ b/src/test/php/g0201_0300/s0240_search_a_2d_matrix_ii/SolutionTest.php @@ -0,0 +1,29 @@ +assertEquals(true, (new Solution())->searchMatrix($matrix, 5)); + } + + public function testSearchMatrix2() { + $matrix = array( + array(1, 4, 7, 11, 15), + array(2, 5, 8, 12, 19), + array(3, 6, 9, 16, 22), + array(10, 13, 14, 17, 24), + array(18, 21, 23, 26, 30) + ); + $this->assertEquals(false, (new Solution())->searchMatrix($matrix, 20)); + } +} diff --git a/src/test/php/g0201_0300/s0283_move_zeroes/SolutionTest.php b/src/test/php/g0201_0300/s0283_move_zeroes/SolutionTest.php new file mode 100644 index 0000000..f1623cc --- /dev/null +++ b/src/test/php/g0201_0300/s0283_move_zeroes/SolutionTest.php @@ -0,0 +1,19 @@ +moveZeroes($array); + $this->assertEquals(array(1, 3, 12, 0, 0), $array); + } + + public function testMoveZeroes2() { + $array = array(0); + (new Solution())->moveZeroes($array); + $this->assertEquals(array(0), $array); + } +} diff --git a/src/test/php/g0201_0300/s0287_find_the_duplicate_number/SolutionTest.php b/src/test/php/g0201_0300/s0287_find_the_duplicate_number/SolutionTest.php new file mode 100644 index 0000000..443d347 --- /dev/null +++ b/src/test/php/g0201_0300/s0287_find_the_duplicate_number/SolutionTest.php @@ -0,0 +1,23 @@ +assertEquals(2, (new Solution())->findDuplicate(array(1, 3, 4, 2, 2))); + } + + public function testFindDuplicate2() { + $this->assertEquals(3, (new Solution())->findDuplicate(array(3, 1, 3, 4, 2))); + } + + public function testFindDuplicate3() { + $this->assertEquals(1, (new Solution())->findDuplicate(array(1, 1))); + } + + public function testFindDuplicate4() { + $this->assertEquals(1, (new Solution())->findDuplicate(array(1, 1, 2))); + } +} diff --git a/src/test/php/g0201_0300/s0295_find_median_from_data_stream/MedianFinderTest.php b/src/test/php/g0201_0300/s0295_find_median_from_data_stream/MedianFinderTest.php new file mode 100644 index 0000000..93e619b --- /dev/null +++ b/src/test/php/g0201_0300/s0295_find_median_from_data_stream/MedianFinderTest.php @@ -0,0 +1,29 @@ +addNum(1); + // arr = [1, 2] + $medianFinder->addNum(2); + // return 1.5 (i.e., (1 + 2) / 2) + $this->assertEquals(1.5, $medianFinder->findMedian()); + // arr[1, 2, 3] + $medianFinder->addNum(3); + // return 2.0 + $this->assertEquals(2.0, $medianFinder->findMedian()); + } + + public function testMedianFinder2() { + $medianFinder = new MedianFinder(); + $medianFinder->addNum(1); + $medianFinder->addNum(3); + $medianFinder->addNum(-1); + $this->assertEquals(1.0, $medianFinder->findMedian()); + } +} diff --git a/src/test/php/g0201_0300/s0300_longest_increasing_subsequence/SolutionTest.php b/src/test/php/g0201_0300/s0300_longest_increasing_subsequence/SolutionTest.php new file mode 100644 index 0000000..bde7dec --- /dev/null +++ b/src/test/php/g0201_0300/s0300_longest_increasing_subsequence/SolutionTest.php @@ -0,0 +1,19 @@ +assertEquals(4, (new Solution())->lengthOfLIS(array(10, 9, 2, 5, 3, 7, 101, 18))); + } + + public function testLengthOfLIS2() { + $this->assertEquals(4, (new Solution())->lengthOfLIS(array(0, 1, 0, 3, 2, 3))); + } + + public function testLengthOfLIS3() { + $this->assertEquals(1, (new Solution())->lengthOfLIS(array(7, 7, 7, 7, 7, 7, 7))); + } +}