Water Stick King Problem

Source: Internet
Author: User

Water Stick King Problem

Personal information: enrolled in the University of Yan undergraduate software project Major in the current three;

I blog: Google search "cqs_2012" can be;

Personal Hobbies: Love data structure and algorithms, hope to work in the future algorithm to make their own contribution to the people;

Blog content: Water paste King problem

Blog time: 2014-5-7;

Programming language: Java;

Programming Bad Environment: Windows 7 Professional Edition x64;

Programming tools: Jdk,eclipse x64;

Cartographic tools: Office ppt;

Hardware information: 7g-3 notebook;


Mantra

Each person is similar to a computer, both higher than the computer, and less than the computer, to avoid weaknesses, with the times.

Topic

Water Paste King Problem (selected from the beauty of programming)

One person posts more than half of the total number of posts, trying to find out who.

Ideas

Mathematical modeling: There are a bunch of people posting, each person posting a ID identification, and then find out the half of the post ID. (if there is already more than half of this post ID)

Each is identified by an int whose ID is like an element of an array, trying to find the number of more than half the ID.

Method: Offset method

Since there is an ID number of more than half, then it is the most, but it can be used to offset the other ID number, one-to-one to cancel, the last of the most will remain a lot, this time we know the answer

Personal algorithm design such as the following (time complexity O (n), n array length)

static int _find_more_than_half (int[] data) {int result = data[0]  ; int number = 1;for (int i = 1;i < data.length;i++) {if (result = = Data[i]) number++;else{number--;if (number = = 0) {result = data [i];}}} return result;}

This algorithm has a flaw, that is, if there is no more than half the ID of the post, it will find the wrong answer; Assuming that there is a half-post ID, it will give the correct answer.

For eg: arrays

int[] Data ={ 1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 , 2,4,4,44,4};

It will give 2 (right now)

For eg: arrays

int[] Data ={1,1,2,2};

He will give 2 (which is wrong at this time)

So we need to strictly check whether the given data conforms to the problem model and then make the choice.

This time the problem has turned into an element that checks whether an array has more than half the number.

The answer is given in the next article, thank you


Water Stick King Problem

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.