Algorithm for finding the maximum difference in an array of numbers

后端 未结 7 483
夕颜
夕颜 2021-02-04 03:57

I have an array of a few million numbers.

double* const data = new double (3600000);

I need to iterate through the array and find the range (th

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 04:03

    This type of question belongs to a branch of algorithms called streaming algorithms. It is the study of problems which require not only an O(n) solution but also need to work in a single pass over the data. the data is inputted as a stream to the algorithm, the algorithm can't save all of the data and then and then it is lost forever. the algorithm needs to get some answer about the data, such as for instance the minimum or the median.

    Specifically you are looking for a maximum (or more commonly in literature - minimum) in a window over a stream.

    Here's a presentation on an article that mentions this problem as a sub problem of what they are trying to get at. it might give you some ideas.

    I think the outline of the solution is something like that - maintain the window over the stream where in each step one element is inserted to the window and one is removed from the other side (a sliding window). The items you actually keep in memory aren't all of the 1000 items in the window but a selected representatives which are going to be good candidates for being the minimum (or maximum).

    read the article. it's abit complex but after 2-3 reads you can get the hang of it.

提交回复
热议问题