https://programmers.co.kr/learn/courses/30/lessons/43165
✅ Solution
- deque를 이용하여 idx가 numbers의 길이보다 작을 때만 +, - 의 경우를 모두 구해준다.
- 만약 idx가 numbers의 길이가 같은 경우에는 target값과 비교하여 같다면 answer에 1을 더해준다.
✅ Code
from collections import deque
def solution(numbers, target):
answer = 0
queue = deque()
queue.append((0, 0))
while queue:
now, idx = queue.popleft()
if idx < len(numbers):
queue.append((now + numbers[idx], idx + 1))
queue.append((now - numbers[idx], idx + 1))
elif idx == len(numbers):
if now == target:
answer += 1
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - 괄호 회전하기 (Python) (0) | 2022.06.09 |
---|---|
[프로그래머스] Level 2 - 짝지어 제거하기 (Python) (0) | 2022.06.04 |
[프로그래머스] Level 2 - 더 맵게 (Python) (0) | 2022.06.03 |
[프로그래머스] Level 2 - 주차 요금 계산 (Python) (0) | 2022.05.24 |
[프로그래머스] Level 2 - 양궁대회 (Python) (2) | 2022.05.24 |