[프로그래머스]-타겟 넘버

이정연·2022년 10월 14일
0

CodingTest

목록 보기
4/165

👋🏻 Intro

목표 시간: 60분
소요 시간: 105분

👨🏻‍💻 CODE

from collections import deque
def solution(numbers, target):
    answer = 0
    need_visit = deque([(0,0)]) # (숫자,거리)

    while need_visit:
        cur, dist = need_visit.popleft()
        if dist == len(numbers) and cur == target:
            answer += 1
        if 0<=dist<len(numbers):
            num = numbers[dist]
        for next in (num,-1*num):
            if 0<=dist+1<=len(numbers):
                need_visit.append((cur+next,dist+1))

    return answer
profile
0x68656C6C6F21

0개의 댓글