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

Step Tracker Class

Simulate a step tracker object: record daily steps and answer total, active day, average and best day queries.

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

  • Model a class as fields plus methods
  • Update counters as new data arrives
  • Handle queries before any data is recorded

โจทย์

ดูฉบับแปล

A fitness app has a StepTracker class. It is created with a goal min_active: a day counts as active when its steps are at least that number. Instead of writing the class, process its method calls as a list of operations:

  • add N: record a new day with N steps (no output).
  • total: output the total steps over all recorded days.
  • active: output how many recorded days are active.
  • average: output the average steps per day, rounded down to a whole number, or 0 if no day is recorded.
  • best: output the most steps in a single day, or 0 if no day is recorded.

Implement step_tracker(min_active, operations).

Input

min_active: The number of steps that makes a day active.

operations: The operations, processed in order.

Output

Return the outputs of the query operations in the order they happen.

เงื่อนไข

1 ≤ min_active ≤ 100,000

0 ≤ number of operations ≤ 1,000

0 ≤ N ≤ 100,000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
min_active = 8000
operations = ["add 9000", "add 4500", "active", "add 12000", "total", "average", "best"]

ผลลัพธ์ที่คาดหวัง:
expected = [1, 25500, 8500, 12000]
อินพุต:
long long min_active = 8000;
std::vector<std::string> operations = {"add 9000", "add 4500", "active", "add 12000", "total", "average", "best"};

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {1, 25500, 8500, 12000};
อินพุต:
long min_active = 8000;
string[] operations = new string[] {"add 9000", "add 4500", "active", "add 12000", "total", "average", "best"};

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

คำอธิบาย

After two days only 9000 reaches 8000, so active is 1. Then the total is 25500, the average is 25500 / 3 = 8500 and the best day is 12000.

ตัวอย่างที่ 2
อินพุต:
min_active = 5000
operations = ["average", "best", "add 5000", "add 4999", "active", "average"]

ผลลัพธ์ที่คาดหวัง:
expected = [0, 0, 1, 4999]
อินพุต:
long long min_active = 5000;
std::vector<std::string> operations = {"average", "best", "add 5000", "add 4999", "active", "average"};

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {0, 0, 1, 4999};
อินพุต:
long min_active = 5000;
string[] operations = new string[] {"average", "best", "add 5000", "add 4999", "active", "average"};

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

คำอธิบาย

With no days both queries give 0. 5000 counts as active but 4999 does not, and (5000 + 4999) / 2 rounds down to 4999.

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

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

คำใบ้ 1

Keep running values (total, days, active days, best) instead of rescanning a list.

คำใบ้ 2

A day is active when steps are greater than or equal to the goal.

คำใบ้ 3

Check for zero days before dividing.

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

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

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

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

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

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

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

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