Nine-chapter algorithm surface question 37 main element

Source: Internet
Author: User

Nine Chapters count judges Net-original website

http://www.jiuzhang.com/problem/37/


Topics

The main element (Majority number) defines numbers that are more than half as many occurrences in an array. Find this number. Requires the use O (1) of extra space and O (n) time.


Step 1: Find this number if the array exists and there is only one occurrence that is strictly more than 1/3. Requires the use O (1) of extra space and O (n) time.


Step 2: Find this number if the array exists and there is only one occurrence that is strictly more than 1/k. Additional space required to use O (k) and Time of O (n)


Answer

Use the offset method. Once you find that there are two different numbers in the array, it is deleted until the remaining number is the same. The remaining number is the main element at this point. Because after each offset operation, the remaining number of the main elements must be more than half. The implementation of the time, record a candidate and its occurrence count, traverse each number, if count==0, then put candidate to traverse to the number, otherwise see the number of traversal and candidate is equal, if equal, then count++, otherwise count--(offset), the candidate is the main element after the traversal is over.


Advanced 1: The idea is that if there are 3 different numbers, it will be cancelled out. Record the number of occurrences of two candidate and each candidate respectively. If the number of traversed and two candidate are not equal, count will be reduced by 1. There may be two candidate left, and then iterate through the entire array to verify who is the main element.


Advanced 2: The idea is that if there are k different numbers, it will be offset. It is necessary to use ingenious data structures to record candidates and make the following actions O (1):

1. Add a candidate/to a candidate number of occurrences +1

2. Is there a number in the candidates

3. Number of occurrences in candidates-1

4. Remove candidates

with a number of occurrences of 0

For 1, 22 operations, we can naturally think of using a hash table to complete. For the first 42 operations, we want to be able to have the least number of candidate information, but it is not the time complexity of O (1) If the heap is used. Note that each time you add a candidate, Count is 1, and each time you change the number of occurrences of a candidate, it only involves adding 1 operations. Therefore, if we can maintain the order of candidates, we can solve this problem easily. The method is to use LinkedList. Unlike ordinary linkedlist, we put all occurrences of the same candidate in a bucket, the candidate in the bucket is linked with the doubly Linked list, and the buckets are also doubly. Linked list link up. So for the +1 operation, we only need to find the corresponding candidate through the hash table, and move the candidate from the current bucket to the next bucket (the bucket with +1 occurrences). In addition, for all operations of the number 1, we record a global base, each 1 operation, then base+1. If the candidates in the first bucket in base and buckets occur the same number of occurrences, the first bucket is deleted entirely. Finally, we'll get the maximum k-1 candidates, iterate over the entire array, and use the hash of O (k) to record the number of occurrences of this k-1 candidates, and you can verify who the real main element is.


Interviewer Angle

Using the offset method of the topic, there are "number of orders" (nine chapters of the algorithm surface question 1), the same is the advanced problem. For the advanced problem, the key is to understand the idea of "offset", since the 2 number can be offset, then the number of 3 number k can also be offset. After offsetting, the remainder of the number, the main element must still exceed 1/3, 1/k. For the situation of 1/k (Advanced 2), in fact, in the interview generally will not ask, the solution is derived from a paper, so will not do the students do not have to worry too much. But the advanced 1 still needs to be mastered, because the advanced 1 indicates whether really understand the solution of the original problem, or just back the answer.



Nine-chapter algorithm surface question 37 main element

Contact Us

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.