I would like to get more into algorithms, so I need your tips and guidance. Please feel free to recommend books, courses, and websites to improve my competitive programming.
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
I would like to get more into algorithms, so I need your tips and guidance. Please feel free to recommend books, courses, and websites to improve my competitive programming.
Binary search logic:
Suppose there is a set of numbers of 100 count. And I want to guess a number I'd start right in the middle that'd be 50. Then, if the guess is too low I'd take the remaining 50 and divide it by two and add it to the 50. We get 75. Too high ? Lower it using the same procedure. It'll be 63. Too high? Next, we do the same procedure and get 57. We'll go like this till we guess right.
To count max numbers of trials we take n=100 and use logarithms. The answer will be log100 (base2) = 7 (6.64)
in python, it would look like this:
def binary_search(list, item):
low = 0 high = len(list)-1 while low <= high: mid = (low + high) guess = list[mid] if guess == item: return mid if guess > item: high = mid - 1 else: low = mid + 1 return None
my_list = [1, 3, 5, 7, 9]
print(binary_search(my_list, 3)) # => 1
print(binary_search(my_list, -1)) # => None
Name |
---|