300x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- google coding competition
- 리눅스
- nlp
- 순열
- 동적프로그래밍
- 브루트포스
- 알고리즘
- 동적 프로그래밍
- dp
- 딥러닝
- CSS
- 프로그래밍
- BFS
- 킥스타트
- 구글 킥스타트
- AI
- 코딩 테스트
- linux
- 파이썬
- kick start
- 백준
- 운영체제
- 그래프
- 코딩테스트
- OS
- PYTHON
- 네트워크
- 프로그래머스
- 코딩
- DFS
Archives
- Today
- Total
오뚝이개발자
[백준15658] 연산자 끼워넣기 (2) 본문
728x90
300x250
문제
https://www.acmicpc.net/problem/15658
생각의 흐름
이전에 포스팅한 연산자 끼워넣기와의 차이점은 연산자의 갯수가 많아졌다는 점이다.
동일한 방법으로 짜도 정답은 맞지만 시간초과가 난다는 사실...
아마 연산자 갯수가 많아졌는데 이들을 permutations에 넣고 돌리는 부분에서 시간 초과가 난 듯하다.
이를 개선하기 위해 재귀를 이용하였다.
코드
n = int(input())
a = list(map(int, input().split()))
op = list(map(int, input().split()))
mx, mn = -1e9, 1e9 # 최대 최소 처음 수
def solve(index, ans, add, sub, mul, div) :
global mx, mn
if index >= n :
mx = max(mx, ans)
mn = min(mn, ans)
return
if add > 0 :
solve(index+1, ans+a[index], add-1, sub, mul, div)
if sub > 0 :
solve(index+1, ans-a[index], add, sub-1, mul, div)
if mul > 0 :
solve(index+1, ans*a[index], add, sub, mul-1, div)
if div > 0 :
solve(index+1, ans//a[index] if ans > 0 else -((-ans)//a[index]), add, sub, mul, div-1)
solve(1, a[0], op[0], op[1], op[2], op[3])
print(mx)
print(mn)
### 출처 : https://statssy.github.io/pro/2019/09/11/baekjoon_15658/
728x90
300x250
'코딩 테스트 > 백준' 카테고리의 다른 글
[백준15649] N과 M (1) (0) | 2020.03.12 |
---|---|
[백준11723] 집합 (0) | 2020.03.10 |
[백준14501] 퇴사 (0) | 2020.03.10 |
[백준1182] 부분수열의 합 (0) | 2020.03.10 |
[백준1759] 암호 만들기 (0) | 2020.03.09 |
Comments