코딩,안되면 될때까지

타겟넘버 본문

프로그래머스/프로그래머스-파이썬

타겟넘버

soo97 2022. 2. 26. 13:44
728x90
반응형

<문제>

https://programmers.co.kr/learn/courses/30/lessons/43165

 

코딩테스트 연습 - 타겟 넘버

n개의 음이 아닌 정수들이 있습니다. 이 정수들을 순서를 바꾸지 않고 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수

programmers.co.kr

<코드>

-파이썬-

from collections import deque
def solution(numbers, target):
    answer = 0
    q = deque()
    q.append([numbers[0],0])
    q.append([-1*numbers[0],0])
    n = len(numbers)
    while q:
        temp,index = q.popleft()
        index+=1
        if index<n:
            q.append([temp+numbers[index],index])
            q.append([temp-numbers[index],index])
        else:
            if temp == target:
                answer+=1
    
    
    
    return answer
728x90
반응형

'프로그래머스 > 프로그래머스-파이썬' 카테고리의 다른 글

[프로그래머스]-괄호변환  (4) 2022.03.12
무지의 먹방 라이브  (0) 2022.02.26
조이스틱  (0) 2022.02.26
2.오픈채팅방  (0) 2022.01.13
1.문자열 압축  (0) 2022.01.12
Comments