Skip to content

Commit 0da4a51

Browse files
committed
added problem 0374
1 parent b8e5a56 commit 0da4a51

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Diff for: problems/problem0374/guess_higher_lower.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package problem0374
2+
3+
/*
4+
We are playing the Guess Game. The game is as follows:
5+
I pick a number from 1 to n. You have to guess which number I picked.
6+
Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
7+
You call a pre-defined API int guess(int num), which returns three possible results:
8+
-1: Your guess is higher than the number I picked (i.e. num > pick).
9+
1: Your guess is lower than the number I picked (i.e. num < pick).
10+
0: your guess is equal to the number I picked (i.e. num == pick).
11+
Return the number that I picked.
12+
*/
13+
14+
func guessNumber(n int, guess func(int) int) int {
15+
var start, mid int
16+
mid = n / 2
17+
for {
18+
switch guess(mid) {
19+
case -1:
20+
n = mid - 1
21+
case 1:
22+
start = mid + 1
23+
case 0:
24+
return mid
25+
}
26+
mid = ((n - start) / 2) + start
27+
}
28+
}

Diff for: problems/problem0374/guess_higher_lower_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem0374
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input int
12+
Expected int
13+
}
14+
15+
var Results = []Result{
16+
{10, 6},
17+
{1, 1},
18+
{12, 4},
19+
}
20+
21+
func TestGuessHigherLower(t *testing.T) {
22+
assert := assert.New(t)
23+
24+
for _, res := range Results {
25+
n := res.Expected
26+
want := res.Expected
27+
guess := func(i int) int {
28+
if i == n {
29+
return 0
30+
} else if i > n {
31+
return -1
32+
} else {
33+
return 1
34+
}
35+
}
36+
got := guessNumber(res.Input, guess)
37+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
38+
}
39+
}

Diff for: readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Each problem is in it's own directory, with test files. There are helper package
122122
| 0345 | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string) | [My Solution](./problems/problem0345) ||
123123
| 0349 | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays) | [My Solution](./problems/problem0349) ||
124124
| 0363 | [Max Sum of Rectangle No Larger Than ](https://leetcode.com/problems/max-sum-of-rectangle-no-larger-than-k) | [My Solution](./problems/problem0363) ||
125+
| 0374 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower) | [My Solution](./problems/problem0374) ||
125126
| 0376 | [Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence) | [My Solution](./problems/problem0376) ||
126127
| 0377 | [Combination Sum IV](https://leetcode.com/problems/combination-sum-iv) | [My Solution](./problems/problem0377) ||
127128
| 0378 | [Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix) | [My Solution](./problems/problem0378) ||

0 commit comments

Comments
 (0)