Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Search] Add solution for Guess Number Higher or Lower #337

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 323 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 324 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.

## Contributors

Expand Down Expand Up @@ -369,6 +369,7 @@
[Sqrt(x)](https://leetcode.com/problems/sqrtx/)| [Swift](./Search/Sqrtx.swift)| Medium| O(logn)| O(1)|
[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)| [Swift](./Search/MedianTwoSortedArrays.swift)| Hard| O(log(m + n))| O(1)|
[Minimize Max Distance to Gas Station](https://leetcode.com/problems/minimize-max-distance-to-gas-station/)| [Swift](./Search/MinimizeMaxDistanceGasStation.swift)| Hard| O(nlogm)| O(1)|
[Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)| [Swift](./Search/GuessNumberHigherOrLower.swift)| Easy| O(logn)| O(1)|

## Sort
| Title | Solution | Difficulty | Time | Space |
Expand Down Expand Up @@ -546,7 +547,7 @@
| [Swift](./DFS/CombinationSumIV.swift) | 377 | [Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | Medium
| | 376 | [Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | Medium
| [Swift](./DP/GuessNumberHigherOrLowerII.swift) | 375 | [Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/) | Medium
| | 374 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/) | Easy
| [Swift](./Search/GuessNumberHigherOrLower.swift) | 374 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/) | Easy
| | 373 | [Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) | Medium
| [Swift](./Math/SuperPow.swift) | 372 | [Super Pow](https://leetcode.com/problems/super-pow/) | Medium
| [Swift](./Math/SumTwoIntegers.swift) | 371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | Easy
Expand Down
34 changes: 34 additions & 0 deletions Search/GuessNumberHigherOrLower.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Question Link: https://leetcode.com/problems/guess-number-higher-or-lower/
* Primary idea: use binary search to find number
* Time Complexity: O(logn), Space Complexity: O(1)
*
* Forward declaration of guess API.
* @param num -> your guess number
* @return -1 if num is higher than the picked number
* 1 if num is lower than the picked number
* otherwise return 0
* func guess(_ num: Int) -> Int
*/

class GuessNumberHigherOrLower {
func guessNumber(_ n: Int) -> Int {
var start = 1
var end = n

while start <= end {
let mid = start + (end - start) / 2
let isGuessed = guess(mid)

if isGuessed == 0 {
return mid
} else if isGuessed == -1 {
end = mid - 1
} else {
start = mid + 1
}
}

return -1
}
}