Skip to content

Commit ccd47ae

Browse files
author
Igor Karpov
committed
Search Insert Position
First Bad Version (two solutions)
1 parent 0860b14 commit ccd47ae

File tree

5 files changed

+155
-2
lines changed

5 files changed

+155
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
9. [Longest Common Prefix](https://github.com/karpovigorok/leetcode/blob/master/src/LongestCommonPrefix/Solution.php)
2828
10. [Count Primes](https://github.com/karpovigorok/leetcode/blob/master/src/CountPrimes/Solution.php)
2929
11. [Rotate Array](https://github.com/karpovigorok/leetcode/blob/master/src/RotateArray/Solution.php)
30-
12. [Sqrt(x)](https://github.com/karpovigorok/leetcode/blob/master/src/Sqrtx/Solution.php)
30+
12. [Sqrt(x)](https://github.com/karpovigorok/leetcode/blob/master/src/Sqrtx/Solution.php)
3131
13. [Sum of Two Integers](https://github.com/karpovigorok/leetcode/blob/master/src/SumOfTwoIntegers/Solution.php)
3232
14. [Valid Parentheses](https://github.com/karpovigorok/leetcode/blob/master/src/ValidParentheses/Solution.php)
3333
15. [Implement strStr()](https://github.com/karpovigorok/leetcode/blob/master/src/ImplementStrstr/Solution.php)
3434
16. [Merge Sorted Array](https://github.com/karpovigorok/leetcode/blob/master/src/MergeSortedArray/Solution.php)
3535
17. [Remove Duplicates from Sorted Array](https://github.com/karpovigorok/leetcode/blob/master/src/RemoveDuplicatesFromSortedArray/Solution.php)
36-
18. [Min Stack](https://github.com/karpovigorok/leetcode/blob/master/result/min-stack.php)
36+
18. [Min Stack](https://github.com/karpovigorok/leetcode/blob/master/result/min-stack.php)
37+
19. [Merge Two Sorted Lists](https://github.com/karpovigorok/leetcode/blob/master/src/MergeTwoSortedLists/Solution.php)
38+
20. [Search Insert Position](https://github.com/karpovigorok/leetcode/blob/master/src/SearchInsertPosition/Solution.php)
39+
21. [First Bad Version](https://github.com/karpovigorok/leetcode/blob/master/src/FirstBadVersion/Solution.php)

result/first-bad-version.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require "../vendor/autoload.php";
4+
5+
use Leetcode\FirstBadVersion\Solution;
6+
7+
$solution = new Solution();
8+
echo $solution->firstBadVersion(2);

result/search-insert-position.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
require "../vendor/autoload.php";
3+
4+
use Leetcode\SearchInsertPosition\Solution;
5+
6+
$solution = new Solution();
7+
8+
$nums = [-1];
9+
$target = 0;
10+
print $solution->searchInsert($nums, $target);

src/FirstBadVersion/Solution.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Leetcode\FirstBadVersion;
4+
5+
/**
6+
* Project: leetcode
7+
* Problem URL: https://leetcode.com/problems/first-bad-version/
8+
* Difficulty: Easy
9+
* 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.
10+
* 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.
11+
* 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.
12+
*
13+
* Author: Igor Karpov <[email protected]>
14+
* Date: 1/8/2020
15+
* Time: 6:07 PM
16+
* Class Solution
17+
18+
19+
20+
21+
/* The isBadVersion API is defined in the parent class VersionControl.
22+
public function isBadVersion($version){} */
23+
24+
class VersionControl {
25+
private $_bad_version = 2;
26+
public function isBadVersion($version): bool
27+
{
28+
//print $version . " is " . intval($this->_bad_version <= $version) . PHP_EOL;
29+
return ($this->_bad_version <= $version);
30+
}
31+
}
32+
33+
class Solution extends VersionControl {
34+
/**
35+
* @param Integer $n
36+
* @return Integer
37+
*/
38+
private $_right = 0;
39+
40+
public function firstBadVersion($n): int {
41+
$start = 1;
42+
if($n <= 1) {
43+
return $n;
44+
}
45+
$end = $n;
46+
while($start < $end) {
47+
$position2check = intdiv(($start + $end), 2 );
48+
if($this->isBadVersion($position2check)) {
49+
if($position2check == 1) return 1;
50+
if($end - $start == 1) return $end;
51+
$end = $position2check;
52+
}
53+
else {
54+
if($start == $position2check) {
55+
$start = $end;
56+
}
57+
else {
58+
$start = $position2check;
59+
}
60+
}
61+
}
62+
return $end;
63+
}
64+
65+
public function firstBadVersionWithRecurtion($n): int
66+
{
67+
if($this->_right == 0) {
68+
$this->_right = $n;
69+
}
70+
if($n <= 0) {
71+
return $n;
72+
}
73+
if($this->isBadVersion($n)) {
74+
if(!$this->isBadVersion($n - 1)) {
75+
return $n;
76+
}
77+
else {
78+
$position2check = ceil($n / 2);
79+
$this->_right = $n;
80+
//print __LINE__ . ': ' . $position2check . PHP_EOL;
81+
return $this->firstBadVersion($position2check);
82+
}
83+
}
84+
else {
85+
if($this->isBadVersion($n + 1)) {
86+
return $n + 1;
87+
}
88+
$position2check = ceil(($n + $this->_right) / 2);
89+
//print __LINE__ . ': ' . $position2check . PHP_EOL;
90+
return $this->firstBadVersion($position2check);
91+
}
92+
}
93+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* Project: leetcode
5+
* Problem URL: https://leetcode.com/problems/search-insert-position/
6+
* Difficulty: Easy
7+
* 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.
8+
* You must write an algorithm with O(log n) runtime complexity.
9+
* Author: Igor Karpov <[email protected]>
10+
* Date: 30.07.21
11+
* Time: 11:14
12+
* Class Solution
13+
* @package Leetcode\MergeTwoSortedLists
14+
*/
15+
16+
17+
namespace Leetcode\SearchInsertPosition;
18+
19+
class Solution
20+
{
21+
/**
22+
* @param Integer[] $nums
23+
* @param Integer $target
24+
* @return Integer
25+
*/
26+
function searchInsert($nums, $target) {
27+
if(sizeof($nums)) {
28+
foreach ($nums as $key => $current) {
29+
if($current >= $target) {
30+
return $key;
31+
}
32+
}
33+
return ++$key;
34+
}
35+
else {
36+
return 0;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)