Find a water guest

Source: Internet
Author: User

First, the topic requirements

A three-person line designed a forum for irrigation. Information college students like to exchange irrigation above, legend in the forum there is a "water king", he not only likes to post, but also reply to other ID issued by each post.

The "Water King" has been rumored to have posted more than half the number of posts.
If you have a list of posts (including replies) for the current forum, and the ID of the author of the Post is in it, can you quickly find the legendary water king?

Achieve time complexity O (n)

Second, design ideas

Because to achieve the time complexity O (n), so the request after reading a pass, you can find the water king, so first of all to consider the adjacent to the collar is not at the same time to eliminate, but also exist, not the water King of the continuous situation

In this case, you need to return to the previous

Third, the experimental code

Package Com.minirisoft;public class Zhaoshuiwang {public static void main (string[] args) {    int m=0;int a[]={2,2,4,4,4 , 3,2,2,2,2};for (int i=0;i<9;i++) {if (a[i]>0) {   if (a[i]!=a[i+1])   {   a[i]=a[i+1]=-1;   i=0;}}}   for (int j=0;j<9;j++) {if (a[j]>0) {m=a[j];}} System.out.println ("Water Guest for" +m);}}

Four, the experiment

Five, experimental experience

Sometimes we should consider the complexity of time, because the complexity of time is important when it comes to dealing with big data.

Find a water guest

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.