diff --git a/README.md b/README.md index e40c1f1..2cb95bb 100755 --- a/README.md +++ b/README.md @@ -27,10 +27,13 @@ 9. [Longest Common Prefix](https://github.com/karpovigorok/leetcode/blob/master/src/LongestCommonPrefix/Solution.php) 10. [Count Primes](https://github.com/karpovigorok/leetcode/blob/master/src/CountPrimes/Solution.php) 11. [Rotate Array](https://github.com/karpovigorok/leetcode/blob/master/src/RotateArray/Solution.php) -12. [Sqrt(x)](https://github.com/karpovigorok/leetcode/blob/master/src/Sqrtx/Solution.php) +12. [Sqrt(x)](https://github.com/karpovigorok/leetcode/blob/master/src/Sqrtx/Solution.php) 13. [Sum of Two Integers](https://github.com/karpovigorok/leetcode/blob/master/src/SumOfTwoIntegers/Solution.php) 14. [Valid Parentheses](https://github.com/karpovigorok/leetcode/blob/master/src/ValidParentheses/Solution.php) 15. [Implement strStr()](https://github.com/karpovigorok/leetcode/blob/master/src/ImplementStrstr/Solution.php) 16. [Merge Sorted Array](https://github.com/karpovigorok/leetcode/blob/master/src/MergeSortedArray/Solution.php) 17. [Remove Duplicates from Sorted Array](https://github.com/karpovigorok/leetcode/blob/master/src/RemoveDuplicatesFromSortedArray/Solution.php) -18. [Min Stack](https://github.com/karpovigorok/leetcode/blob/master/result/min-stack.php) \ No newline at end of file +18. [Min Stack](https://github.com/karpovigorok/leetcode/blob/master/result/min-stack.php) +19. [Merge Two Sorted Lists](https://github.com/karpovigorok/leetcode/blob/master/src/MergeTwoSortedLists/Solution.php) +20. [Search Insert Position](https://github.com/karpovigorok/leetcode/blob/master/src/SearchInsertPosition/Solution.php) +21. [First Bad Version](https://github.com/karpovigorok/leetcode/blob/master/src/FirstBadVersion/Solution.php) \ No newline at end of file diff --git a/result/first-bad-version.php b/result/first-bad-version.php new file mode 100644 index 0000000..9edf0b0 --- /dev/null +++ b/result/first-bad-version.php @@ -0,0 +1,8 @@ +firstBadVersion(2); \ No newline at end of file diff --git a/result/search-insert-position.php b/result/search-insert-position.php new file mode 100644 index 0000000..6336fe0 --- /dev/null +++ b/result/search-insert-position.php @@ -0,0 +1,10 @@ +searchInsert($nums, $target); \ No newline at end of file diff --git a/src/FirstBadVersion/Solution.php b/src/FirstBadVersion/Solution.php new file mode 100644 index 0000000..e39930f --- /dev/null +++ b/src/FirstBadVersion/Solution.php @@ -0,0 +1,93 @@ + + * Date: 1/8/2020 + * Time: 6:07 PM + * Class Solution + + + + +/* The isBadVersion API is defined in the parent class VersionControl. + public function isBadVersion($version){} */ + +class VersionControl { + private $_bad_version = 2; + public function isBadVersion($version): bool + { + //print $version . " is " . intval($this->_bad_version <= $version) . PHP_EOL; + return ($this->_bad_version <= $version); + } +} + +class Solution extends VersionControl { + /** + * @param Integer $n + * @return Integer + */ + private $_right = 0; + + public function firstBadVersion($n): int { + $start = 1; + if($n <= 1) { + return $n; + } + $end = $n; + while($start < $end) { + $position2check = intdiv(($start + $end), 2 ); + if($this->isBadVersion($position2check)) { + if($position2check == 1) return 1; + if($end - $start == 1) return $end; + $end = $position2check; + } + else { + if($start == $position2check) { + $start = $end; + } + else { + $start = $position2check; + } + } + } + return $end; + } + + public function firstBadVersionWithRecurtion($n): int + { + if($this->_right == 0) { + $this->_right = $n; + } + if($n <= 0) { + return $n; + } + if($this->isBadVersion($n)) { + if(!$this->isBadVersion($n - 1)) { + return $n; + } + else { + $position2check = ceil($n / 2); + $this->_right = $n; + //print __LINE__ . ': ' . $position2check . PHP_EOL; + return $this->firstBadVersion($position2check); + } + } + else { + if($this->isBadVersion($n + 1)) { + return $n + 1; + } + $position2check = ceil(($n + $this->_right) / 2); + //print __LINE__ . ': ' . $position2check . PHP_EOL; + return $this->firstBadVersion($position2check); + } + } +} \ No newline at end of file diff --git a/src/SearchInsertPosition/Solution.php b/src/SearchInsertPosition/Solution.php new file mode 100644 index 0000000..1151673 --- /dev/null +++ b/src/SearchInsertPosition/Solution.php @@ -0,0 +1,39 @@ + + * Date: 30.07.21 + * Time: 11:14 + * Class Solution + * @package Leetcode\MergeTwoSortedLists + */ + + +namespace Leetcode\SearchInsertPosition; + +class Solution +{ + /** + * @param Integer[] $nums + * @param Integer $target + * @return Integer + */ + function searchInsert($nums, $target) { + if(sizeof($nums)) { + foreach ($nums as $key => $current) { + if($current >= $target) { + return $key; + } + } + return ++$key; + } + else { + return 0; + } + } +} \ No newline at end of file