-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8df006
commit 3b456e6
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
1_Easy_LeetCode_Questions/leetcode_374_guess-number-higher-or-lower.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# The guess API is already defined for you. | ||
# @param num, your guess | ||
# @return -1 if num is higher than the picked number | ||
# 1 if num is lower than the picked number | ||
# otherwise return 0 | ||
# def guess(num: int) -> int: | ||
|
||
# How the 'guess()' self-made function might look like: | ||
# (where 'num' represents the number guessed by the player) | ||
def guess(num): | ||
pick = 2 | ||
if num > pick: | ||
return -1 | ||
elif num < pick: | ||
return 1 | ||
else: | ||
return 0 | ||
|
||
|
||
|
||
# This brute force method fails because its Big O Notation of Time Complexity is O(n), causing the | ||
# error 'Time exceeded'. | ||
# class Solution: | ||
# def guessNumber(self, n: int) -> int: | ||
|
||
# guessed_number = 10 | ||
|
||
# while True: | ||
# higher_equal_lower = guess(guessed_number) | ||
|
||
# # This means that the 'guessed_number' is higher than the picked number | ||
# if higher_equal_lower == 1: | ||
# guessed_number += 1 | ||
# # This means that the 'guessed_number' is lower than the picked number | ||
# elif higher_equal_lower == -1: | ||
# guessed_number -= 1 | ||
# # This means that the 'guessed_number' is equal than the picked number | ||
# else: | ||
# return guessed_number | ||
# print(guessed_number) | ||
|
||
|
||
|
||
# Using more efficient Search Algorithms with better Big O Notation of Time Complexity to find the | ||
# number being picked. | ||
|
||
# Using the Iterative Binary Search Algorithm, which has a Big O Notation of Time Complexity of | ||
# O(log n) | ||
class Solution: | ||
def guessNumber(self, n: int) -> int: | ||
|
||
# The Iterative Binary Search Algorithm | ||
left_limit = 0 | ||
right_limit = n | ||
|
||
while left_limit <= right_limit: | ||
guessed_number = (left_limit + right_limit) // 2 | ||
higher_equal_lower = guess(guessed_number) | ||
|
||
if higher_equal_lower == 0: | ||
return guessed_number | ||
|
||
if higher_equal_lower == 1: | ||
left_limit = guessed_number + 1 | ||
else: | ||
right_limit = guessed_number - 1 | ||
|
||
print(f"'guessed_number': {guessed_number}") | ||
|
||
# If the picked number is not in specified range of numbers that the picked number can be | ||
# chosen from, here we will 'return -1' | ||
return -1 | ||
|
||
|
||
range_of_numbers_that_the_picked_number_can_be_chosen_from = 2 | ||
|
||
solution = Solution() | ||
|
||
print(solution.guessNumber(range_of_numbers_that_the_picked_number_can_be_chosen_from)) |