[백준 백트래킹] 연산자 끼워넣기(python)

이진규·2022년 2월 13일
1

백준(PYTHON)

목록 보기
42/115

문제

https://www.acmicpc.net/problem/14888

나의 코드

"""
1. 아이디어


2. 시간복잡도

"""

from sys import stdin
input = stdin.readline

n = int(input())
nums = list(map(int, input().split()))
op = list(map(int, input().split())) # 연산자 : +, -, *, /

maximum = -1e9
minimum = 1e9

def btr(depth, total, plus, minus, multiply, divide):
    global maximum, minimum
    if depth == n: # 최댓값과 최솟값을 갱신해준다.
        maximum = max(total, maximum)
        minimum = min(total, minimum)
        return

    if plus:
        btr(depth+1, total + nums[depth], plus-1, minus, multiply, divide)
    if minus:
        btr(depth+1, total - nums[depth], plus, minus-1, multiply, divide)
    if multiply:
        btr(depth+1, total * nums[depth], plus, minus, multiply-1, divide)
    if divide:
        btr(depth+1, int(total / nums[depth]), plus, minus, multiply, divide-1)

btr(1, nums[0], op[0], op[1], op[2], op[3])
print(maximum)
print(minimum)
    

느낀점

삼성 기출문제

profile
항상 궁금해하고 공부하고 기록하자.

0개의 댓글