Skip to content

Commit 72d6e3a

Browse files
committed
leetcode
1 parent fb6357e commit 72d6e3a

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
3+
4+
-* 374. Guess Number Higher or Lower *-
5+
6+
We are playing the Guess Game. The game is as follows:
7+
8+
I pick a number from 1 to n. You have to guess which number I picked.
9+
10+
Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
11+
12+
You call a pre-defined API int guess(int num), which returns three possible results:
13+
14+
-1: Your guess is higher than the number I picked (i.e. num > pick).
15+
1: Your guess is lower than the number I picked (i.e. num < pick).
16+
0: your guess is equal to the number I picked (i.e. num == pick).
17+
Return the number that I picked.
18+
19+
20+
21+
Example 1:
22+
23+
Input: n = 10, pick = 6
24+
Output: 6
25+
Example 2:
26+
27+
Input: n = 1, pick = 1
28+
Output: 1
29+
Example 3:
30+
31+
Input: n = 2, pick = 1
32+
Output: 1
33+
34+
35+
Constraints:
36+
37+
1 <= n <= 231 - 1
38+
1 <= pick <= n
39+
Accepted
40+
416,581
41+
Submissions
42+
815,531
43+
44+
*/
45+
46+
int guess(int number) => number;
47+
48+
class A {
49+
int guessNumber(int n) {
50+
int low = 1;
51+
int high = n;
52+
while (low <= high) {
53+
int mid = low + (high - low) ~/ 2;
54+
int g = guess(mid);
55+
if (g == 0) {
56+
return mid;
57+
} else if (g == -1) {
58+
high = mid - 1;
59+
} else {
60+
low = mid + 1;
61+
}
62+
}
63+
return -1;
64+
}
65+
}
66+
67+
class B {
68+
int guessNumber(int n) {
69+
int i = 1, mid = n ~/ 2, k, l = 0;
70+
if (guess(mid) == 0)
71+
return mid;
72+
else if (guess(mid) == -1) {
73+
k = mid;
74+
l = 0;
75+
} else
76+
k = n;
77+
l = mid;
78+
for (i = l; i < k; i++) {
79+
if (guess(i) == 0) break;
80+
}
81+
return i;
82+
}
83+
}
84+
85+
class C {
86+
int guessNumber(int n) {
87+
return binarySearch(0, n);
88+
}
89+
90+
int binarySearch(int start, int end) {
91+
if (start > end) return -1;
92+
int mid = start + (end - start) ~/ 2;
93+
switch (guess(mid)) {
94+
case -1:
95+
return binarySearch(start, mid - 1);
96+
case 1:
97+
return binarySearch(mid + 1, end);
98+
default:
99+
return mid;
100+
}
101+
}
102+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
//* Forward declaration of guess API.
4+
//* @param num your guess
5+
//* @return -1 if num is higher than the picked number
6+
//* 1 if num is lower than the picked number
7+
//* otherwise return 0
8+
func guess(num int) int
9+
10+
// func guessNumber(n int) int {
11+
// var low int = 1
12+
// var high int = n
13+
// for low <= high {
14+
// var mid int = low + (high-low)/2
15+
// var g int = guess(mid)
16+
// if g == 0 {
17+
// return mid
18+
// } else if g == -1 {
19+
// high = mid - 1
20+
// } else {
21+
// low = mid + 1
22+
// }
23+
// }
24+
25+
// return -1
26+
// }
27+
28+
func guessNumber(n int) int {
29+
return binarySearch(0, n)
30+
}
31+
32+
func binarySearch(start, finish int) int {
33+
if start > finish {
34+
return -1
35+
}
36+
dot := (finish-start)/2 + start
37+
38+
switch guess(dot) {
39+
case -1:
40+
return binarySearch(start, dot-1)
41+
case 1:
42+
return binarySearch(dot+1, finish)
43+
default:
44+
return dot
45+
}
46+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 🔥 Guess Number Higher or Lower 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Guess API
4+
5+
```dart
6+
Forward declaration of guess API.
7+
@param number your guess
8+
@return -1 if number is higher than the picked number
9+
1 if number is lower than the picked number
10+
otherwise return 0
11+
```
12+
13+
```dart
14+
int guess(int number) => number;
15+
```
16+
17+
## Solution - 1
18+
19+
```dart
20+
class Solution {
21+
int guessNumber(int n) {
22+
int low = 1;
23+
int high = n;
24+
while (low <= high) {
25+
int mid = low + (high - low) ~/ 2;
26+
int g = guess(mid);
27+
if (g == 0) {
28+
return mid;
29+
} else if (g == -1) {
30+
high = mid - 1;
31+
} else {
32+
low = mid + 1;
33+
}
34+
}
35+
return -1;
36+
}
37+
}
38+
```
39+
40+
## Solution - 2
41+
42+
```dart
43+
class Solution {
44+
int guessNumber(int n) {
45+
int i = 1, mid = n ~/ 2, k, l = 0;
46+
if (guess(mid) == 0)
47+
return mid;
48+
else if (guess(mid) == -1) {
49+
k = mid;
50+
l = 0;
51+
} else
52+
k = n;
53+
l = mid;
54+
for (i = l; i < k; i++) {
55+
if (guess(i) == 0) break;
56+
}
57+
return i;
58+
}
59+
}
60+
```
61+
62+
## Solution - 3
63+
64+
```dart
65+
class Solution {
66+
int guessNumber(int n) {
67+
return binarySearch(0, n);
68+
}
69+
70+
int binarySearch(int start, int end) {
71+
if (start > end) return -1;
72+
int mid = start + (end - start) ~/ 2;
73+
switch (guess(mid)) {
74+
case -1:
75+
return binarySearch(start, mid - 1);
76+
case 1:
77+
return binarySearch(mid + 1, end);
78+
default:
79+
return mid;
80+
}
81+
}
82+
}
83+
```
84+
85+
### 😈 Disclaimer 😈 :-
86+
87+
This Solution is not available in DART Programing language with is a bummer. Hurts my feeling. But as a man we should implement it no matter what. We are not bunch of wussies who gonna skip it if it's not available in one language we love. Instead we will conquer the sea and rivers and cross the mountains so see what's lies beyond our horizons.
88+
89+
### [GitHub Link](https://github.com/ayoubzulfiqar/leetcode)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
129129
- [**222.** Count Complete Tree Nodes](CountCompleteTreeNodes/count_complete_tree_nodes.dart)
130130
- [**326.** Power of Three](PowerOfThree/power_of_three.dart)
131131
- [**338.** Counting Bits](CountingBits/counting_bits.dart)
132+
- [**374.** Guess Number Higher or Lower](GuessNumberHigherOrLower/guess_number_higher_or_lower.dart)
132133

133134
## Reach me via
134135

0 commit comments

Comments
 (0)