I am working on a project and came across the following problem I have to solve.
Imagine we have a time series data Ts
which is an array of pair, say each element is (Xi, Yi)
. The length of this array is constant N
but is updated regularly. Each update consists of two steps,
- We remove
Ts[0]
- Add a new pair
(X, Y)
to the end ofTs
Essentially like an online update to a sliding window of size N
.
Now here comes the problem. At any given times (could be after an update or before), we are given a nonzero input integer A
and B
. The goal is to find such i
between 0
and N-1
inclusive where the term (A-Xi)/(B-Yi)
is maximized.
Another constraints I would like to mention here,
(X0, X1, X2, ... X(N-1))
is non-decreasing,Xi
is non-zero non-negative integer for alli
.(Y0, Y1, Y2, ... Y(N-1))
is non-decreasing,Yi
is non-zero non-negative integer for alli
.A
andB
are both non-zero non-negative integer.- For any query, we can safely assume
B > Yi
for every possiblei
. Technically for the original problem,B
could be equal to the lastY(N-1)
. But this is a boundary condition and we would just eliminateN-1
in this case. - We can also safely assume
A > Xi
for every possiblei
. Unlike constraint (4). It is not possible forA - Xi
to be 0 in any case.
A naive approach would be to iterate through all N
possibilities and easily find the solution (This is what I am doing). But I am thinking of a way to optimize this in the case of very large N
. A query could come at any time for however many number of times, for example,
- Query, Update, Query (1000 times), Update. Or could also be,
- Query, Update, Query, Update, ...
Is there any room for optimization here? I am interested to see a proof from more mathematical standpoint if there is none as well.
abs(A+Xi / B-Yi)
to be maximized, orA+Xi / B-Yi
?? $\endgroup$