Combination Sum - Leetcode 39 - Recursive Backtracking (Python)

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

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

GregHogg
Автор

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

chisomedoka
Автор

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

ohmyoni
Автор

Seriously this channel is so underrated

shanky
Автор

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]

JoeTan-nqfq
Автор

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.

saleheen
Автор

On the third level, the right branch of [2], why isn't it already a [2, 3]? Why is it duplicating [2] from its parent to its right branch? X
I mean, why is it drawn like this? @3:45
*[2]*
/ \
[2, 2] *[2]*

Instead of:
[2]
/ \
[2, 2] [2, 3]

nithinbosej
Автор

What's this decision tree called? Does it have a specific name?

ZKachmer
Автор

For whatever reason, my output comes out backwards ([[7], [223]]) to the example being shown ([[2, 2, 3], [7]]).

bigk
Автор

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

alichopping
Автор

why are you using curr_sum as argument, why not just do sum(sol) inside the function

prakashprabhu
Автор

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

sutendramirajkar
Автор

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

nekdo_pac
welcome to shbcf.ru