Combination Sum - Leetcode 39 - Recursive Backtracking (Python)

Поділитися
Вставка
  • Опубліковано 5 січ 2025

КОМЕНТАРІ • 14

  • @GregHogg
    @GregHogg  5 місяців тому

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @chisomedoka5651
    @chisomedoka5651 6 місяців тому +21

    why is backtracking so hard!!!!!!! I feel dumb, thanks for you explanation. Currently watching for the fifth time

    • @anna-plink
      @anna-plink 4 місяці тому +5

      You're not alone!!

    • @Johnson_Amah
      @Johnson_Amah 2 місяці тому +1

      You are definitely not alone

    • @jithendragunuru
      @jithendragunuru 2 місяці тому

      +1

    • @vinaylasetti4665
      @vinaylasetti4665 3 дні тому +1

      Backtracking is a mechanism formed by don't pick a choice and pick a choice approach to arrive at a target solution combination. As per my understanding, we can use backtracking technique whenever we have to deal with combinations. So If you ask why Backtracking? because without backtracking we would endup in duplicate combination of solutions.
      I hope this gives some light! Thanks

  • @maskedoni3229
    @maskedoni3229 8 місяців тому

    I really like these recursive backtracking problems, you explain them well.

    • @GregHogg
      @GregHogg  8 місяців тому +1

      Oh I'm really glad to hear that. Such a tricky topic, I'm glad I did okay 😊

  • @JoeTan-nq4fq
    @JoeTan-nq4fq 8 днів тому

    Using a for..loop seems easier
    def dfs(index: int, remainder: int, state: list) -> None:
    # Base Case
    if remainder < 0: return
    if remainder == 0: return result.append(state[:])
    # Backtrack when the remainder is less than 0
    for i in range(index, n):
    if (x := remainder - candidates[i]) >= 0:
    dfs(i, x, state + [candidates[i]])
    else:
    return
    [2,3,5], 8
    index = 0 1 2
    canddidates[i] = 2,3,5 3,5 5
    ________[ ]_________
    / | \
    i=0 __[2]__ [3] [5]
    / \ / \
    i=0 [2,2] [2,3] [3,3] [3,5]
    / \ |
    i=0 [2,2,2] [2,2,3] [2,3,3]
    |
    i=0 [2,2,2,2]

  • @alichopping2703
    @alichopping2703 3 місяці тому

    These videos are great, can you do one for Combination Sum II?

  • @saleheen12
    @saleheen12 2 місяці тому

    The graph showed in the video hurt my brain but when I saw the code. I realized, it's almost the same code as "Subsets" problem.

  • @jst8922
    @jst8922 7 місяців тому

    What tool you use for digital whiteboard (3:43) ?