4Sum - Leetcode 18 - Two Pointers (Python)
ฝัง
- เผยแพร่เมื่อ 10 ก.พ. 2025
- Master Data Structures & Algorithms for FREE at AlgoMap.io/
Code solutions in Python, Java, C++ and JS for this can be found at my GitHub repo here: github.com/gah...
Complete DSA Pathway Zero to Hero: • Data Structures & Algo...
Please check my playlists for free DSA problem solutions:
• Fundamental DSA Theory
• Array & String Questions
• 2 Pointers Questions
• Sliding Window Questions
• Binary Search Questions
• Stack Questions
• Linked List Questions
• Tree Questions
• Heap Questions
• Recursive Backtracking...
• Graph Questions
• Dynamic Programming (D...
My Data Science & ML TH-cam Playlist: • Greg's Path to Become ...
Learn Python and Data Science FASTER at mlnow.ai :)
Support the content: / @greghogg
Follow me on Instagram: / greghogg5
Connect with me on LinkedIn: / greghogg
Follow me on TikTok: / greghogg5
Coursera Plus: imp.i384100.ne...
My Favorite Courses:
Data Structures & Algorithms:
UCalifornia San Diego DSA: imp.i384100.ne...
Stanford Algorithms: imp.i384100.ne...
Python Data Structures: imp.i384100.ne...
Meta Coding Interview Prep: imp.i384100.ne...
Python:
UMichigan Python for Everybody: imp.i384100.ne...
Python Mastery from MLNOW.ai: mlnow.ai/cours...
Google IT Automation w/ Python: imp.i384100.ne...
Web Dev / Full Stack:
Meta Front-End Developer: imp.i384100.ne...
IBM Full Stack Developer: imp.i384100.ne...
Meta Back-End Developer: imp.i384100.ne...
John Hopkins HTML, CSS & JS: imp.i384100.ne...
IBM DevOps: imp.i384100.ne...
Cloud Development:
AWS Fundamentals: imp.i384100.ne...
GCP Cloud Engineer: imp.i384100.ne...
Microsoft Azure Fundamentals: imp.i384100.ne...
Game Development:
Michigan State Unity Development: imp.i384100.ne...
UColorado C++ for Unreal Engine: www.coursera.o...
SQL & Data Science:
SQL by MLNOW.ai: mlnow.ai/cours...
Python for Data Science by MLNOW.ai: mlnow.ai/cours...
Google Data Analytics: imp.i384100.ne...
IBM Data Science: imp.i384100.ne...
IBM Data Engineer: imp.i384100.ne...
Machine Learning & AI:
ML Mastery at MLNOW.ai: mlnow.ai/cours...
ML w/ Andrew Ng: www.coursera.o...
Deep Learning w/ Andrew Ng: imp.i384100.ne...
Master Data Structures & Algorithms for FREE at AlgoMap.io/
Awesome explanation, thank you very much!
Better than neetcode lol
Isn't it kinda misleading to call it a 2 pointer approach when we essentially use n (here 4) pointers (lo and hi are basically pointers too right?)
Feels kinda bad to understand these topics😂
oi, why is it?
@@gourav69682sum, 3sum, 4sum, dp...shall I go on?
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)?
sorry but I dont know how to do 3 sum😮💨
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