# CSPT10 Build Week 1 ``` """ Understand: input: 16 output: 4 input: 8 output: 2 (actually 2.82) input: 1 output: 1 Plan: Use binary search to keep halving search space Keep track of nearest solution found (in else case) """ class Solution: def mySqrt(self, x: int) -> int: min, max = 0, x res = 0 while min <= max: mid = int((min + max) / 2) squared = mid * mid if squared == x: return int(mid) elif squared > x: max = mid - 1 else: min = mid + 1 res = mid return int(res) class Solution: def mySqrtBruteForce(self, x: int) -> int: for i in range(0, x + 1): squared = i * i if squared == x: return i elif squared > x: return i - 1 return -1 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up