# A de-weight algorithm for ordered vectors

Source: Internet
Author: User

Disclaimer: This article refers to the XUETANGX data structure of the teacher Ding's related courses, is a personal summary.

First of all, this must be a simple and seemingly straightforward proposition.
For ordered vectors, special attention is the "ordered" vector, seizing the important feature, that is, the same elements must be in the same uninterrupted section, that is, the same elements are adjacent to form an interval.

Like this:

Finally to do:

That is, the repeating element retains only one.

Considering that the repeating elements are immediate, it is easy to write the following algorithm directly:

That is, the same interval for each element preserves only a single element.

By the while loop and the remove operation can be learned that the algorithm complexity is O (n^2);

A little thought will reveal that the inefficiency of the above algorithm is rooted in the fact that the same element can be used as a successor to the deleted element multiple times forward.

For this, it is natural to consider repeating the same elements in batches.

You are likely to do this when you implement it:

1. Find the starting position of the current interval I, find the starting position of the next interval J;

2. Call the Remove-like interface to move the [J and its successors] uniformly to [i+1 and its successors].

But half of what you'll find is that this optimizes the number of calls to remove or its similar interfaces, but the complexity of the interface does not change, and it is still O (n).

The whole algorithm is still the complexity of O (n^2).

In fact, the idea that the above-mentioned uniform deletion is straightforward in terms of interval is certainly correct, but we do not need to actually call remove or a similar interface for a real delete operation when implemented.

Instead, you can implicitly delete duplicate elements.

Cash into code as:

In fact, the key part is such an operation:

Use an example to understand the beauty of this process:

It is equivalent to taking the first element from each repeating interval directly at the back of the previous deduplication interval.

Once a traversal is completed, the go-to-heavy interval is maintained, and the elements behind it can be truncated directly.

This implicit operation is often the most difficult to surmount and break through in thinking.

It is as if the mind always unconsciously simulates the process of interval deletion, and then moves the whole tail forward in a shortsighted manner.

And here, the really useful information is actually only the repetition interval of the value of that one is repeated.

Focus on the information that is really useful for solving problems, and look at the big picture, which is exactly what we need to exercise.

I hereby make a note.

A de-weight algorithm for ordered vectors

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.