[Pre-sleep inspiration and Divergent thinking] by a simple array comparison problem thought of

Source: Internet
Author: User

Objective

It is said that an excellent program ape often have such experience, the day encountered a brain-racking problem can not be solved, night after sleep, in the middle of the dream will be inspired, immediately get up, open the computer, one go. The next day if you don't read the notes, you have no idea that you have found such ingenious solutions.

Lying in bed last night, accidentally thought of a problem, suddenly inspired by the idea of a clever solution, almost want to get up and write down, just the bedroom has been powered down, nothing. When I got up in the morning and didn't wash my face, I knocked the article first.

Problem

A very simple question, the length is 100 and 101 ab two arrays, the value range is 0-99, contains duplicates, find out to come out of a number.

Ideas

The first thought was to sort it out, and then a trip to find out the difference. Sort by the beginning of the natural thinking of the standard fast row, for numbers, there are cardinality and counting.

The thought of counting, suddenly found that there is no need to sort, three-time traversal can be solved.

    1. Iterate through a array, counting with the array count;
    2. Traverse B Array, count[b[i]]--;
    3. Traverse count, the value is 1 of the subscript is the request;
Extrapolate

Then the thought is divergent, further thinking

    • Length of 100 and 100+n, find out the number of n;
    • Find the same n, that is, a subset;
    • For three arrays, a subset (subscript with a count array value of 3 is);
    • For two arrays, find out the similarities and differences between the elements
Lift Three evils N
    • For n arrays, ask for a subset (count is N)
    • For n arrays, the similarities and differences of the elements are obtained
Lifting N anti-n^n

If the topic does not give a range, or is not a number, the amount of big data to do,

    • An array of two elements, for example, to find one of the different elements

If the element value is unique, hash, the second traversal, the hash conflict is the request

Conclusion

。。。

The result was two or three O ' night in the morning before falling asleep, the conclusion is no longer think, divergent thinking is too terrible t^t.

Program Ape easy to brain over, night or good rest, do not think too much, of course, the dream of sudden inspiration or to immediately climb up to write down, after all, sometimes 1% of the inspiration is more important ...

[Pre-sleep inspiration and Divergent thinking] by a simple array comparison problem thought of

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.