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
- ๊ทธ๋ฆฌ๋
- Til
- ์ฝ๋ฉํ ์คํธ
- mongodb
- CSS
- error
- HTML
- ํฌ๋กค๋ง
- ๋ ธ๋ง๋์ฝ๋
- react
- fe
- ๋๋ฆผ์ฝ๋ฉ
- ํ๋ก๊ทธ๋๋จธ์ค
- ํ๋ก์ ํธ
- heapq
- ์๊ณ ๋ฆฌ์ฆ
- ํ์ด์ฌ
- Python
- ์ฝ๋ฉ์ ํ
- javascript
- ์ฝ๋ฉ
- ํ ์ดํ๋ก์ ํธ
- ๋ฐฑ์ค
- JS
- node.js
- ๋ชจ๊ฐ์ฝ
- KDT
- ๊ตญ๋น์ง์
- ๊ฐ๋ฐ
- ํ๋ก ํธ์๋
Archives
- Today
- Total
๐ฑ → ๐ณ
[ํ๋ก๊ทธ๋๋จธ์ค] ํ์ผ ๋๋ฒ - python ๋ณธ๋ฌธ
728x90
๋ฌธ์ ์ ๋ณด
๋ฌธ์ ์ ํ | ๋์ด๋ | ๊ฑธ๋ฆฐ ์๊ฐ | ํด๊ฒฐ ์ ๋ฌด | ์ค์ค๋ก ๊ตฌํ ์ฑ๊ณต |
DFS/BFS | level2 | 33m | O | O |
์ค๊ณ ๋ฐฉ๋ฒ
- numbers ๋๋ฉด์ arr ๋ชจ๋ ์์์ ๋ํด -num๊ณผ +num์ ํด์ค
- arr ๋ชจ๋ ์์๋ฅผ ๋ ๋๋ arr.popleft()๋ก ํ๋์ฉ ๋นผ์ ์ฌ์ฉ
- -num๊ณผ +num์ ํด์ค ์์๋ arr.append()
- numbers ๋ค ๋์์ผ๋ฉด ๋ง์ง๋ง arr์ ์๋ ์์ ์ค target๊ณผ ์ผ์นํ๋ ์์ ์นด์ดํ
์ฝ๋
๋ด ํ์ด
from collections import deque
def solution(numbers, target):
arr = deque([0])
for num in numbers:
for _ in range(len(arr)):
v = arr.popleft()
arr.append(v - num)
arr.append(v + num)
return arr.count(target)
๋ค๋ฅธ ์ฌ๋ ํ์ด 1
answer = 0
def DFS(idx, numbers, target, value):
global answer
N = len(numbers)
if(idx== N and target == value):
answer += 1
return
if(idx == N):
return
DFS(idx+1,numbers,target,value+numbers[idx])
DFS(idx+1,numbers,target,value-numbers[idx])
def solution(numbers, target):
global answer
DFS(0,numbers,target,0)
return answer
๋ค๋ฅธ ์ฌ๋ ํ์ด 2
from itertools import product
def solution(numbers, target):
l = [(x, -x) for x in numbers]
s = list(map(sum, product(*l)))
return s.count(target)
728x90
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ธฐ๋ฅ๊ฐ๋ฐ - python (0) | 2023.08.02 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ฃผํ์ง ๋ชปํ ์ ์ - python (0) | 2023.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] JadenCase ๋ฌธ์์ด ๋ง๋ค๊ธฐ - python (0) | 2023.07.25 |
[Python] ํ ์๋ฃ๊ตฌ์กฐ / ํํ(heapq) / ํ์ด์ฌ์์ heapq ๋ชจ๋ ์ฌ์ฉ (1) | 2023.05.30 |
[๋ฐฑ์ค] 13458๋ฒ: ์ํ ๊ฐ๋ python (0) | 2023.01.11 |