Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- BFS
- 슬라이딩 윈도우
- 세그먼트 트리
- 투 포인터
- 구현
- Python
- 맵
- 누적 합
- 트리
- 브루트포스
- 이분 탐색
- 에라토스테네스의 체
- 플로이드-워셜
- 싸피
- boj
- JavaScript
- SSAFY
- 13164
- 모던 JavaScript 튜토리얼
- 수학
- 정수론
- 2357
- DP
- 문자열
- 애드 혹
- 그래프
- DFS
- 정렬
- 해시 테이블
- 그리디
Archives
- Today
- Total
흙금이네 블로그
[BOJ] 1270 - 전쟁 - 땅따먹기 (Python) 본문
아이디어 #1
딕셔너리를 이용하여 과반수인 병사 번호를 찾는다.
풀이 #1
import sys
input = sys.stdin.readline
def solution():
n = int(input())
for _ in range(n):
T, *numbers = map(int, input().split())
cnt_dict = dict()
for number in numbers:
temp = cnt_dict.get(number, 0)
if temp >= T//2:
print(number)
break
cnt_dict[number] = temp+1
else:
print('SYJKGW')
solution()
아이디어 #2
보이어-무어 다수결 투표 알고리즘을 이용하여 과반수인 병사 번호를 찾는다.
풀이 #2
import sys
input = sys.stdin.readline
def solution():
n = int(input())
for _ in range(n):
T, *numbers = map(int, input().split())
res = cnt = 0
for number in numbers:
if cnt == 0:
res = number
if number == res:
cnt += 1
else:
cnt -= 1
if numbers.count(res) > T//2:
print(res)
else:
print('SYJKGW')
solution()
'알고리즘' 카테고리의 다른 글
[BOJ] 22856 - 트리 순회 (Python, JavaScript) (0) | 2023.05.05 |
---|---|
[BOJ] 1477 - 휴게소 세우기 (Python) (0) | 2023.05.04 |
[BOJ] 1563 - 개근상 (Python) (0) | 2023.05.03 |
[BOJ] 1327 - 소트 게임 (Python) (0) | 2023.05.02 |
[BOJ] 3078 - 좋은 친구 (Python) (0) | 2023.05.01 |
Comments