Skip to content

Commit b5567fb

Browse files
authored
Merge pull request #4 from Mingguriguri/minjeong
Minjeong / 3월 3주차 / 4문제
2 parents fa4cf8a + 7ff67f5 commit b5567fb

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 입력받기
2+
n = int(input()) # 수의 개수
3+
nums = list(map(int, input().split())) # 수열
4+
operator = list(map(int, input().split())) # 연산자
5+
6+
# 최솟값, 최댓값 초기화
7+
minValue= 1000000000
8+
maxValue= -1000000000
9+
10+
def calculator(idx, res, add, sub, prd, div):
11+
global minValue, maxValue
12+
13+
if idx == n: # 종료조건: 연산자를 모두 사용했을 경우
14+
# 다 탐색했기 때문에 최대최소 비교해서 최솟값과 최댓값 갱신
15+
maxValue = max(maxValue, res)
16+
minValue = min(minValue, res)
17+
return
18+
19+
if add > 0:
20+
calculator(idx + 1, res + nums[idx], add-1, sub, prd, div)
21+
22+
if sub > 0:
23+
calculator(idx + 1, res - nums[idx], add, sub-1, prd, div)
24+
25+
if prd > 0:
26+
calculator(idx + 1, res * nums[idx], add, sub, prd-1, div)
27+
28+
if div > 0:
29+
calculator(idx + 1, int(res / nums[idx]), add, sub, prd, div-1)
30+
31+
calculator(1, nums[0], operator[0], operator[1], operator[2], operator[3])
32+
print(maxValue)
33+
print(minValue)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
N = int(input())
5+
S = [list(map(int, input().split())) for _ in range(N)]
6+
check = [False] * N
7+
minValue = 1000000000
8+
9+
def calculate():
10+
global minValue
11+
Ateam = 0
12+
Bteam = 0
13+
for i in range(N):
14+
for j in range(N):
15+
if check[i] and check[j]:
16+
Ateam += S[i][j]
17+
if not check[i] and not check[j]:
18+
Bteam += S[i][j]
19+
20+
minValue = min(minValue, abs(Ateam - Bteam))
21+
22+
def backtracking(start, size):
23+
global minValue
24+
if size == N//2:
25+
calculate()
26+
return
27+
28+
for i in range(start, N):
29+
if check[i] == False:
30+
check[i] = True
31+
backtracking(i+1, size+1)
32+
check[i] = False
33+
34+
35+
backtracking(0, 0)
36+
print(minValue)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from collections import deque
2+
3+
def solution(cacheSize, cities):
4+
totalTime = 0
5+
cache = deque([])
6+
'''
7+
전체적인 과정은 FIFO로 수행된다.
8+
만약 이미 존재하는 도시라면 맨 뒤로 다시 보낸다.
9+
'''
10+
11+
for i in range(len(cities)):
12+
city = cities[i].lower() # 대소문자 구분 없으므로 cities를 lowercase로 바꾸기
13+
if cacheSize == 0: # cacheSize가 0인 경우에 대한 예외처리(TC 7, 17)
14+
totalTime += 5
15+
continue # 그 후 바로 빠져나옴
16+
if city in cache: # 캐시에 도시가 이미 존재하는 경우
17+
cache.remove(city)
18+
cache.append(city)
19+
totalTime += 1 # cache hit
20+
else:
21+
if len(cache) == cacheSize and cacheSize > 0: # 캐시가 가득찼다면, 맨 앞에 하나 버린다. 이때 cacheSize가 0인 경우도 있다. 이때는 popleft하면 에러가 나므로 cacheSize가 0이상인 경우만 처리되도록 한다.
22+
cache.popleft()
23+
cache.append(city)
24+
totalTime += 5 # cache miss
25+
26+
27+
return totalTime
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from collections import Counter
2+
3+
def solution(str1, str2):
4+
answer = 0
5+
A = [] # str1
6+
B = [] # str2
7+
8+
# str1과 str2 전처리: 2개씩 끊어서 저장하기
9+
for i in range(len(str1)):
10+
temp = str1[i:i+2].lower() # 대소문자 구분 X
11+
if temp.isalpha() and len(temp) == 2:
12+
A.append(temp)
13+
14+
for i in range(len(str2)):
15+
temp = str2[i:i+2].lower() # 대소문자 구분 X
16+
if temp.isalpha() and len(temp) == 2:
17+
B.append(temp)
18+
19+
# Counter 객체로 변환
20+
A = Counter(A)
21+
B = Counter(B)
22+
23+
# 교집합과 합집합 구하기
24+
intersection = A & B
25+
union = A | B
26+
27+
# 합집합의 수가 0인 경우에 대한 예외처리
28+
if sum(union.values()) == 0:
29+
answer = 65536
30+
else:
31+
answer = int(sum(intersection.values()) / sum(union.values()) * 65536)
32+
33+
return answer
34+

0 commit comments

Comments
 (0)