A
A
Alexey Mairin2018-03-02 21:33:31
Algorithms
Alexey Mairin, 2018-03-02 21:33:31

How to find the largest sequence in O(n)?

Hello!
An array of numbers is given, it is necessary to find the length of the longest sequence in it (the sequence can only be with a difference of 1) in O (n)
Actually, here is an example:
1 2 433 500 3 900 4
Therefore, the longest sequence is 4 (1 2 3 4)
AND here's how to implement it all in 1 pass
Maybe some tips/links/articles

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry, 2018-03-02
@demon416nds

banal pass with counting the length of the current sequence

S
Sergey Sokolov, 2018-03-02
@sergiks

In English. the problem is called " longest increasing subsequence "
Here is a publication of an algorithm for solving this problem for n log n−n log log n + O(n)
Upd. In Russian it is called the Largest Increasing Subsequence Problem (LIS).

A
Astrohas, 2018-03-02
@Astrohas

https://www.youtube.com/watch?v=t2DpD9GnhfU gurowitz explains more clearly.
Just take the standard algorithm, with the nth item, check its difference in values, and if the difference is 1, then only increase the counter.

T
tsarevfs, 2018-03-02
@tsarevfs

Store a hash map where the key is the value of the last element in the sequence encountered earlier, and the value is the length. And then we go through the array and fill it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question