Bladeren bron

May Challenge Day 1 done

master
Lachlan Jacob 5 jaren geleden
bovenliggende
commit
11e6628bbb
3 gewijzigde bestanden met toevoegingen van 51 en 0 verwijderingen
  1. 32
    0
      problems/278/main.py
  2. 16
    0
      problems/278/problem.txt
  3. 3
    0
      problems/278/run.sh

+ 32
- 0
problems/278/main.py Bestand weergeven

@@ -0,0 +1,32 @@
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return a bool
# def isBadVersion(version):

# I am assuming that the latest definitely is bad based on the way the problem is worded
# so I will just implement a binary search pretty mcuh

def isBadVersion(n):
return n >= 4

class Solution:
def firstBadVersion(self, n):
# This check cuts out a case quickly, and also makes the logic easier to write
# because negatives and zeros no longer have to be dealt with
if isBadVersion(0):
return 0
knownGood = 0
knownBad = n
while knownBad - knownGood > 1:
# find the mid point
# check whether it's good or bad, and update accordingly
mid = (knownGood + knownBad) // 2
if isBadVersion(mid):
knownBad = mid
else:
knownGood = mid
return knownBad

s = Solution()
print("Expected: 4")
print("Got:", s.firstBadVersion(5))

+ 16
- 0
problems/278/problem.txt Bestand weergeven

@@ -0,0 +1,16 @@
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 will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example:

Given n = 5, and version = 4 is the first bad version.

call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true

Then 4 is the first bad version.


+ 3
- 0
problems/278/run.sh Bestand weergeven

@@ -0,0 +1,3 @@
#!/bin/bash

python3 main.py

Laden…
Annuleren
Opslaan