The sword refers to the number in the offer (29) array that appears more than half or 1/3 or 1/N.

Source: Internet
Author: User

Question: a number in the array appears more than half the length of the array. Please find this number.

Method 1: If this number is sorted, the number located in the middle of the array must be a number that appears more than half the length of the array.

This number is the median in statistics, that is, the number n/2 in the array with the length of N.

Obtain any k-th number in the array. This problem involves the O (n) solution. Note: here, the kth element counts from 1, and the repeating element does not go heavy.

(1) Sort directly by sort, and then locate the element whose index is kth-1

int FindKth1(std::vector<int>& num, int kth){    if (kth > num.size()) {        throw std::invalid_argument("invalid_argument");    }    std::sort(num.begin(), num.end(), std::greater<int>());    return num.at(kth - 1);}

 

(2) directly use STD: nth_element. This function positions the elements referred to by the second parameter to the final position after sorting, and its left side is smaller than this value, and its right side is greater than this value.

Note that the index of the kth element is num. Begin () + kth-1.

int FindKth2(std::vector<int>& num, int kth){    if (kth > num.size()) {        throw std::invalid_argument("invalid_argument");    }    std::nth_element(num.begin(), num.begin() + kth - 1, num.end(), std::greater<int>());    return num.at(kth - 1);}

 

(3) directly use STD: partial_sort. This function sorts the [first, mid) intervals without any guarantee. Note that the Left-closed and right-open intervals

int FindKth3(std::vector<int>& num, int kth){    if (kth > num.size()) {        throw std::invalid_argument("invalid_argument");    }    std::partial_sort(num.begin(), num.begin() + kth, num.end(), std::greater<int>());    return num.at(kth - 1);}

 

 

Method 2: based on the number of Val occurrences that exceeds half the length of the array, we delete two different numbers each time (whether or not we include Val). In the remaining array, the number of occurrences of the Val value is still half the length of the remaining array.

Saves two values when traversing the array: one is a number in the array and the other is the number of times.

When traversing to the next number, if the next number is the same as the previously saved number, the number of times is increased by 1.

If the next number is different from the previously saved number, the number of times is 1. If the number is reduced to 0, you need to save the next number and set the number to 1.

Int findmorehalf (const STD: vector <int> & num) {If (Num. size () = 0) {Throw STD: invalid_argument ("invalid_argument");} int result = num. at (0); int COUNT = 1; for (INT I = 1; I <num. size (); ++ I) {If (Count = 0) {Result = num. at (I); Count = 1;} else {If (Num. at (I) = Result) {++ count;} else {-- count ;}} int resnum = STD: Count (Num. begin (), num. end (), result); // check if (resnum * 2> N) {return result;} else {Throw STD: invalid_argument ("invalid_argument ");}}

 

Expansion problems:

What if the number of times in the array exceeds the length of 1/K?

First, there can be a maximum of k-1 Numbers that appear more than 1/K in the array length.

Method 1: Use unordered_map of C ++ 11 to create a hash <key, count>, in the second traversal, the number t (n) = O (n) whose length exceeds 1/K (n is the array length)

Method 2: Use unordered_map of C ++ 11 to create a K-length hash <key, count> and traverse the original array.

If the element is already in the hash, add the corresponding count to 1.

If the element is not in the hash, insert it into the hash and set COUNT = 1. If the length of map is K after the insert operation, the Count of each key is reduced by 1, and delete the key with Count = 0.

Time Complexity T (n) = O (N), but space complexity decreases.

 

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.