From 1d4535a154bba9e68c667dce860491e8a163e2dc Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 9 Dec 2023 04:33:44 +0200 Subject: [PATCH] Added tasks 31-46 --- .../php/g0001_0100/s0001_two_sum/Solution.php | 2 +- .../s0002_add_two_numbers/Solution.php | 2 +- .../Solution.php | 2 +- .../Solution.php | 2 +- .../s0006_zigzag_conversion/Solution.php | 2 +- .../s0007_reverse_integer/Solution.php | 6 ++- .../s0008_string_to_integer_atoi/Solution.php | 2 +- .../s0009_palindrome_number/Solution.php | 2 +- .../Solution.php | 5 ++ .../s0022_generate_parentheses/Solution.php | 4 +- .../s0023_merge_k_sorted_lists/Solution.php | 2 +- .../s0024_swap_nodes_in_pairs/Solution.php | 2 +- .../Solution.php | 2 +- .../s0031_next_permutation/Solution.php | 42 +++++++++++++++ .../s0031_next_permutation/readme.md | 38 ++++++++++++++ .../Solution.php | 52 +++++++++++++++++++ .../s0032_longest_valid_parentheses/readme.md | 32 ++++++++++++ .../Solution.php | 38 ++++++++++++++ .../readme.md | 37 +++++++++++++ .../Solution.php | 39 ++++++++++++++ .../readme.md | 34 ++++++++++++ .../s0035_search_insert_position/Solution.php | 30 +++++++++++ .../s0035_search_insert_position/readme.md | 44 ++++++++++++++++ .../s0039_combination_sum/Solution.php | 37 +++++++++++++ .../s0039_combination_sum/readme.md | 52 +++++++++++++++++++ .../s0041_first_missing_positive/Solution.php | 24 +++++++++ .../s0041_first_missing_positive/readme.md | 30 +++++++++++ .../s0042_trapping_rain_water/Solution.php | 33 ++++++++++++ .../s0042_trapping_rain_water/readme.md | 27 ++++++++++ .../s0045_jump_game_ii/Solution.php | 32 ++++++++++++ .../g0001_0100/s0045_jump_game_ii/readme.md | 30 +++++++++++ .../s0046_permutations/Solution.php | 40 ++++++++++++++ .../g0001_0100/s0046_permutations/readme.md | 29 +++++++++++ .../s0031_next_permutation/SolutionTest.php | 25 +++++++++ .../SolutionTest.php | 19 +++++++ .../SolutionTest.php | 19 +++++++ .../SolutionTest.php | 25 +++++++++ .../SolutionTest.php | 19 +++++++ .../s0039_combination_sum/SolutionTest.php | 28 ++++++++++ .../SolutionTest.php | 19 +++++++ .../SolutionTest.php | 15 ++++++ .../s0045_jump_game_ii/SolutionTest.php | 15 ++++++ .../s0046_permutations/SolutionTest.php | 23 ++++++++ 43 files changed, 949 insertions(+), 13 deletions(-) create mode 100644 src/main/php/g0001_0100/s0031_next_permutation/Solution.php create mode 100644 src/main/php/g0001_0100/s0031_next_permutation/readme.md create mode 100644 src/main/php/g0001_0100/s0032_longest_valid_parentheses/Solution.php create mode 100644 src/main/php/g0001_0100/s0032_longest_valid_parentheses/readme.md create mode 100644 src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.php create mode 100644 src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md create mode 100644 src/main/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.php create mode 100644 src/main/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md create mode 100644 src/main/php/g0001_0100/s0035_search_insert_position/Solution.php create mode 100644 src/main/php/g0001_0100/s0035_search_insert_position/readme.md create mode 100644 src/main/php/g0001_0100/s0039_combination_sum/Solution.php create mode 100644 src/main/php/g0001_0100/s0039_combination_sum/readme.md create mode 100644 src/main/php/g0001_0100/s0041_first_missing_positive/Solution.php create mode 100644 src/main/php/g0001_0100/s0041_first_missing_positive/readme.md create mode 100644 src/main/php/g0001_0100/s0042_trapping_rain_water/Solution.php create mode 100644 src/main/php/g0001_0100/s0042_trapping_rain_water/readme.md create mode 100644 src/main/php/g0001_0100/s0045_jump_game_ii/Solution.php create mode 100644 src/main/php/g0001_0100/s0045_jump_game_ii/readme.md create mode 100644 src/main/php/g0001_0100/s0046_permutations/Solution.php create mode 100644 src/main/php/g0001_0100/s0046_permutations/readme.md create mode 100644 src/test/php/g0001_0100/s0031_next_permutation/SolutionTest.php create mode 100644 src/test/php/g0001_0100/s0032_longest_valid_parentheses/SolutionTest.php create mode 100644 src/test/php/g0001_0100/s0033_search_in_rotated_sorted_array/SolutionTest.php create mode 100644 src/test/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/SolutionTest.php create mode 100644 src/test/php/g0001_0100/s0035_search_insert_position/SolutionTest.php create mode 100644 src/test/php/g0001_0100/s0039_combination_sum/SolutionTest.php create mode 100644 src/test/php/g0001_0100/s0041_first_missing_positive/SolutionTest.php create mode 100644 src/test/php/g0001_0100/s0042_trapping_rain_water/SolutionTest.php create mode 100644 src/test/php/g0001_0100/s0045_jump_game_ii/SolutionTest.php create mode 100644 src/test/php/g0001_0100/s0046_permutations/SolutionTest.php diff --git a/src/main/php/g0001_0100/s0001_two_sum/Solution.php b/src/main/php/g0001_0100/s0001_two_sum/Solution.php index dd0c725..00a19f3 100644 --- a/src/main/php/g0001_0100/s0001_two_sum/Solution.php +++ b/src/main/php/g0001_0100/s0001_two_sum/Solution.php @@ -12,7 +12,7 @@ class Solution { * @param Integer $target * @return Integer[] */ - function twoSum($nums, $target) { + public function twoSum($nums, $target) { $ind = []; for ($i = 0; $i < count($nums); ++$i) { $complement = $target - $nums[$i]; diff --git a/src/main/php/g0001_0100/s0002_add_two_numbers/Solution.php b/src/main/php/g0001_0100/s0002_add_two_numbers/Solution.php index 604c609..8b41bbb 100644 --- a/src/main/php/g0001_0100/s0002_add_two_numbers/Solution.php +++ b/src/main/php/g0001_0100/s0002_add_two_numbers/Solution.php @@ -25,7 +25,7 @@ class Solution { * @param ListNode $l2 * @return ListNode */ - function addTwoNumbers($l1, $l2) { + public function addTwoNumbers($l1, $l2) { $dummyHead = new ListNode(0); $p = $l1; $q = $l2; diff --git a/src/main/php/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.php b/src/main/php/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.php index ca60cbc..a625ebb 100644 --- a/src/main/php/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.php +++ b/src/main/php/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.php @@ -11,7 +11,7 @@ class Solution { * @param Integer[] $nums2 * @return Float */ - function findMedianSortedArrays($nums1, $nums2) { + public function findMedianSortedArrays($nums1, $nums2) { $nums3 = []; $sum = count($nums1) + count($nums2); $med = ($sum / 2); diff --git a/src/main/php/g0001_0100/s0005_longest_palindromic_substring/Solution.php b/src/main/php/g0001_0100/s0005_longest_palindromic_substring/Solution.php index 870b3b9..e036a2e 100644 --- a/src/main/php/g0001_0100/s0005_longest_palindromic_substring/Solution.php +++ b/src/main/php/g0001_0100/s0005_longest_palindromic_substring/Solution.php @@ -12,7 +12,7 @@ class Solution { * @param String $s * @return String */ - function longestPalindrome($s): string { + public function longestPalindrome($s): string { if (($length = strlen($s)) <= 1) { return $s; } diff --git a/src/main/php/g0001_0100/s0006_zigzag_conversion/Solution.php b/src/main/php/g0001_0100/s0006_zigzag_conversion/Solution.php index 754f890..d9cdbe7 100644 --- a/src/main/php/g0001_0100/s0006_zigzag_conversion/Solution.php +++ b/src/main/php/g0001_0100/s0006_zigzag_conversion/Solution.php @@ -10,7 +10,7 @@ class Solution { * @param Integer $numRows * @return String */ - function convert($s, $numRows) { + public function convert($s, $numRows) { if ($numRows == 1) { return $s; } diff --git a/src/main/php/g0001_0100/s0007_reverse_integer/Solution.php b/src/main/php/g0001_0100/s0007_reverse_integer/Solution.php index b98420e..bbe3455 100644 --- a/src/main/php/g0001_0100/s0007_reverse_integer/Solution.php +++ b/src/main/php/g0001_0100/s0007_reverse_integer/Solution.php @@ -6,7 +6,11 @@ // #2023_12_03_Time_3_ms_(90.99%)_Space_19.2_MB_(44.23%) class Solution { - function reverse($x) { + /** + * @param Integer $x + * @return Integer + */ + public function reverse($x) { $ls_negative = ''; if (strpos($x, '-') === 0) { $ls_negative = substr($x, 0, 1); diff --git a/src/main/php/g0001_0100/s0008_string_to_integer_atoi/Solution.php b/src/main/php/g0001_0100/s0008_string_to_integer_atoi/Solution.php index 9804c2b..7212b5e 100644 --- a/src/main/php/g0001_0100/s0008_string_to_integer_atoi/Solution.php +++ b/src/main/php/g0001_0100/s0008_string_to_integer_atoi/Solution.php @@ -9,7 +9,7 @@ class Solution { * @param String $s * @return Integer */ - function myAtoi($s) { + public function myAtoi($s) { $pos = 0; $res = 0; for ($i = 0; $i < strlen($s); $i++) { diff --git a/src/main/php/g0001_0100/s0009_palindrome_number/Solution.php b/src/main/php/g0001_0100/s0009_palindrome_number/Solution.php index bf49357..bbd6dae 100644 --- a/src/main/php/g0001_0100/s0009_palindrome_number/Solution.php +++ b/src/main/php/g0001_0100/s0009_palindrome_number/Solution.php @@ -9,7 +9,7 @@ class Solution { * @param Integer $x * @return Boolean */ - function isPalindrome($x) { + public function isPalindrome($x) { if ($x < 0) { return false; } diff --git a/src/main/php/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.php b/src/main/php/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.php index 7b22699..9dc1806 100644 --- a/src/main/php/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.php +++ b/src/main/php/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.php @@ -23,6 +23,11 @@ class Solution { private $n; + /** + * @param ListNode $head + * @param Integer $n + * @return ListNode + */ public function removeNthFromEnd($head, $n) { $this->n = $n; $node = new ListNode(0, $head); diff --git a/src/main/php/g0001_0100/s0022_generate_parentheses/Solution.php b/src/main/php/g0001_0100/s0022_generate_parentheses/Solution.php index 938616d..f8fe3a6 100644 --- a/src/main/php/g0001_0100/s0022_generate_parentheses/Solution.php +++ b/src/main/php/g0001_0100/s0022_generate_parentheses/Solution.php @@ -11,13 +11,13 @@ class Solution { * @param Integer $n * @return String[] */ - function generateParenthesis(int $n): array { + public function generateParenthesis(int $n): array { $sb = ''; $ans = []; return $this->generate($sb, $ans, $n, $n); } - function generate(string &$sb, array &$ans, int $open, int $close): array { + private function generate(string &$sb, array &$ans, int $open, int $close): array { if ($open === 0 && $close === 0) { $ans[] = $sb; return $ans; diff --git a/src/main/php/g0001_0100/s0023_merge_k_sorted_lists/Solution.php b/src/main/php/g0001_0100/s0023_merge_k_sorted_lists/Solution.php index 5ec4930..092d4db 100644 --- a/src/main/php/g0001_0100/s0023_merge_k_sorted_lists/Solution.php +++ b/src/main/php/g0001_0100/s0023_merge_k_sorted_lists/Solution.php @@ -24,7 +24,7 @@ class Solution { * @param ListNode[] $lists * @return ListNode */ - function mergeKLists($lists) { + public function mergeKLists($lists) { $heap = new \SplMinHeap(); $head = []; foreach ($lists as $list) { diff --git a/src/main/php/g0001_0100/s0024_swap_nodes_in_pairs/Solution.php b/src/main/php/g0001_0100/s0024_swap_nodes_in_pairs/Solution.php index 9ddc704..82e3068 100644 --- a/src/main/php/g0001_0100/s0024_swap_nodes_in_pairs/Solution.php +++ b/src/main/php/g0001_0100/s0024_swap_nodes_in_pairs/Solution.php @@ -24,7 +24,7 @@ class Solution { * @param ListNode $head * @return ListNode */ - function swapPairs($head) { + public function swapPairs($head) { $output = new ListNode(0); $output->next = $head; $point = $output; diff --git a/src/main/php/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.php b/src/main/php/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.php index e9f5728..8b385c5 100644 --- a/src/main/php/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.php +++ b/src/main/php/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.php @@ -25,7 +25,7 @@ class Solution { * @param Integer $k * @return ListNode */ - function reverseKGroup($head, $k) { + public function reverseKGroup($head, $k) { if ($this->countNodesInList($head) < $k || $head->next == null || $head == null) { return $head; } else { diff --git a/src/main/php/g0001_0100/s0031_next_permutation/Solution.php b/src/main/php/g0001_0100/s0031_next_permutation/Solution.php new file mode 100644 index 0000000..7e00c49 --- /dev/null +++ b/src/main/php/g0001_0100/s0031_next_permutation/Solution.php @@ -0,0 +1,42 @@ += 0 && $nums[$i] >= $nums[$i + 1]) { + $i--; + } + if ($i >= 0) { + $j = count($nums) - 1; + while ($nums[$j] <= $nums[$i]) { + $j--; + } + $this->swap($nums, $i, $j); + } + $this->reverse($nums, $i + 1, count($nums) - 1); + } + + private function swap(&$nums, $i, $j) { + $temp = $nums[$i]; + $nums[$i] = $nums[$j]; + $nums[$j] = $temp; + } + + private function reverse(&$nums, $i, $j) { + while ($i < $j) { + $this->swap($nums, $i++, $j--); + } + } +} diff --git a/src/main/php/g0001_0100/s0031_next_permutation/readme.md b/src/main/php/g0001_0100/s0031_next_permutation/readme.md new file mode 100644 index 0000000..bf3ed98 --- /dev/null +++ b/src/main/php/g0001_0100/s0031_next_permutation/readme.md @@ -0,0 +1,38 @@ +31\. Next Permutation + +Medium + +Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers. + +If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). + +The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** [1,3,2] + +**Example 2:** + +**Input:** nums = [3,2,1] + +**Output:** [1,2,3] + +**Example 3:** + +**Input:** nums = [1,1,5] + +**Output:** [1,5,1] + +**Example 4:** + +**Input:** nums = [1] + +**Output:** [1] + +**Constraints:** + +* `1 <= nums.length <= 100` +* `0 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/php/g0001_0100/s0032_longest_valid_parentheses/Solution.php b/src/main/php/g0001_0100/s0032_longest_valid_parentheses/Solution.php new file mode 100644 index 0000000..2a04be6 --- /dev/null +++ b/src/main/php/g0001_0100/s0032_longest_valid_parentheses/Solution.php @@ -0,0 +1,52 @@ + $left) { + $left = 0; + $right = 0; + } + if ($left == $right) { + $max = max($max, $left + $right); + } + } + $left = 0; + $right = 0; + for ($i = $n - 1; $i >= 0; $i--) { + $ch = $s[$i]; + if ($ch == '(') { + $left++; + } else { + $right++; + } + if ($left > $right) { + $left = 0; + $right = 0; + } + if ($left == $right) { + $max = max($max, $left + $right); + } + } + return $max; + } +} diff --git a/src/main/php/g0001_0100/s0032_longest_valid_parentheses/readme.md b/src/main/php/g0001_0100/s0032_longest_valid_parentheses/readme.md new file mode 100644 index 0000000..f11141d --- /dev/null +++ b/src/main/php/g0001_0100/s0032_longest_valid_parentheses/readme.md @@ -0,0 +1,32 @@ +32\. Longest Valid Parentheses + +Hard + +Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring. + +**Example 1:** + +**Input:** s = "(()" + +**Output:** 2 + +**Explanation:** The longest valid parentheses substring is "()". + +**Example 2:** + +**Input:** s = ")()())" + +**Output:** 4 + +**Explanation:** The longest valid parentheses substring is "()()". + +**Example 3:** + +**Input:** s = "" + +**Output:** 0 + +**Constraints:** + +* 0 <= s.length <= 3 * 104 +* `s[i]` is `'('`, or `')'`. \ No newline at end of file diff --git a/src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.php b/src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.php new file mode 100644 index 0000000..7d2aad5 --- /dev/null +++ b/src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.php @@ -0,0 +1,38 @@ +> 1) + $lo; + if ($target == $nums[$mid]) { + return $mid; + } + if ($nums[$lo] <= $nums[$mid]) { + if ($nums[$lo] <= $target && $target <= $nums[$mid]) { + $hi = $mid - 1; + } else { + $lo = $mid + 1; + } + } elseif ($nums[$mid] <= $target && $target <= $nums[$hi]) { + $lo = $mid + 1; + } else { + $hi = $mid - 1; + } + } + return -1; + } +} diff --git a/src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md b/src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md new file mode 100644 index 0000000..ebfff91 --- /dev/null +++ b/src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md @@ -0,0 +1,37 @@ +33\. Search in Rotated Sorted Array + +Medium + +There is an integer array `nums` sorted in ascending order (with **distinct** values). + +Prior to being passed to your function, `nums` is **possibly rotated** at an unknown pivot index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,5,6,7]` might be rotated at pivot index `3` and become `[4,5,6,7,0,1,2]`. + +Given the array `nums` **after** the possible rotation and an integer `target`, return _the index of_ `target` _if it is in_ `nums`_, or_ `-1` _if it is not in_ `nums`. + +You must write an algorithm with `O(log n)` runtime complexity. + +**Example 1:** + +**Input:** nums = [4,5,6,7,0,1,2], target = 0 + +**Output:** 4 + +**Example 2:** + +**Input:** nums = [4,5,6,7,0,1,2], target = 3 + +**Output:** -1 + +**Example 3:** + +**Input:** nums = [1], target = 0 + +**Output:** -1 + +**Constraints:** + +* `1 <= nums.length <= 5000` +* -104 <= nums[i] <= 104 +* All values of `nums` are **unique**. +* `nums` is an ascending array that is possibly rotated. +* -104 <= target <= 104 \ No newline at end of file diff --git a/src/main/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.php b/src/main/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.php new file mode 100644 index 0000000..69b8381 --- /dev/null +++ b/src/main/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.php @@ -0,0 +1,39 @@ +helper($nums, $target, false); + $ans[1] = $this->helper($nums, $target, true); + return $ans; + } + + private function helper($nums, $target, $equals) { + $l = 0; + $r = count($nums) - 1; + $result = -1; + while ($l <= $r) { + $mid = $l + intval(($r - $l) / 2); + if ($nums[$mid] == $target) { + $result = $mid; + } + if ($nums[$mid] < $target || ($nums[$mid] == $target && $equals)) { + $l = $mid + 1; + } else { + $r = $mid - 1; + } + } + return $result; + } +} diff --git a/src/main/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md b/src/main/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md new file mode 100644 index 0000000..7c04eb2 --- /dev/null +++ b/src/main/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md @@ -0,0 +1,34 @@ +34\. Find First and Last Position of Element in Sorted Array + +Medium + +Given an array of integers `nums` sorted in non-decreasing order, find the starting and ending position of a given `target` value. + +If `target` is not found in the array, return `[-1, -1]`. + +You must write an algorithm with `O(log n)` runtime complexity. + +**Example 1:** + +**Input:** nums = [5,7,7,8,8,10], target = 8 + +**Output:** [3,4] + +**Example 2:** + +**Input:** nums = [5,7,7,8,8,10], target = 6 + +**Output:** [-1,-1] + +**Example 3:** + +**Input:** nums = [], target = 0 + +**Output:** [-1,-1] + +**Constraints:** + +* 0 <= nums.length <= 105 +* -109 <= nums[i] <= 109 +* `nums` is a non-decreasing array. +* -109 <= target <= 109 \ No newline at end of file diff --git a/src/main/php/g0001_0100/s0035_search_insert_position/Solution.php b/src/main/php/g0001_0100/s0035_search_insert_position/Solution.php new file mode 100644 index 0000000..8c2ac5a --- /dev/null +++ b/src/main/php/g0001_0100/s0035_search_insert_position/Solution.php @@ -0,0 +1,30 @@ + $nums[$mid]) { + $lo = $mid + 1; + } + } + return $lo; + } +} diff --git a/src/main/php/g0001_0100/s0035_search_insert_position/readme.md b/src/main/php/g0001_0100/s0035_search_insert_position/readme.md new file mode 100644 index 0000000..94ead0d --- /dev/null +++ b/src/main/php/g0001_0100/s0035_search_insert_position/readme.md @@ -0,0 +1,44 @@ +35\. Search Insert Position + +Easy + +Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. + +You must write an algorithm with `O(log n)` runtime complexity. + +**Example 1:** + +**Input:** nums = [1,3,5,6], target = 5 + +**Output:** 2 + +**Example 2:** + +**Input:** nums = [1,3,5,6], target = 2 + +**Output:** 1 + +**Example 3:** + +**Input:** nums = [1,3,5,6], target = 7 + +**Output:** 4 + +**Example 4:** + +**Input:** nums = [1,3,5,6], target = 0 + +**Output:** 0 + +**Example 5:** + +**Input:** nums = [1], target = 0 + +**Output:** 0 + +**Constraints:** + +* 1 <= nums.length <= 104 +* -104 <= nums[i] <= 104 +* `nums` contains **distinct** values sorted in **ascending** order. +* -104 <= target <= 104 \ No newline at end of file diff --git a/src/main/php/g0001_0100/s0039_combination_sum/Solution.php b/src/main/php/g0001_0100/s0039_combination_sum/Solution.php new file mode 100644 index 0000000..55f658d --- /dev/null +++ b/src/main/php/g0001_0100/s0039_combination_sum/Solution.php @@ -0,0 +1,37 @@ +combinationSumRec(count($coins), $coins, $amount, $subList, $ans); + return $ans; + } + + private function combinationSumRec($n, $coins, $amount, &$subList, &$ans) { + if ($amount == 0 || $n == 0) { + if ($amount == 0) { + $base = $subList; + array_push($ans, $base); + } + return; + } + if ($amount - $coins[$n - 1] >= 0) { + array_push($subList, $coins[$n - 1]); + $this->combinationSumRec($n, $coins, $amount - $coins[$n - 1], $subList, $ans); + array_pop($subList); + } + $this->combinationSumRec($n - 1, $coins, $amount, $subList, $ans); + } +} diff --git a/src/main/php/g0001_0100/s0039_combination_sum/readme.md b/src/main/php/g0001_0100/s0039_combination_sum/readme.md new file mode 100644 index 0000000..1f2c636 --- /dev/null +++ b/src/main/php/g0001_0100/s0039_combination_sum/readme.md @@ -0,0 +1,52 @@ +39\. Combination Sum + +Medium + +Given an array of **distinct** integers `candidates` and a target integer `target`, return _a list of all **unique combinations** of_ `candidates` _where the chosen numbers sum to_ `target`_._ You may return the combinations in **any order**. + +The **same** number may be chosen from `candidates` an **unlimited number of times**. Two combinations are unique if the frequency of at least one of the chosen numbers is different. + +It is **guaranteed** that the number of unique combinations that sum up to `target` is less than `150` combinations for the given input. + +**Example 1:** + +**Input:** candidates = [2,3,6,7], target = 7 + +**Output:** [[2,2,3],[7]] + +**Explanation:** + + 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. + 7 is a candidate, and 7 = 7. + These are the only two combinations. + +**Example 2:** + +**Input:** candidates = [2,3,5], target = 8 + +**Output:** [[2,2,2,2],[2,3,3],[3,5]] + +**Example 3:** + +**Input:** candidates = [2], target = 1 + +**Output:** [] + +**Example 4:** + +**Input:** candidates = [1], target = 1 + +**Output:** [[1]] + +**Example 5:** + +**Input:** candidates = [1], target = 2 + +**Output:** [[1,1]] + +**Constraints:** + +* `1 <= candidates.length <= 30` +* `1 <= candidates[i] <= 200` +* All elements of `candidates` are **distinct**. +* `1 <= target <= 500` \ No newline at end of file diff --git a/src/main/php/g0001_0100/s0041_first_missing_positive/Solution.php b/src/main/php/g0001_0100/s0041_first_missing_positive/Solution.php new file mode 100644 index 0000000..6d1597d --- /dev/null +++ b/src/main/php/g0001_0100/s0041_first_missing_positive/Solution.php @@ -0,0 +1,24 @@ + 0; + }); + $positiveNumbers = array_flip($positiveNumbers); + for ($i = 1; $i <= count($positiveNumbers) + 1; $i++) { + if (isset($positiveNumbers[$i]) === false) { + return $i; + } + } + } +} diff --git a/src/main/php/g0001_0100/s0041_first_missing_positive/readme.md b/src/main/php/g0001_0100/s0041_first_missing_positive/readme.md new file mode 100644 index 0000000..4cd0b1d --- /dev/null +++ b/src/main/php/g0001_0100/s0041_first_missing_positive/readme.md @@ -0,0 +1,30 @@ +41\. First Missing Positive + +Hard + +Given an unsorted integer array `nums`, return the smallest missing positive integer. + +You must implement an algorithm that runs in `O(n)` time and uses constant extra space. + +**Example 1:** + +**Input:** nums = [1,2,0] + +**Output:** 3 + +**Example 2:** + +**Input:** nums = [3,4,-1,1] + +**Output:** 2 + +**Example 3:** + +**Input:** nums = [7,8,9,11,12] + +**Output:** 1 + +**Constraints:** + +* 1 <= nums.length <= 5 * 105 +* -231 <= nums[i] <= 231 - 1 \ No newline at end of file diff --git a/src/main/php/g0001_0100/s0042_trapping_rain_water/Solution.php b/src/main/php/g0001_0100/s0042_trapping_rain_water/Solution.php new file mode 100644 index 0000000..e8879c9 --- /dev/null +++ b/src/main/php/g0001_0100/s0042_trapping_rain_water/Solution.php @@ -0,0 +1,33 @@ +1 <= n <= 2 * 104 +* 0 <= height[i] <= 105 \ No newline at end of file diff --git a/src/main/php/g0001_0100/s0045_jump_game_ii/Solution.php b/src/main/php/g0001_0100/s0045_jump_game_ii/Solution.php new file mode 100644 index 0000000..25a01df --- /dev/null +++ b/src/main/php/g0001_0100/s0045_jump_game_ii/Solution.php @@ -0,0 +1,32 @@ += count($nums) - $i - 1) { + return $minJump; + } + } + return $minJump; + } +} diff --git a/src/main/php/g0001_0100/s0045_jump_game_ii/readme.md b/src/main/php/g0001_0100/s0045_jump_game_ii/readme.md new file mode 100644 index 0000000..10a43da --- /dev/null +++ b/src/main/php/g0001_0100/s0045_jump_game_ii/readme.md @@ -0,0 +1,30 @@ +45\. Jump Game II + +Medium + +Given an array of non-negative integers `nums`, you are initially positioned at the first index of the array. + +Each element in the array represents your maximum jump length at that position. + +Your goal is to reach the last index in the minimum number of jumps. + +You can assume that you can always reach the last index. + +**Example 1:** + +**Input:** nums = [2,3,1,1,4] + +**Output:** 2 + +**Explanation:** The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. + +**Example 2:** + +**Input:** nums = [2,3,0,1,4] + +**Output:** 2 + +**Constraints:** + +* 1 <= nums.length <= 104 +* `0 <= nums[i] <= 1000` \ No newline at end of file diff --git a/src/main/php/g0001_0100/s0046_permutations/Solution.php b/src/main/php/g0001_0100/s0046_permutations/Solution.php new file mode 100644 index 0000000..cddaddc --- /dev/null +++ b/src/main/php/g0001_0100/s0046_permutations/Solution.php @@ -0,0 +1,40 @@ +permuteRecur($nums, $finalResult, array(), array_fill(0, count($nums), false)); + return $finalResult; + } + + private function permuteRecur($nums, &$finalResult, $currResult, $used) { + if (count($currResult) == count($nums)) { + array_push($finalResult, $currResult); + return; + } + for ($i = 0; $i < count($nums); $i++) { + if ($used[$i]) { + continue; + } + array_push($currResult, $nums[$i]); + $used[$i] = true; + $this->permuteRecur($nums, $finalResult, $currResult, $used); + $used[$i] = false; + array_pop($currResult); + } + } +} diff --git a/src/main/php/g0001_0100/s0046_permutations/readme.md b/src/main/php/g0001_0100/s0046_permutations/readme.md new file mode 100644 index 0000000..4d95634 --- /dev/null +++ b/src/main/php/g0001_0100/s0046_permutations/readme.md @@ -0,0 +1,29 @@ +46\. Permutations + +Medium + +Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] + +**Example 2:** + +**Input:** nums = [0,1] + +**Output:** [[0,1],[1,0]] + +**Example 3:** + +**Input:** nums = [1] + +**Output:** [[1]] + +**Constraints:** + +* `1 <= nums.length <= 6` +* `-10 <= nums[i] <= 10` +* All the integers of `nums` are **unique**. \ No newline at end of file diff --git a/src/test/php/g0001_0100/s0031_next_permutation/SolutionTest.php b/src/test/php/g0001_0100/s0031_next_permutation/SolutionTest.php new file mode 100644 index 0000000..b301d11 --- /dev/null +++ b/src/test/php/g0001_0100/s0031_next_permutation/SolutionTest.php @@ -0,0 +1,25 @@ +nextPermutation($array); + $this->assertEquals(array(1, 3, 2), $array); + } + + public function testNextPermutation2() { + $array = array(3, 2, 1); + (new Solution())->nextPermutation($array); + $this->assertEquals(array(1, 2, 3), $array); + } + + public function testNextPermutation3() { + $array = array(1, 1, 5); + (new Solution())->nextPermutation($array); + $this->assertEquals(array(1, 5, 1), $array); + } +} diff --git a/src/test/php/g0001_0100/s0032_longest_valid_parentheses/SolutionTest.php b/src/test/php/g0001_0100/s0032_longest_valid_parentheses/SolutionTest.php new file mode 100644 index 0000000..c1e9633 --- /dev/null +++ b/src/test/php/g0001_0100/s0032_longest_valid_parentheses/SolutionTest.php @@ -0,0 +1,19 @@ +assertEquals(2, (new Solution())->longestValidParentheses("(()")); + } + + public function testLongestValidParentheses2() { + $this->assertEquals(4, (new Solution())->longestValidParentheses(")()())")); + } + + public function testLongestValidParentheses3() { + $this->assertEquals(0, (new Solution())->longestValidParentheses("")); + } +} diff --git a/src/test/php/g0001_0100/s0033_search_in_rotated_sorted_array/SolutionTest.php b/src/test/php/g0001_0100/s0033_search_in_rotated_sorted_array/SolutionTest.php new file mode 100644 index 0000000..b98d778 --- /dev/null +++ b/src/test/php/g0001_0100/s0033_search_in_rotated_sorted_array/SolutionTest.php @@ -0,0 +1,19 @@ +assertEquals(4, (new Solution())->search(array(4, 5, 6, 7, 0, 1, 2), 0)); + } + + public function testSearch2() { + $this->assertEquals(-1, (new Solution())->search(array(4, 5, 6, 7, 0, 1, 2), 3)); + } + + public function testSearch3() { + $this->assertEquals(-1, (new Solution())->search(array(1), 0)); + } +} diff --git a/src/test/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/SolutionTest.php b/src/test/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/SolutionTest.php new file mode 100644 index 0000000..6cb4a3d --- /dev/null +++ b/src/test/php/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/SolutionTest.php @@ -0,0 +1,25 @@ +searchRange(array(5, 7, 7, 8, 8, 10), 8); + $this->assertEquals($expected, $actual); + } + + public function testSearchRange2() { + $expected = array(-1, -1); + $actual = (new Solution())->searchRange(array(5, 7, 7, 8, 8, 10), 6); + $this->assertEquals($expected, $actual); + } + + public function testSearchRange3() { + $expected = array(-1, -1); + $actual = (new Solution())->searchRange(array(), 0); + $this->assertEquals($expected, $actual); + } +} diff --git a/src/test/php/g0001_0100/s0035_search_insert_position/SolutionTest.php b/src/test/php/g0001_0100/s0035_search_insert_position/SolutionTest.php new file mode 100644 index 0000000..f976437 --- /dev/null +++ b/src/test/php/g0001_0100/s0035_search_insert_position/SolutionTest.php @@ -0,0 +1,19 @@ +assertEquals(2, (new Solution())->searchInsert(array(1, 3, 5, 6), 5)); + } + + public function testSearchInsert2() { + $this->assertEquals(1, (new Solution())->searchInsert(array(1, 3, 5, 6), 2)); + } + + public function testSearchInsert3() { + $this->assertEquals(4, (new Solution())->searchInsert(array(1, 3, 5, 6), 7)); + } +} diff --git a/src/test/php/g0001_0100/s0039_combination_sum/SolutionTest.php b/src/test/php/g0001_0100/s0039_combination_sum/SolutionTest.php new file mode 100644 index 0000000..908431f --- /dev/null +++ b/src/test/php/g0001_0100/s0039_combination_sum/SolutionTest.php @@ -0,0 +1,28 @@ +assertEquals( + array(array(7), array(3, 2, 2)), + (new Solution())->combinationSum(array(2, 3, 6, 7), 7) + ); + } + + public function testCombinationSum2() { + $this->assertEquals( + array(array(5, 3), array(3, 3, 2), array(2, 2, 2, 2)), + (new Solution())->combinationSum(array(2, 3, 5), 8) + ); + } + + public function testCombinationSum3() { + $this->assertEquals( + array(), + (new Solution())->combinationSum(array(2), 1) + ); + } +} diff --git a/src/test/php/g0001_0100/s0041_first_missing_positive/SolutionTest.php b/src/test/php/g0001_0100/s0041_first_missing_positive/SolutionTest.php new file mode 100644 index 0000000..3f16bdc --- /dev/null +++ b/src/test/php/g0001_0100/s0041_first_missing_positive/SolutionTest.php @@ -0,0 +1,19 @@ +assertEquals(3, (new Solution())->firstMissingPositive(array(1, 2, 0))); + } + + public function testFirstMissingPositive2() { + $this->assertEquals(2, (new Solution())->firstMissingPositive(array(3, 4, -1, 1))); + } + + public function testFirstMissingPositive3() { + $this->assertEquals(1, (new Solution())->firstMissingPositive(array(7, 8, 9, 11, 12))); + } +} diff --git a/src/test/php/g0001_0100/s0042_trapping_rain_water/SolutionTest.php b/src/test/php/g0001_0100/s0042_trapping_rain_water/SolutionTest.php new file mode 100644 index 0000000..b52b94c --- /dev/null +++ b/src/test/php/g0001_0100/s0042_trapping_rain_water/SolutionTest.php @@ -0,0 +1,15 @@ +assertEquals(6, (new Solution())->trap(array(0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1))); + } + + public function testTrap2() { + $this->assertEquals(9, (new Solution())->trap(array(4, 2, 0, 3, 2, 5))); + } +} diff --git a/src/test/php/g0001_0100/s0045_jump_game_ii/SolutionTest.php b/src/test/php/g0001_0100/s0045_jump_game_ii/SolutionTest.php new file mode 100644 index 0000000..5ef10a5 --- /dev/null +++ b/src/test/php/g0001_0100/s0045_jump_game_ii/SolutionTest.php @@ -0,0 +1,15 @@ +assertEquals(2, (new Solution())->jump(array(2, 3, 1, 1, 4))); + } + + public function testJump2() { + $this->assertEquals(2, (new Solution())->jump(array(2, 3, 0, 1, 4))); + } +} diff --git a/src/test/php/g0001_0100/s0046_permutations/SolutionTest.php b/src/test/php/g0001_0100/s0046_permutations/SolutionTest.php new file mode 100644 index 0000000..b5b7628 --- /dev/null +++ b/src/test/php/g0001_0100/s0046_permutations/SolutionTest.php @@ -0,0 +1,23 @@ +assertEquals($expected, (new Solution())->permute(array(1, 2, 3))); + } + + public function testPermute2() { + $expected = array(array(0, 1), array(1, 0)); + $this->assertEquals($expected, (new Solution())->permute(array(0, 1))); + } + + public function testPermute3() { + $expected = array(array(1)); + $this->assertEquals($expected, (new Solution())->permute(array(1))); + } +}