Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- dfs
- 제곱근
- 스택
- 재귀
- 이진탐색
- 백준
- 파이썬
- 브루트포스
- 약수구하기
- 오픽독학
- issubset
- 삼중for문 탈출
- 복사
- 슬라이싱
- bfs
- 딕셔너리
- 완전이진트리
- 우선순위큐
- set
- heapq
- 최대재귀높이
- 구현
- date_format
- 파일명 변경
- IH
- Deque
- %H
- Inner Join
- 오픽
- not null
Archives
- Today
- Total
나예
[프로그래머스] 타겟넘버(bfs, 파이썬) 본문
1. 문제
https://school.programmers.co.kr/learn/courses/30/lessons/43165?language=python3
2. 풀이
큐를 2개 이용함 q_next는 임시 q를 담아두는 공간이다
우선 q에 numbers[0]과 -numbers[0] 을 담아두고 시작
numbers[1]부터 끝까지 q에 있는값에 numbers[i]를 더하고 빼고 q_enxt에 넣는다
그 후 q가 비면 q_next의 값을 q에 넣어주고 q_next를 비워준다
이렇게 반복 ..
3. 코드
from collections import deque
def solution(numbers, target):
answer = 0
q= deque()
q_next=deque()
q.append(numbers[0])
q.append(-numbers[0])
for i in range(1,len(numbers)):
while q:
pop= q.popleft()
q_next.append(pop+numbers[i])
q_next.append(pop-numbers[i])
q = q_next.copy()
q_next.clear()
for x in q:
if target == x:
answer +=1
return answer
728x90