[
array
sort
hash table
binary search
]
BinarySearch 0550 Unique Integers in Sorted List
Problem statement
https://binarysearch.com/problems/Unique-Integers-in-Sorted-List/
Solution 1
Just use set here.
Complexity
It is O(n)
for time and O(k)
for space.
Code
class Solution:
def solve(self, nums):
return len(set(nums))
Solution 2
Or we can use binary search to make big steps here.
Complexity
It is O(k log n)
for time and O(1)
for space.
Code
class Solution:
def solve(self, nums):
ans, i = 0, 0
while i < len(nums):
if i < len(nums) - 1 and nums[i] == nums[i + 1]:
i = bisect_right(nums, nums[i])
else:
i += 1
ans += 1
return ans