-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First Bad Version (two solutions)
- Loading branch information
Igor Karpov
committed
Jul 30, 2021
1 parent
0860b14
commit ccd47ae
Showing
5 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
require "../vendor/autoload.php"; | ||
|
||
use Leetcode\FirstBadVersion\Solution; | ||
|
||
$solution = new Solution(); | ||
echo $solution->firstBadVersion(2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
require "../vendor/autoload.php"; | ||
|
||
use Leetcode\SearchInsertPosition\Solution; | ||
|
||
$solution = new Solution(); | ||
|
||
$nums = [-1]; | ||
$target = 0; | ||
print $solution->searchInsert($nums, $target); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Leetcode\FirstBadVersion; | ||
|
||
/** | ||
* Project: leetcode | ||
* Problem URL: https://leetcode.com/problems/first-bad-version/ | ||
* Difficulty: Easy | ||
* Description: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. | ||
* Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad. | ||
* You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API. | ||
* | ||
* Author: Igor Karpov <[email protected]> | ||
* 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* Project: leetcode | ||
* Problem URL: https://leetcode.com/problems/search-insert-position/ | ||
* Difficulty: Easy | ||
* Description: 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. | ||
* Author: Igor Karpov <[email protected]> | ||
* 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; | ||
} | ||
} | ||
} |