모든 챌린지
보통 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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

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