Skip to content

Commit 458fcfc

Browse files
committed
[level 2] Title: 타겟 넘버, Time: 642.91 ms, Memory: 98.6 MB -BaekjoonHub
1 parent 669cf29 commit 458fcfc

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

프로그래머스/2/43165. 타겟 넘버/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 10.3 MB, 시간: 378.77 ms
7+
메모리: 98.6 MB, 시간: 642.91 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 03월 07일 15:06:29
19+
2025년 04월 26일 16:05:51
2020

2121
### 문제 설명
2222

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
from collections import deque
2+
13
def solution(numbers, target):
2-
global ans
3-
ans = 0
4-
def dfs(i, total):
5-
global ans
6-
if (i == len(numbers)):
7-
if target == total:
8-
ans += 1
9-
return
10-
dfs(i+1, total + numbers[i])
11-
dfs(i+1, total - numbers[i])
12-
return
13-
dfs(0, 0)
14-
return ans
4+
global cnt
5+
cnt = 0
6+
7+
def bfs(numbers, target):
8+
global cnt
9+
q = deque([(0, numbers[0]), (0, -numbers[0])])
10+
11+
while q:
12+
i, total = q.popleft()
13+
14+
if total == target and i == len(numbers) - 1:
15+
cnt += 1
16+
if i+1 < len(numbers):
17+
q.append((i+1, total + numbers[i+1]))
18+
q.append((i+1, total - numbers[i+1]))
19+
20+
bfs(numbers, target)
21+
return cnt
22+

0 commit comments

Comments
 (0)