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...

ความคิดเห็น • 11

  • @GregHogg
    @GregHogg  5 หลายเดือนก่อน

    Master Data Structures & Algorithms for FREE at AlgoMap.io/

  • @DrFrancis1686
    @DrFrancis1686 2 วันที่ผ่านมา

    Awesome explanation, thank you very much!

  • @donkeykong5616
    @donkeykong5616 หลายเดือนก่อน +2

    Better than neetcode lol

  • @Thebeast_QwQ
    @Thebeast_QwQ 21 วันที่ผ่านมา

    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?)

  • @mitranshuraj2811
    @mitranshuraj2811 6 หลายเดือนก่อน +6

    Feels kinda bad to understand these topics😂

    • @gourav6968
      @gourav6968 6 หลายเดือนก่อน +1

      oi, why is it?

    • @mitranshuraj2811
      @mitranshuraj2811 5 หลายเดือนก่อน

      ​@@gourav69682sum, 3sum, 4sum, dp...shall I go on?

  • @JoeTan-nq4fq
    @JoeTan-nq4fq 3 หลายเดือนก่อน

    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)?

  • @Fen-i3n
    @Fen-i3n 4 หลายเดือนก่อน

    sorry but I dont know how to do 3 sum😮‍💨

    • @xingyuxiang1637
      @xingyuxiang1637 2 หลายเดือนก่อน

      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