[LeetCode] Design Exam Scores Tracker

3709. Design Exam Scores Tracker

Alice frequently takes exams and wants to track her scores and calculate the total scores over specific time periods.

Implement the ExamTracker class:

  • ExamTracker(): Initializes the ExamTracker object.
  • void record(int time, int score): Alice takes a new exam at time time and achieves the score score.
  • long long totalScore(int startTime, int endTime): Returns an integer that represents the total score of all exams taken by Alice between startTime and endTime (inclusive). If there are no recorded exams taken by Alice within the specified time interval, return 0.

It is guaranteed that the function calls are made in chronological order. That is,

  • Calls to record() will be made with strictly increasing time.
  • Alice will never ask for total scores that require information from the future. That is, if the latest record() is called with time = t, then totalScore() will always be called with startTime <= endTime <= t.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

class ExamTracker {
vector<pair<long long, long long>> pre;
int find(int x) {
return lower_bound(begin(pre), end(pre), pair<long long, long long>{x + 1, INT_MIN}) - begin(pre) - 1;
}
public:
ExamTracker() {
pre = {{0,0}};
}

void record(int time, int score) {
pre.push_back({time, pre.back().second + score});
}

double totalScore(int startTime, int endTime) {
int r = find(endTime), l = find(startTime - 1);
if(l == r) return 0;
return pre[r].second - pre[l].second;
}
};

/**
* Your ExamTracker object will be instantiated and called as such:
* ExamTracker* obj = new ExamTracker();
* obj->record(time,score);
* double param_2 = obj->avgScore(startTime,endTime);
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/10/13/PS/LeetCode/design-exam-scores-tracker/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.