Skip to content

Commit

Permalink
Search Insert Position
Browse files Browse the repository at this point in the history
First Bad Version (two solutions)
  • Loading branch information
Igor Karpov committed Jul 30, 2021
1 parent 0860b14 commit ccd47ae
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
8 changes: 8 additions & 0 deletions result/first-bad-version.php
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);
10 changes: 10 additions & 0 deletions result/search-insert-position.php
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);
93 changes: 93 additions & 0 deletions src/FirstBadVersion/Solution.php
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);
}
}
}
39 changes: 39 additions & 0 deletions src/SearchInsertPosition/Solution.php
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;
}
}
}

0 comments on commit ccd47ae

Please sign in to comment.