2465. Number of Distinct Averages
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 58% Topics: Array, Hash Table, Two Pointers, Sorting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(nlogn)
# Space: O(n)
# sort, two pointers, hash table
class Solution(object):
def distinctAverages(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
lookup = set()
nums.sort()
left, right = 0, len(nums)-1
while left < right:
lookup.add(nums[left]+nums[right])
left, right = left+1, right-1
return len(lookup)
Solution from kamyu104/LeetCode-Solutions · MIT