ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Above-Average Values

Keep the values that are greater than the average.

สิ่งที่คุณจะได้เรียนรู้

  • Compute an average first
  • Filter values in a second pass
  • Keep the original order

โจทย์

ดูฉบับแปล

Compute the average of the list, then keep only the values strictly greater than it, in their original order.

Implement above_average(numbers).

Input

numbers: The list of integers.

Output

Return the values above the average (an empty list for an empty input).

เงื่อนไข

0 ≤ length ≤ 10,000; -1,000,000 ≤ each value ≤ 1,000,000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
numbers = [1, 2, 3, 4, 10]

ผลลัพธ์ที่คาดหวัง:
expected = [10]
อินพุต:
std::vector<long long> numbers = {1, 2, 3, 4, 10};

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {10};
อินพุต:
long[] numbers = new long[] {1, 2, 3, 4, 10};

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {10};

คำอธิบาย

The average is 20 / 5 = 4; only 10 is greater than 4.

ตัวอย่างที่ 2
อินพุต:
numbers = [5, 5, 5]

ผลลัพธ์ที่คาดหวัง:
expected = []
อินพุต:
std::vector<long long> numbers = {5, 5, 5};

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {};
อินพุต:
long[] numbers = new long[] {5, 5, 5};

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {};

คำอธิบาย

Every value equals the average 5, so none is greater: [].

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

First find the list’s average, then keep values strictly above it.

คำใบ้ 2

This usually requires one pass to measure the total and another to collect matches.

คำใบ้ 3

If the list is empty, the answer should also be empty.

ความซับซ้อนที่คาดหวัง

O(n) time, O(1) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก