[
counter
]
BinarySearch 0273 A Number and Its Triple
Problem statement
https://binarysearch.com/problems/A-Number-and-Its-Triple/
Solution
Just use counter, be careful with x = 0
case.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(self, nums):
cnt = Counter(nums)
for x in nums:
if (x != 0 and 3*x in cnt) or (x == 0 and cnt[x] >= 2): return True
return False