Combination Sum - Leetcode 39 - Recursive Backtracking (Python)

Поделиться
HTML-код
  • Опубликовано: 19 янв 2025

Комментарии • 16

  • @GregHogg
    @GregHogg  5 месяцев назад

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

  • @chisomedoka5651
    @chisomedoka5651 7 месяцев назад +22

    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 3 месяца назад +1

      You are definitely not alone

    • @jithendragunuru
      @jithendragunuru 2 месяца назад

      +1

    • @vinaylasetti4665
      @vinaylasetti4665 17 дней назад +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

  • @shanky6343
    @shanky6343 3 дня назад

    Seriously this channel is so underrated

  • @ohmyoni
    @ohmyoni 8 месяцев назад +2

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

    • @GregHogg
      @GregHogg  8 месяцев назад +2

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

  • @sutendramirajkar4118
    @sutendramirajkar4118 День назад

    This can be easily solved using knapsack approach - dynamic programming.

  • @alichopping2703
    @alichopping2703 3 месяца назад

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

  • @JoeTan-nq4fq
    @JoeTan-nq4fq 22 дня назад

    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]

  • @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 8 месяцев назад

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