CorrelationTTV (Easy version)
This is the easy version of RollingCorr. The only difference between this and the hard version are the constraints. Solving one problem does not guarantee solving the other.
This is an interactive problem.
Implement the class RollingCorr, which persistently calculates the Pearson correlation coefficient of a two-dimensional incoming data stream.
You will receive a sequence of pairs . After each pair arrives, you must update your internal state and return the correlation of all points seen so far.
The interactor is non-adaptive, meaning the pair you receive is independent of your previous correlation calculation. However, the judge will immediately terminate if your correlation is incorrect.
Constructor
RollingCorr()
Your class must provide a constructor. It should initialize any internal state required to support persistent rolling updates.
- (Details of what you initialize are intentionally left unspecified; choose what you need.)
Methods
update(double x, double y)
The judge will call
update(x, y)repeatedly.Each call appends the pair to the stream.
Return the Pearson correlation coefficient of all appended pairs so far (including the new one).
Pearson Correlation Coefficient
The correlation after points is:
Notes
If fewer than two points have been seen, return
0.0.If the correlation is undefined (for example, all are identical or all are identical), return
0.0.Your answer will be accepted if it is within
1e-5absolute or relative error of the expected value.You do not need to write any input/output parsing.
Constraints
xandyare finitedoublevalues.|x|,|y|- The number of updates may be at most
1e3.
Inserting data stream...