leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 26ff224f42305cd88b999873c5759d0e9b396ac8
parent e6f15ec9044de2531e5dfb22d828ce0fb4c1d165
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Sun,  9 Apr 2023 16:00:16 -0500

Completed guessing binary search problem

Diffstat:
Aguess_number/a.out | 0
Aguess_number/guess-number-higher-or-lower.cpp | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/guess_number/a.out b/guess_number/a.out Binary files differ. diff --git a/guess_number/guess-number-higher-or-lower.cpp b/guess_number/guess-number-higher-or-lower.cpp @@ -0,0 +1,55 @@ +#include <iostream> + +using namespace std; + + +//Leet Ratings +// Speed Memory +//Total 0ms 6.1MB +//Beats 100% 8.48% + + +//This is the function to return if guess is higher or lower than the actual value. +int guess(int n){ + if(n > 5){ + return -1; + } + if(n < 5){ + return 1; + } + if(n == 5){ + return 0; + } + return 100; + +} + +//This is the logic from the leet-code problem that loops through the list using a binary search until the answer is guessed. +int guessNumber(int n) { + unsigned long int lower_bound = 0; + unsigned long int upper_bound = n; + while(true){ + unsigned long int guess_int = (lower_bound + upper_bound) / 2; + unsigned long int guess_val = guess(guess_int + 1); + if(guess_val == -1){ + upper_bound = guess_int; + } + if(guess_val == 1){ + lower_bound = guess_int; + } + if(guess_val == 0){ + return guess_int + 1; + } + } + return 0; +} + +//Constructor method. +int main(){ + int number = guessNumber(5); + cout << number; + + +} + +