2670. Find the Distinct Difference Array
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 76% Topics: Array, Hash Table
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
# hash table, prefix sum
class Solution(object):
def distinctDifferenceArray(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
result = [0]*len(nums)
lookup = set()
for i in xrange(len(nums)):
lookup.add(nums[i])
result[i] = len(lookup)
lookup.clear()
for i in reversed(xrange(len(nums))):
result[i] -= len(lookup)
lookup.add(nums[i])
return result
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions