MovingAverages
SRM 209 · 2004-08-28 · by Wernie
SRM 209 · 2004-08-28 · by Wernie · Simple Math
Problem Statement
Problem Statement
Moving averages are well known in stock charts analysis.
They are used to emphasize the direction of a trend and to smooth out fluctuations.
Athletes may use moving averages to analyze their training results.
Given aString[] times containing the times from successive training sessions
(e.g. the time to cycle a certain leg)
and an int n, return a int[] containing the n-moving averages in seconds for
these times, with each average rounded down.
Each element of times is in the format "hh:mm:ss" (quotes for clarity), where hh, mm and ss are two digit numbers (with a leading zero if necessary) indicating the number of hours, minutes and seconds, respectively.
A n-moving average is the average (i.e. the arithmetic mean) of n consecutive times. So for t times given, t-n+1 n-moving averages are to be calculated. The first average is composed from the times 1 to n, the second average from the times 2 to n+1 and so on, the last average is composed from the times t-n+1 to t.
Given a
Each element of times is in the format "hh:mm:ss" (quotes for clarity), where hh, mm and ss are two digit numbers (with a leading zero if necessary) indicating the number of hours, minutes and seconds, respectively.
A n-moving average is the average (i.e. the arithmetic mean) of n consecutive times. So for t times given, t-n+1 n-moving averages are to be calculated. The first average is composed from the times 1 to n, the second average from the times 2 to n+1 and so on, the last average is composed from the times t-n+1 to t.
Constraints
- times contains between 1 and 50 elements, inclusive.
- Each element of times is in the format "hh:mm:ss" (quotes for clarity), wherehh is a two digit number (with a leading zero if necessary) between 0 and 23, inclusive,mm is a two digit number (with a leading zero if necessary) between 0 and 59, inclusive,ss is a two digit number (with a leading zero if necessary) between 0 and 59, inclusive.
- n is between 1 and the number of elements in times, inclusive.
Examples
0)
{"01:19:10", "01:17:40", "01:19:44", "01:17:23", "01:17:07"}
3
Returns: { 4731, 4695, 4684 }
4731 (01:18:51) is the average of of the first three times, namely 01:19:10, 01:17:40, and 01:19:44. 4695 (01:18:15) is the average of the middle three times (01:17:40, 01:19:44, 01:17:23). Finally, 4684 (01:18:04) is the average of the last three times (01:19:44, 01:17:23, 01:17:07).
1)
{"01:19:10", "01:17:40", "01:19:44", "01:17:23", "01:17:07"}
1
Returns: { 4750, 4660, 4784, 4643, 4627 }
The 1-moving averages are just the times given.
2)
{"01:19:10", "01:17:40", "01:19:44", "01:17:23", "01:17:07"}
5
Returns: { 4692 }
The 5-moving averages of five times is just one value, the average of all the five values.
3)
{"13:33:44", "13:33:42", "13:33:41"}
2
Returns: { 48823, 48821 }
4)
{"00:00:00", "00:00:00", "00:00:00", "00:00:01", "00:00:01", "00:00:01"}
3
Returns: { 0, 0, 0, 1 }
Submissions are judged against all 23 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class MovingAverages with a public method vector<int> calculate(vector<string> times, int n) · 23 test cases · 2 s / 256 MB per case