Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cfe33af

Browse files
[level 2] Title: 타겟 넘버, Time: 10.10 ms, Memory: 10.2 MB -BaekjoonHub
1 parent f451f4a commit cfe33af

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [level 2] 타겟 넘버 - 43165
2+
3+
[문제 링크](https://programmers.co.kr/learn/courses/30/lessons/43165)
4+
5+
### 성능 요약
6+
7+
메모리: 10.2 MB, 시간: 10.10 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 깊이/너비 우선 탐색(DFS/BFS)
12+
13+
### 채점결과
14+
15+
<br/>정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 문제 설명
18+
19+
<p>n개의 음이 아닌 정수들이 있습니다. 이 정수들을 순서를 바꾸지 않고 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수 있습니다.</p>
20+
<div class="highlight"><pre class="codehilite"><code>-1+1+1+1+1 = 3
21+
+1-1+1+1+1 = 3
22+
+1+1-1+1+1 = 3
23+
+1+1+1-1+1 = 3
24+
+1+1+1+1-1 = 3
25+
</code></pre></div>
26+
<p>사용할 수 있는 숫자가 담긴 배열 numbers, 타겟 넘버 target이 매개변수로 주어질 때 숫자를 적절히 더하고 빼서 타겟 넘버를 만드는 방법의 수를 return 하도록 solution 함수를 작성해주세요.</p>
27+
28+
<h5>제한사항</h5>
29+
30+
<ul>
31+
<li>주어지는 숫자의 개수는 2개 이상 20개 이하입니다.</li>
32+
<li>각 숫자는 1 이상 50 이하인 자연수입니다.</li>
33+
<li>타겟 넘버는 1 이상 1000 이하인 자연수입니다.</li>
34+
</ul>
35+
36+
<h5>입출력 예</h5>
37+
<table class="table">
38+
<thead><tr>
39+
<th>numbers</th>
40+
<th>target</th>
41+
<th>return</th>
42+
</tr>
43+
</thead>
44+
<tbody><tr>
45+
<td>[1, 1, 1, 1, 1]</td>
46+
<td>3</td>
47+
<td>5</td>
48+
</tr>
49+
<tr>
50+
<td>[4, 1, 2, 1]</td>
51+
<td>4</td>
52+
<td>2</td>
53+
</tr>
54+
</tbody>
55+
</table>
56+
<h5>입출력 예 설명</h5>
57+
58+
<p><strong>입출력 예 #1</strong></p>
59+
60+
<p>문제 예시와 같습니다.</p>
61+
62+
<p><strong>입출력 예 #2</strong></p>
63+
<div class="highlight"><pre class="codehilite"><code>+4+1-2+1 = 4
64+
+4-1+2-1 = 4
65+
</code></pre></div>
66+
<ul>
67+
<li>총 2가지 방법이 있으므로, 2를 return 합니다.</li>
68+
</ul>
69+
70+
71+
> 출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from itertools import product
2+
def solution(numbers, target):
3+
answer = 0
4+
size = len(numbers)
5+
for op in product([1, -1], repeat=size):
6+
result = [x * y for x, y in zip(numbers, op)]
7+
if sum(result) == target:
8+
answer += 1
9+
return answer

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /