4059. Design Exam Scores Tracker¶
4059. Design Exam Scores Tracker
Medium
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 theExamTrackerobject.void record(int time, int score): Alice takes a new exam at timetimeand achieves the scorescore.long long totalScore(int startTime, int endTime): Returns an integer that represents the total score of all exams taken by Alice betweenstartTimeandendTime(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 increasingtime. - Alice will never ask for total scores that require information from the future. That is, if the latest
record()is called withtime = t, thentotalScore()will always be called withstartTime <= endTime <= t.
Example 1:
Input:
["ExamTracker", "record", "totalScore", "record", "totalScore", "totalScore", "totalScore", "totalScore"]
[[], [1, 98], [1, 1], [5, 99], [1, 3], [1, 5], [3, 4], [2, 5]]
Output:
[null, null, 98, null, 98, 197, 0, 99]
Explanation
ExamTracker examTracker = new ExamTracker();examTracker.record(1, 98); // Alice takes a new exam at time 1, scoring 98.
examTracker.totalScore(1, 1); // Between time 1 and time 1, Alice took 1 exam at time 1, scoring 98. The total score is 98.
examTracker.record(5, 99); // Alice takes a new exam at time 5, scoring 99.
examTracker.totalScore(1, 3); // Between time 1 and time 3, Alice took 1 exam at time 1, scoring 98. The total score is 98.
examTracker.totalScore(1, 5); // Between time 1 and time 5, Alice took 2 exams at time 1 and 5, scoring 98 and 99. The total score is
98 + 99 = 197.examTracker.totalScore(3, 4); // Alice did not take any exam between time 3 and time 4. Therefore, the answer is 0.
examTracker.totalScore(2, 5); // Between time 2 and time 5, Alice took 1 exam at time 5, scoring 99. The total score is 99.
Constraints:
1 <= time <= 1091 <= score <= 1091 <= startTime <= endTime <= t, wheretis the value oftimefrom the most recent call ofrecord().- Calls of
record()will be made with strictly increasingtime. - After
ExamTracker(), the first function call will always berecord(). - At most
105calls will be made in total torecord()andtotalScore().
Solution¶
class ExamTracker {
private ArrayList<Long> pref;
private ArrayList<Pair> res;
static class Pair {
int time, cost;
public Pair(int time, int cost) {
this.time = time;
this.cost = cost;
}
@Override
public String toString() {
return "(" + time + ", " + cost + ")";
}
}
public ExamTracker() {
res = new ArrayList<>();
pref = new ArrayList<>();
}
public void record(int time, int score) {
long toAdd = 0;
if (pref.size() > 0)
toAdd += pref.get(pref.size() - 1) * 1L + score * 1L;
else
toAdd = score * 1L;
pref.add(toAdd);
res.add(new Pair(time, score));
}
public long totalScore(int startTime, int endTime) {
long res = 0;
int left = getLeft(startTime), right = getRight(endTime);
if (left == - 1 || right == -1)
return 0L;
res = pref.get(right);
if (left - 1 >= 0)
res -= pref.get(left - 1);
return res;
}
private int getLeft(int target) {
int low = 0, high = res.size() - 1, ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (res.get(mid).time >= target) {
ans = mid;
high = mid - 1;
} else
low = mid + 1;
}
return ans;
}
private int getRight(int target) {
int low = 0, high = res.size() - 1, ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (res.get(mid).time <= target) {
ans = mid;
low = mid + 1;
} else
high = mid - 1;
}
return ans;
}
}
/**
Your ExamTracker object will be instantiated and called as such:
ExamTracker obj = new ExamTracker();
obj.record(time,score);
long param_2 = obj.totalScore(startTime,endTime);
*/
/**
* Your ExamTracker object will be instantiated and called as such:
* ExamTracker obj = new ExamTracker();
* obj.record(time,score);
* long param_2 = obj.totalScore(startTime,endTime);
*/
Complexity Analysis¶
- Time Complexity: O(?)
- Space Complexity: O(?)
Explanation¶
[Add detailed explanation here]