모든 챌린지
보통 0명 해결 정답률 0%

K-th Smallest Element

Find the k-th smallest value without fully sorting.

Return the k-th smallest value of the list (k counts from 1). Quickselect finds it in average O(n) time without sorting everything.

Implement kth_smallest(numbers, k).

Input

numbers: The list of integers. k: Which smallest value to return, starting at 1.

Output

Return the k-th smallest value.

제약 조건

1 ≤ k ≤ length ≤ 200,000

예시

예시 1
입력:
k = 3
numbers = [7, 10, 4, 3, 20, 15]

예상 출력:
expected = 7
입력:
long long k = 3;
std::vector<long long> numbers = {7, 10, 4, 3, 20, 15};

예상 출력:
long long expected = 7;
입력:
long k = 3;
long[] numbers = new long[] {7, 10, 4, 3, 20, 15};

예상 출력:
long expected = 7;

설명

Sorted: 3, 4, 7, 10, 15, 20; the 3rd smallest is 7.

예시 2
입력:
k = 2
numbers = [5, 5, 1]

예상 출력:
expected = 5
입력:
long long k = 2;
std::vector<long long> numbers = {5, 5, 1};

예상 출력:
long long expected = 5;
입력:
long k = 2;
long[] numbers = new long[] {5, 5, 1};

예상 출력:
long expected = 5;

설명

Sorted: 1, 5, 5; the 2nd smallest is 5 (duplicates count).

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.