โจทย์
An inversion is a pair of positions i < j with numbers[i] > numbers[j]. Count all inversions. Lists can be long, so a merge-sort based O(n log n) count is needed.
Implement count_inversions(numbers).
Input
numbers: The list of integers.
Output
Return the number of inversions.
เงื่อนไข
0 ≤ length ≤ 200,000
ตัวอย่าง
ตัวอย่างที่ 1
อินพุต:
numbers = [2, 4, 1, 3, 5]
ผลลัพธ์ที่คาดหวัง:
expected = 3
อินพุต:
std::vector<long long> numbers = {2, 4, 1, 3, 5};
ผลลัพธ์ที่คาดหวัง:
long long expected = 3;
อินพุต:
long[] numbers = new long[] {2, 4, 1, 3, 5};
ผลลัพธ์ที่คาดหวัง:
long expected = 3;
คำอธิบาย
Out-of-order pairs: (2,1), (4,1), (4,3): 3 inversions.
ตัวอย่างที่ 2
อินพุต:
numbers = [1, 2, 3]
ผลลัพธ์ที่คาดหวัง:
expected = 0
อินพุต:
std::vector<long long> numbers = {1, 2, 3};
ผลลัพธ์ที่คาดหวัง:
long long expected = 0;
อินพุต:
long[] numbers = new long[] {1, 2, 3};
ผลลัพธ์ที่คาดหวัง:
long expected = 0;
คำอธิบาย
Already sorted: 0 inversions.
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน