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 | 31 |
Tags
- Pytorch pruning
- Pruning Tutorial
- pytorch
- Single Shot MultiBox Detector
- 다음큰숫자
- 프로그래머스타겟넘버파이썬
- 코딩테스트연습
- Paper list
- One stage detector
- ECCV
- Two stage Detector
- 코딩테스트2단계
- 프로그래머스타겟넘버
- Object Detection
- 프로그래머스네트워크
- Code Study
- 코딩테스트네트워크
- 프로그래머스bfs
- 최솟값 만들기
- Faster R-CNN
- 프로그래머스너비우선탐색
- 프로그래머스파이썬
- SSD 리뷰
- 타겟넘버bfs
- 프로그래머스
- ssd
- 프로그래머스타겟넘버정답
- deep learning
- 커피후기
- 프로그래머스게임맵최단거리
Archives
- Today
- Total
soyeonland
프로그래머스 dfs/bfs 타겟넘버 python 본문
프로그래머스 dfs/bfs 타겟넘버 python (난이도 level2)
- 문제
- 문제 해설
주어진 numbers로 모든 경우의 수를 따져야 하는 문제이므로 dfs, bfs로 접근
- 실수했던 point
더보기
q 초깃값에 -numbers[0] 를 추가할 생각을 못했음 (이번에는 테케를 보고 바꿨지만, 예제에 없으면 틀렸을뻔)
q.popleft() 대신 q.pop()을 대신 사용함
코드를 정리하지 않고 작성하다가, 생각이 막혀서 변수 다시 정의
- solution (bfs 로 접근)
from collections import deque
cases = [-1, +1]
q = deque()
def solution(numbers, target):
answer = 0
cnt = 0
q.append([numbers[0], cnt])
q.append([-numbers[0],cnt])
while(len(q)>=1):
cur_number, cur_cnt = q.popleft()
new_cnt = cur_cnt + 1
if new_cnt < len(numbers):
for case in cases:
new_num = case*numbers[new_cnt]
new_sum = cur_number + new_num
if new_sum == target and new_cnt == len(numbers)-1:
#print('answer', new_sum)
answer += 1
else:
q.append([new_sum, new_cnt])
#print('q',q)
#print('\n')
return answer
'Study > 코딩테스트 연습' 카테고리의 다른 글
프로그래머스 dfs/bfs 게임 맵 최단거리 파이썬 (1) | 2024.10.13 |
---|---|
프로그래머스 dfs/bfs 네트워크 파이썬 (0) | 2024.10.13 |
단어변환 (0) | 2024.10.02 |
귤고르기 (0) | 2024.07.30 |
N개의 최소 공배수 (0) | 2024.07.17 |