Master Data Structures & Algorithms for FREE at AlgoMap.io/
@JoeTan-nq4fq2 ай бұрын
If it is a k sum problem, does it mean we need to resort to recursive solution (until we end up with 2 sum problem)?
@mitranshuraj28115 ай бұрын
Feels kinda bad to understand these topics😂
@gourav69684 ай бұрын
oi, why is it?
@mitranshuraj28114 ай бұрын
@@gourav69682sum, 3sum, 4sum, dp...shall I go on?
@donkeykong561611 күн бұрын
Better than neetcode lol
@Fen-i3n3 ай бұрын
sorry but I dont know how to do 3 sum😮💨
@xingyuxiang163719 күн бұрын
Build a graph and then traverse it. Hashing on nodes, integers, or IDs. class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: nums.sort() n = len(nums) seen = set() ans = set() for i in range(n): for j in range(i + 1, n): lastNumber = - nums[i] - nums[j] if lastNumber in seen: ans.add((nums[i], nums[j], lastNumber)) seen.add(nums[i]) return ans class Solution: def fourSum(self, nums: List[int], target: int) -> List[List[int]]: nums.sort() n = len(nums) seen = set() ans = set() for i in range(n): for j in range(i + 1, n): for k in range(j + 1, n): lastNumber = target - nums[i] - nums[j] - nums[k] if lastNumber in seen: ans.add((nums[i], nums[j], nums[k], lastNumber)) seen.add(nums[i]) return ans