Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11-kjs254 #47

Merged
merged 3 commits into from
Jun 27, 2024
Merged

11-kjs254 #47

merged 3 commits into from
Jun 27, 2024

Conversation

kjs254
Copy link
Member

@kjs254 kjs254 commented Apr 5, 2024

πŸ”— 문제 링크

νƒ€κ²Ÿ λ„˜λ²„

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

2h

✨ μˆ˜λ„ μ½”λ“œ

1. λ¬Έμ œν•΄μ„

μ‚¬μš©ν•  수 μžˆλŠ” μˆ«μžκ°€ λ‹΄κΈ΄ λ°°μ—΄ numbers, νƒ€κ²Ÿ λ„˜λ²„ target이 λ§€κ°œλ³€μˆ˜λ‘œ μ£Όμ–΄μ§ˆ λ•Œ 숫자λ₯Ό 적절히 λ”ν•˜κ³  λΉΌμ„œ νƒ€κ²Ÿ λ„˜λ²„λ₯Ό λ§Œλ“œλŠ” λ°©λ²•μ˜ 수λ₯Ό return ν•˜λ„λ‘ solution ν•¨μˆ˜λ₯Ό μž‘μ„±ν•΄μ£Όμ„Έμš”.

numbers = [1, 1, 1, 1, 1]
target = 3

인 경우,

1. -1+1+1+1+1 = 3
2. +1-1+1+1+1 = 3
3. +1+1-1+1+1 = 3
4. +1+1+1-1+1 = 3
5. +1+1+1+1-1 = 3

μœ„μ™€ 같이 5가지 κ²½μš°κ°€ κ°€λŠ₯ν•˜λ―€λ‘œ 5λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

λ°°μ—΄μ˜ 길이가 n 이라면 2n가지 경우의 수λ₯Ό κ³ λ €ν•˜μ—¬ 쑰건을 λ§Œμ‘±ν•˜λŠ” 경우λ₯Ό μ°Ύμ•„μ•Ό ν•˜λ―€λ‘œ 이λ₯Ό λ°”νƒ•μœΌλ‘œ μ½”λ“œλ₯Ό κ΅¬ν˜„ν•˜κ² μŠ΅λ‹ˆλ‹€.


2. μˆ˜λ„μ½”λ“œ

  1. n = len(numbers)

  2. 2n개의 λͺ¨λ“  인덱슀 집합 idxs_listλ₯Ό λ°˜ν™˜ν•˜λŠ” DFS()λ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€.
    (* n=2인 경우 [[],[0],[1],[0,1]]와 같은 2차원 배열을 λ°˜ν™˜ν•©λ‹ˆλ‹€.)

  3. idxs_listλ₯Ό μˆœνšŒν•˜λ©° numbers의 λΆ€ν˜Έλ₯Ό λ°”κΎΌ λ’€ sum()ν•˜μ—¬ 쑰건과 λ§žλŠ”μ§€ νŒλ…ν•©λ‹ˆλ‹€.

핡심인 DFSλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

def DFS(length, start, path):
    if len(path) > length:
        return
    idxs_list.append(path)
    
    for i in range(start, length):
        DFS(length, i + 1, path + [i])

idxs_listλŠ” global μ „μ—­λ³€μˆ˜λ‘œ λ‘” λ’€ μž¬κ·€λ₯Ό ν†΅ν•˜μ—¬ idxs_list에 차곑차곑 μΈλ±μŠ€λ“€μ΄ μ‚½μž…ν•˜λ„λ‘ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

이 λΆ€λΆ„ κ΅¬ν˜„μ΄ 많이 νž˜λ“€μ–΄μ„œ 힌트 및 λ‹€λ₯Έ μž¬κ·€ν•¨μˆ˜λ“€μ„ 많이 μ°Έκ³ ν•˜μ—¬ μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€.. μž¬κ·€ μ–΄λ €μ›Œ

μ΅œμ’… μ½”λ“œ

def DFS(length, start, path):
    if len(path) > length:
        return
    idxs_list.append(path)
    
    for i in range(start, length):
        DFS(length, i + 1, path + [i])

def solution(numbers, target):
    global idxs_list
    idxs_list = []
    DFS(len(numbers),0,[])
    my_numbers = [x for x in numbers]
    count=0
    
    for idx in idxs_list:
        for i in idx:
            my_numbers[i] *=-1
        if sum(my_numbers)==target:
            count+=1
        my_numbers = [x for x in numbers]
    
    return count

메인 ν•¨μˆ˜λ₯Ό λ³΄μ‹œλ©΄ numbersλ₯Ό μ°Έμ‘°ν•  λ•Œ 리슀트 μ»΄ν”„λ¦¬ν—¨μ…˜μ„ μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

my_numbers = [x for x in numbers]

μ²˜μŒμ—λŠ” 아무 생각 없이

my_numbers = numbers

μœ„ λ°©λ²•μœΌλ‘œ my_numbersλ₯Ό μƒμ„±ν•˜μ˜€μ§€λ§Œ μ£Όμ†Œ μ°Έμ‘° 방식 λ•Œλ¬Έμ— my_numbersλ₯Ό μˆ˜μ •ν•˜λ©΄ numbers도 μˆ˜μ •λ˜κΈ° λ•Œλ¬Έμ— 리슀트 μ»΄ν”„λ¦¬ν—¨μ…˜μ„ 톡해 μƒˆλ‘œμš΄ 배열을 μƒμ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

νŒŒμ΄μ¬μ—μ„œλŠ” 얕은 볡사, κΉŠμ€ 볡사 λ“±μ˜ κ°œλ…μœΌλ‘œ 많이 μ•Œλ €μ Έ μžˆμŠ΅λ‹ˆλ‹€.

파이썬 (Python) - κΉŠμ€ 볡사 (Deep Copy)λ₯Ό μ°Έκ³ ν•˜μ‹œλ©΄ μžμ„Έν•˜κ²Œ 보싀 수 μžˆμŠ΅λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

DFS에 λŒ€ν•΄ 쑰금 더 μ΄ν•΄ν•˜κ²Œ 되고 파이썬의 κΉŠμ€ 볡사에 λŒ€ν•΄μ„œλ„ μžμ„Ένžˆ μ•Œ 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ²˜μŒμ—λŠ” 얕은 볡사, κΉŠμ€ 볡사가 무엇인지 잘 λͺ°λžμ§€λ§Œ 글을 μ½μ–΄λ³΄λ‹ˆ λ‹€μ‹œ κ·Έ κ°œλ…μ΄ κΈ°μ–΅λ‚˜λ”λΌκ³ μš”.
리슀트 μ»΄ν”„λ¦¬ν—¨μ…˜λ„ 처음 λ³΄λŠ” κ°œλ…μΈκ°€ ν•˜μ—¬ λ‹€μ‹œ ν•œλ²ˆ μ°Ύμ•„λ³΄μ•˜λ„€μš”.
배울점이 λ§Žμ€ PRμ΄μ—ˆλ˜κ±° κ°™μŠ΅λ‹ˆλ‹€.

μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€. 😁

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ˜μ™Έλ‘œ 일반적인 DFS λ¬Έμ œμ˜€λ„€μš”.. 덀으둜 파이썬의 νŠΉμ§•μ„ ν•˜λ‚˜ μ•Œμ•„κ°ˆ 수 μžˆμ–΄μ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€.

@tgyuuAn tgyuuAn merged commit de9a275 into AlgoLeadMe:main Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants