You are given a list of data points data_points, where each data point is [timestamp, value, tag]. You are also given a string tag and two integers start_time and end_time.
Return the sum of the value fields of all data points whose tag matches tag and whose timestamp falls within the inclusive range [start_time, end_time]. If no data point matches, return 0.
Function Signature
def sum_tag_values_in_range(
data_points: list[list[str]], tag: str, start_time: int, end_time: int
) -> int:
Rules
-
Every data point is a list of three strings. The timestamp and the value are base-10 integers written as strings, such as
"12"
or
"-8"
. Timestamps are compared by numeric value.
-
A data point matches when its tag is exactly equal to
tag
(whole string, case-sensitive) and
start_time <= timestamp <= end_time
. Both ends of the range are included.
-
Data points can appear in any order, and several can share a timestamp. Every matching data point adds its value once.
Constraints
-
1 <= len(data_points) <= 10^5
-
0 <= timestamp <= 10^9
-
-10^4 <= value <= 10^4
-
1 <= len(tag) <= 20
-
start_time <= end_time
Examples
Example 1
Input: data_points = [["1","10","cpu"],["2","20","mem"],["3","30","cpu"],["4","40","cpu"],["5","50","mem"]], tag = "cpu", start_time = 1, end_time = 4
Output: 80
The "cpu" points are at timestamps 1, 3 and 4, all inside [1, 4], so the sum is 10 + 30 + 40 = 80.
Example 2
Input: data_points = [["1","10","cpu"],["2","20","mem"],["3","30","cpu"],["4","40","cpu"],["5","50","mem"]], tag = "mem", start_time = 2, end_time = 5
Output: 70
The "mem" points are at timestamps 2 and 5, both inside [2, 5], so the sum is 20 + 50 = 70.
Example 3
Input: data_points = [["5","7","disk"],["7","-8","disk"],["12","4","disk"],["7","100","cpu"],["13","9","disk"]], tag = "disk", start_time = 6, end_time = 12
Output: -4
Only the "disk" points at timestamps 7 and 12 are in range (12 counts because the end is inclusive), so the sum is -8 + 4 = -4. The "disk" points at 5 and 13 fall outside the range, and the "cpu" point is skipped even though its timestamp is inside it.