โจทย์
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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน