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

3-wjdheesp44 #10

Merged
merged 2 commits into from
Apr 10, 2024
Merged

3-wjdheesp44 #10

merged 2 commits into from
Apr 10, 2024

Conversation

wjdheesp44
Copy link
Collaborator

πŸ”— 문제 링크

14888번 μ—°μ‚°μž λΌμ›Œλ„£κΈ°

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

40λΆ„

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

λ°±νŠΈλž˜ν‚Ή, 브루트포슀λ₯Ό μ΄μš©ν•˜λŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€.

κ³ λ €ν•  점

  • μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•œ 횟수λ₯Ό κ³ λ €ν•˜μ—¬ λ°±νŠΈλž˜ν‚ΉμœΌλ‘œ 횟수λ₯Ό λ˜λŒλ €μ•Ό ν•©λ‹ˆλ‹€.
  • μ΅œλŒ“κ°’, μ΅œμ†Ÿκ°’μ˜ λ²”μœ„λ₯Ό κ³ λ €ν•©λ‹ˆλ‹€. (-10μ–΅ ~ 10μ–΅)
  • λ‚˜λˆ—μ…ˆμ„ ν•  λ•Œ, λ‚˜λˆ—μ…ˆμ€ μ •μˆ˜ λ‚˜λˆ—μ…ˆμœΌλ‘œ λͺ«λ§Œ μ·¨ν•©λ‹ˆλ‹€. 음수λ₯Ό μ–‘μˆ˜λ‘œ λ‚˜λˆŒ λ•Œ, μ–‘μˆ˜λ‘œ λ°”κΎΌ λ’€ λͺ«μ„ μ·¨ν•˜κ³ , κ·Έ λͺ«μ„ 음수둜 λ°”κΎΌ 것과 κ°™μŠ΅λ‹ˆλ‹€.
    • νŒŒμ΄μ¬μ—μ„œλŠ” int() λ‚΄μž₯ν•¨μˆ˜λ₯Ό ν™œμš©ν•©λ‹ˆλ‹€.


μˆœμ„œ

1. μ‚¬μš©ν•˜λ €λŠ” μ—°μ‚°μžκ°€ 0보닀 클 λ•Œ, μ—°μ‚°μžλ₯Ό μ‚¬μš©
2. μ—°μ‚°μžλ₯Ό μ‚¬μš© ν›„, μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ•˜μ„ κ²½μš°λ„ κ³ λ €ν•˜κΈ° μœ„ν•΄ μž¬κ·€ν•¨μˆ˜ 이후, μ—°μ‚°μžλ₯Ό λ‹€μ‹œ λ”ν•΄μ€Œ
3. μ‚¬μš©ν•œ μ—°μ‚°μžμ˜ κ°œμˆ˜μ™€ 수의 κ°œμˆ˜κ°€ 같을 λ•Œ, μ΅œμ†Ÿκ°’κ³Ό μ΅œλŒ“κ°’μ„ ꡬ함

n = int(input())
nums = list(map(int, input().split()))
add, sub, mul, div = map(int, input().split())

max_value = -int(1e9)
min_value = int(1e9)

def backtracking(i, total):
    global add, sub, mul, div, max_value, min_value
    if i == n:
        max_value = max(max_value, total)
        min_value = min(min_value, total)
        return 

    if add > 0:
        add -= 1
        backtracking(i+1, total + nums[i])
        add += 1
    if sub > 0:
        sub -= 1
        backtracking(i+1, total - nums[i])
        sub += 1
    if mul > 0:
        mul -= 1
        backtracking(i+1, total * nums[i])
        mul += 1
    if div > 0:
        div -= 1
        backtracking(i+1, int(total / nums[i]))
        div += 1

backtracking(1, nums[0])
print(max_value)
print(min_value)

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

μ²˜μŒμ— μ‹μ˜ 결과의 μ΅œλŒ“κ°’κ³Ό μ΅œμ†Ÿκ°’μ˜ λ²”μœ„μ™€ λ‚˜λˆ—μ…ˆ μ—°μ‚° 방법을 μ œλŒ€λ‘œ 보지 μ•Šμ•„μ„œ 계속 값이 λ‹€λ₯΄κ²Œ λ‚˜μ™”μŠ΅λ‹ˆλ‹€. νŒŒμ΄μ¬μ—μ„œλŠ” λ‚˜λˆ—μ…ˆ μ—°μ‚° 방법이 λ‹€λ₯΄λ‹€λŠ” 것을 μ•Œκ²Œ 된 것 κ°™μ•„μš”.

Copy link
Member

@SunYerim SunYerim left a comment

Choose a reason for hiding this comment

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

ν¬μˆ˜λ‹˜μ΄λž‘ μƒκ°ν•œ 둜직 μžμ²΄λŠ” λΉ„μŠ·ν–ˆλŠ”λ°, μ œκ°€ ν‘Ό μ½”λ“œλ„ ν•œ 번 올렀봐용
기본적인 μ½”λ“œ κ΅¬μ‘°λŠ” λ˜‘κ°™μ€λ°, 각 μ—°μ‚°μžλ§ˆλ‹€ μ‹€ν–‰ν•΄μ£Όμ–΄μ•Όλ˜λŠ”? μ½”λ“œκ°€ ν•œ μ€„λ‘œ μž‘μ„œμ˜€λ””μ–΄μžˆμ–΄μ„œ 쑰금 더 직관적인 것 κ°™λ‹€λŠ” 생각도 λ“œλ„€μš” 어쨋든 μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€ !!

'''logic
    1. dfsλ₯Ό μ΄μš©ν•œ μ΅œλŒ€μ΅œμ†Œλ₯Ό κ΅¬ν•˜λŠ” 방법'''
import sys

input = sys.stdin.readline
num = int(input())

num_list = list(map(int, input().split()))

# μ—°μ‚°μž
op = list(map(int, input().split()))

# μ΅œμ†Ÿκ°’ μ΅œλŒ“κ°’ μ΄ˆκΈ°ν™” !
max_num = -1e9
min_num = 1e9

# dfs λ©”μ†Œλ“œ κ΅¬ν˜„
def dfs(depth, total, add, sub, mul, div):
    global max_num, min_num
    if depth == num:
        max_num = max(total, max_num)
        min_num = min(total, min_num)
        return
    
    if add:
        dfs(depth +1, total + num_list[depth], add-1, sub, mul, div)
    if sub:
        dfs(depth+1, total - num_list[depth], add, sub-1, mul, div)
    if mul:
        dfs(depth+1, total * num_list[depth], add, sub, mul-1, div)
    if div:
        dfs(depth+1, int(total/num_list[depth]), add, sub, mul, div-1)
    
dfs(1, num_list[0], op[0], op[1], op[2], op[3])
print(max_num)
print(min_num)

Comment on lines +8 to +30
def backtracking(i, total):
global add, sub, mul, div, max_value, min_value
if i == n:
max_value = max(max_value, total)
min_value = min(min_value, total)
return

if add > 0:
add -= 1
backtracking(i+1, total + nums[i])
add += 1
if sub > 0:
sub -= 1
backtracking(i+1, total - nums[i])
sub += 1
if mul > 0:
mul -= 1
backtracking(i+1, total * nums[i])
mul += 1
if div > 0:
div -= 1
backtracking(i+1, int(total / nums[i]))
div += 1
Copy link
Member

Choose a reason for hiding this comment

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

저도 μ˜ˆμ „μ— 파이썬으둜 ν’€μ—ˆμ—ˆλŠ”λ°, μ €λŠ” ν•΄λ‹Ή μ½”λ“œλ₯Ό μ΄λ ‡κ²Œ μž‘μ„±ν–ˆμ—ˆμ–΄μš”!

def dfs(depth, total, add, sub, mul, div):
    global max_num, min_num
    if depth == num:
        max_num = max(total, max_num)
        min_num = min(total, min_num)
        return
    
    if add:
        dfs(depth +1, total + num_list[depth], add-1, sub, mul, div)
    if sub:
        dfs(depth+1, total - num_list[depth], add, sub-1, mul, div)
    if mul:
        dfs(depth+1, total * num_list[depth], add, sub, mul-1, div)
    if div:
        dfs(depth+1, int(total/num_list[depth]), add, sub, mul, div-1)

mul += 1
if div > 0:
div -= 1
backtracking(i+1, int(total / nums[i]))
Copy link
Collaborator

Choose a reason for hiding this comment

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

int(total / nums[i])
total // nums[i]
μ•„λž˜μ²˜λŸΌ μ •μˆ˜ λ‚˜λˆ—μ…ˆ μ—°μ‚°μž("//")λ₯Ό μ¨μ„œ κ²°κ³Όλ₯Ό λ‚΄λ¦Όν•  수 도 μžˆλ„€μš” 처음 μ•Œμ•˜μŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

Copy link
Collaborator

@erase-jeong erase-jeong left a comment

Choose a reason for hiding this comment

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

덕뢄에 λ°±νŠΈλ ˆν‚Ή μ•Œκ³ λ¦¬μ¦˜ μ°Ύμ•„λ³΄λ©΄μ„œ κ³΅λΆ€ν–ˆμŠ΅λ‹ˆλ‹€.

@wjdheesp44 wjdheesp44 merged commit 0385956 into main Apr 10, 2024
1 check passed
@wjdheesp44 wjdheesp44 deleted the 3-wjdheesp44 branch April 10, 2024 12:20
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