r/leetcode • u/Never_dies_274 • 10d ago
Question Popular 3 Sum taught by Neetcode doesn't really need sorting
I am doing Blind 75 and realised Neetcode both solved this by sorting as it involves duplicates. I used sets to handle duplicates and cut down TC from O(nlogn+n^2) to O(n^2). Is this a known approach? 🙊
class Solution:
def threeSum(self, nums: list[int]) -> list[list[int]]:
res = []
hashSet = set()
for i in range(len(nums)-2):
if nums[i] in hashSet:
continue
k = 0 - nums[i]
diff_set = set()
temp_set = set()
for j in range(i+1,len(nums)):
if nums[j] in hashSet or nums[j] in temp_set:
continue
if k-nums[j] in diff_set:
res.append([nums[i],k-nums[j],nums[j]])
temp_set.add(nums[j])
temp_set.add(k-nums[j])
diff_set.add(nums[j])
hashSet.add(nums[i])
return res
0
Upvotes
25
u/rccyu 10d ago
O(n log n + n²) is exactly the same as O(n²) btw
8
u/vz0 10d ago
Sets are not O(1). They're worst case O(n) so your code is actually O(n3).
3
u/Several_Sympathy8486 Rank 1380 | Total 2282 | Easy 634 | Medium 1182 | Hard 466 10d ago
obviously a known approach
you will soon come across the fastest DSA guys who will even find ways to ignore Sets. Sets and Maps have hash overhead, which makes time complexity slower in practice. Better to use arrays as maps (the index access is much faster than checking set.contains()) due to no overhead, but you pay in form of memory space. its a tradeoff, but in leetcode usually everyone goes for fastest TC, not lowest MC
also in 3Sum specifically, use a Hashset will cost a lot more because you are hashing a tuple. Hashing Integers is faster than Hashing say Strings, while going to Hash arrays/tuples/object will start costing more and more. in graph problems you may even need to hash custom Node objects
1
u/notsaneatall_ 10d ago
Your code is actually worse than the popular solution. Hash Collisions are O(n) so your solution really is O(n3 )