[
array
binary search
]
BinarySearch 0546 Coincidence Search
Problem statement
https://binarysearch.com/problems/Coincidence-Search/
Solution
Just simulate process.
Complexity
It is O(n log n)
for time and O(1)
for additional space.
Code
class Solution:
def solve(self, nums):
ans = 0
for n in nums:
beg, end = 0, len(nums) - 1
while beg <= end:
mid = (beg + end) // 2
if nums[mid] < n:
beg = mid + 1
elif nums[mid] > n:
end = mid - 1
else:
ans += 1
break
return ans