DFS/BFS - 타겟넘버

송다은·2024년 10월 11일
def solution(numbers, target):
    answer = 0
    leaves = [0] #트리 초기화
    
    for num in numbers:
        temp=[] 
    
        for leaf in leaves:
            temp.append(leaf+num)
            temp.append(leaf-num)

        leaves = temp # 매번 경우의 수마다 값을 초기화해줌
    for leaf in leaves:
        if leaf == target:
            answer+=1
        
    return answer

DFS/BFS 처음 풀어봐요....
위 코드는 BFS이며 트리구조에서 각 가로에 있는 값들을 갱신하고 추가해주면서 값들을 확인하는 flow를 이해할 수 있었다!!

profile
Anomaly Detection, AI Security, Multimodal

0개의 댓글