Skip to content

Commit 0fb43e4

Browse files
authored
Merge pull request #130 from AlgorithmStudy-Allumbus/minjeong
Minjeong / 1월 4주차 / 5문제
2 parents 620d696 + 2a064b3 commit 0fb43e4

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sys
2+
3+
input = sys.stdin.readline
4+
5+
# 입력
6+
N, C = map(int, input().split()) # N: 물건의 개수, C: 목표 무게
7+
items = list(map(int, input().split())) # 물건의 무게
8+
9+
items.sort() # 이분탐색을 위해 오름차순 정렬
10+
11+
12+
def binary_search(left, right, target):
13+
"""이분 탐색 함수: 배열에서 target이 존재하면 1, 없으면 0 반환"""
14+
while left <= right:
15+
mid = (left + right) // 2
16+
if items[mid] == target:
17+
return 1
18+
elif items[mid] > target:
19+
right = mid - 1
20+
else:
21+
left = mid + 1
22+
return 0
23+
24+
25+
def check(N, C):
26+
"""조건을 만족하는 조합이 있는지 확인"""
27+
28+
# 1개만 선택해도 되는 경우
29+
if C in items:
30+
return 1
31+
32+
# 2개 또는 3개 조합 확인
33+
start, end = 0, N - 1
34+
while start < end:
35+
total = items[start] + items[end]
36+
37+
# 2개로도 C를 만족하는 경우
38+
if total == C:
39+
return 1
40+
41+
# 2개의 물건 무게 합이 C보다 크다면 end를 왼쪽으로
42+
elif total > C:
43+
end -= 1
44+
# 2개의 물건 무게의 합이 C보다 작을 경우 = 3개를 확인해야 하는 경우
45+
else:
46+
diff = C - total # 부족한 무게 계산
47+
# 차이값이 start나 end에 위치한 값이 아니면서 동시에 부족한 무게를 이분탐색으로 찾는다.
48+
if items[start] != diff and items[end] != diff and binary_search(start, end, diff):
49+
return 1
50+
# 여기서 위의 조건이 성립되지 않는다면 start를 오른쪽으로 이동
51+
start += 1
52+
return 0
53+
54+
55+
# 정답 출력
56+
print(check(N, C))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
3+
input = sys.stdin.readline
4+
5+
# 입력 받기
6+
N = int(input()) # 사람 수
7+
taller = list(map(int, input().split())) # 각 사람이 기억하는 왼쪽 큰 사람의 수
8+
9+
# 결과 리스트 초기화
10+
result = [None] * N
11+
12+
# 키가 작은 사람(1부터 N까지)을 처리
13+
for i in range(1, N + 1): # i는 현재 사람의 키
14+
count = taller[i - 1] # 현재 사람이 기억하는 왼쪽 큰 사람의 수
15+
idx = 0 # result에서 위치를 찾기 위한 인덱스
16+
17+
while count > 0 or result[idx] is not None: # 빈 자리가 아니고 count가 남아있다면 이동
18+
if result[idx] is None: # 빈 자리일 경우에만 count 감소
19+
count -= 1
20+
idx += 1 # 다음 자리로 이동
21+
22+
result[idx] = i # 적절한 자리에 현재 사람 배치
23+
24+
# 결과 출력
25+
print(*result)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 입력 받기
5+
students = []
6+
for _ in range(int(input())):
7+
name, day, month, year = input().split()
8+
students.append((int(year), int(month), int(day), name)) # 튜플로 저장
9+
10+
# 정렬: 연도 -> 월 -> 일 기준
11+
students.sort()
12+
13+
# 가장 나이가 적은 사람과 많은 사람 출력
14+
print(students[-1][3]) # 가장 나이가 적은 사람
15+
print(students[0][3]) # 가장 나이가 많은 사람
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
# 입력 받기
7+
N = int(input()) # 카드의 개수
8+
queue = deque(range(1, N + 1)) # 1부터 N까지 큐 생성
9+
answer = [] # 버린 카드를 저장할 리스트
10+
11+
# 큐에서 카드 처리
12+
while len(queue) > 1:
13+
answer.append(queue.popleft()) # 맨 앞의 카드를 버린다
14+
queue.append(queue.popleft()) # 그다음 카드를 맨 뒤로 옮긴다
15+
16+
# 마지막 남은 카드 추가
17+
answer.append(queue.popleft())
18+
19+
# 출력
20+
print(' '.join(map(str, answer))) # 버린 카드와 마지막 남은 카드 출력
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 입력
5+
N = int(input())
6+
arr = list(map(int, input().split()))
7+
8+
arr.sort() # 투 포인터를 적용하기 위해 배열을 오름차순으로 정렬
9+
cnt = 0 # 좋은 수 개수
10+
11+
for i in range(N):
12+
target = arr[i] # 현재 '좋은 수'인지 확인할 목표값
13+
left, right = 0, N-1
14+
while left < right:
15+
# 타겟 값과 동일할 경우
16+
if target == arr[left] + arr[right]:
17+
if right == i: # target이 양수이고 자기 자신이라면
18+
right -= 1
19+
elif left == i: # target이 음수이고 자기 자신이라면
20+
left += 1
21+
else: # 좋은 수 찾음
22+
cnt += 1
23+
break
24+
# 타겟 값이 더 클 경우, 왼쪽 포인터를 오른쪽으로 이동
25+
elif target > arr[left] + arr[right]:
26+
left += 1
27+
# 타겟 값이 더 작을 경우, 오른쪽 포인터를 왼쪽으로 이동
28+
else:
29+
right -= 1
30+
31+
# 정답 출력
32+
print(cnt)

0 commit comments

Comments
 (0)