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
- 강화학습
- bineary search
- 딕셔너리
- 파이썬 알고리즘
- 코딩테스트
- HTTP
- 해시
- 파이썬
- 프로그래머스
- 캐싱
- 알고리즘
- 비지도학습
- post
- Merge sort
- 깊이우선탐색
- 자바
- 이진탐색
- 지도학습
- 코테
- 딥러닝
- rest api
- BOJ
- 파이썬 오류
- 스택과 힙
- 코딩
- 오버라이딩
- 머신러닝
- 백준
- 멱등
- 너비우선탐색
Archives
- Today
- Total
chae._.chae
[Algorithm] 백준 #1205 등수 구하기 본문
728x90
반응형
https://www.acmicpc.net/problem/1205
import sys
from collections import defaultdict
input = sys.stdin.readline
N, new_score, P = map(int, input().split())
scores = list(map(int, input().split()))
# 등수 계산
def get_rank(scores, new_score):
dict = defaultdict(int)
for num in scores:
dict[num] += 1
answer = 0
for key, value in dict.items():
if key == new_score:
answer += 1
print(answer)
else: # 정답이 아니야
answer += value
scores.sort(reverse=True)
if len(scores) == P: # 꽉 찬 경우
if scores[-1] >= new_score: # 점수가 더 낮으면
print(-1)
else:
scores.pop() # 오른쪽 원소 제거
scores.append(new_score)
scores.sort(reverse=True)
get_rank(scores, new_score)
else:
scores.append(new_score)
scores.sort(reverse=True)
get_rank(scores, new_score)
등수 구할때, answer을 이용해서 순차적으로 나아가며 모든 점수의 등수를 각각 구하는 절차로 진행
# 다른 풀이
import sys
input = sys.stdin.readline
N, new, P = map(int, input().split())
if N == 0:
print(1)
else:
score = list(map(int, input().split()))
if N == P and score[-1] >= new: # 점수가 더 낮으면
print(-1)
else:
for i in range(N): # 점수리스트에 넣어서 값을 비교하면서
if new >= score[i]:
print(i+1)
break
else: # 새로운 점수보다 작거나 같은 점수가 없다면 N+1
print(N+1)
새로 입력 받은 점수를 score에 append하고, 정렬하지 않고 값을 비교하며 진행함.
728x90
'파이썬 알고리즘 > BOJ' 카테고리의 다른 글
| [Algorithm] 백준 #9017 크로스 컨트리 (0) | 2024.07.07 |
|---|---|
| [Algorithm] 백준 #1244 스위치 켜고 끄기 (0) | 2024.07.07 |
| [Algorithm] 백준 #20125 쿠키의 신체 측정 (0) | 2024.07.07 |
| [Algorithm] 백준 #10816 숫자 카드 2 (0) | 2024.06.18 |
| [Algorithm] 백준 DP #11053 가장 긴 증가하는 부분 수열 (0) | 2023.07.08 |