본문 바로가기
python-algorithm

codeforces 158A - Next Round

by 무적김두칠 2023. 1. 25.

https://codeforces.com/problemset/problem/158/A

 

Problem - 158A - Codeforces

 

codeforces.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from bisect import bisect_right, bisect_left
 
 
def sol(nums, n, k):
    nums.sort()
    if nums[-k] != 0:
        answer = (n - bisect_left(nums, nums[-k]))
    else:
        answer = n-nums.count(0)
    return answer
 
 
if __name__ == '__main__':
    n, k = map(int, input().split())
    nums = list(map(int, input().split()))
    print(sol(nums, n, k))
 
cs

이분탐색으로 풀면 쉽게 푸는데 다만 k번째가 0일때는 예외처리해주셔야합니다

It's easy to solve by bisect, but you have to make an exception when the kth is 0.

반응형

'python-algorithm' 카테고리의 다른 글

codeforces 1742A - Sum  (0) 2023.01.25
codeforces 133A - HQ9+  (0) 2023.01.25
백준 26560 Periods  (0) 2023.01.24
leetcode 1684. Count the Number of Consistent Strings  (2) 2023.01.22
leetcode 58. Length of Last Word  (0) 2023.01.21

댓글