Leetcode-cycle Detection,find the Duplicate number

Source: Internet
Author: User

Originally thought this question is very simple, but looks the request, first cannot change this array, the second can use only O (1) space, the third time complexity is less than O (n^2), cannot use the way which iterates the array to solve. There are two ways, one is to use binary Search, and the other is to use the Floyd cycle detection algorithm. Binary Search Method

This method was based on a theroy called Pigeonhole Princinpal.

In Mathematics,pigeonhole princinpal states that if n items is put into a m container,and n>m,then at least one Contai NER must contain more than one item.

Thinking 1:1. At First,the search space are 1~n,we find out the the middle of 1~n as mid and figure out what many numbers in the Array is equals or less than mid. We define it as count.

2.If count is equals or less than mid,then the search space shrink to mid+1~n.

3.If count is bigger than mid,then the search space shrink to 1~mid.

e.g. Given an array,[1,2,3,4,4],n=4

    mid=1+ (4-1)/2=2, then we compare all the numbers in the array and get count=2.

There is only numbers between 1~2,and count=2. It means that there is 2 spaces for (or both).

Think that just because there is 2 spaces for 1~2,it doesn ' t mean there is no duplicate numbers between 1~2,it C An IS {2,2} or {count=2} when.

But think is about the rest of the array.

There is numbers between (a), so there is 3 spaces left and the rest of the numbers is between (--).

There is only numbers between-there is 3 space for them,so according to the pigeonhole Princinpal, there mus t be a duplicate number between (--).

And we can know from the question that there are only one duplicate number in the array. So the duplicate one must is between (-).

Then we keep on searching. mid=3+ (4-3)/2=3, then we can conclude that count=3. There is 3 numbers between and there are 2 numbers between (4~4). We may conclude, the duplicate number is 4.

public static int findduplicate (int[] nums) {int low=1;int high=nums.length-1;int count=0;while (low

Cycle Detection algorithm by Floyd how it works

Let ' s has a tortoise and a hare (the pointers) pointing to the beginning of the list.

First part of the algorithm

They start to move and the tortoise move a, step while the hare move, and steps each time.

Then they meet at the some meeting point.

The figure illustrates a cycle and M. States the step from the start to the cycle.

The cycle length is n and the tortoise and the Hare first meet at K-steps away from the cycle start.

The tortoise Move I step,and i=m+p*n+k (assume, the tortoise move around the cycle for P times).

The Hare move 2i Step,and 2i=m+q*n+k (assume, the hare move around the cycle for Q times).

I=2i-i= (q-p) *n so m+k= (q-2p) n

We can conclude this m+k is some multiples of the length of the cycle.

Second part of the algorithm

Let's move the tortoise to the start of the list and keep the hare at the meeting point.

Now we start to move them and they both move a step at a time.

The hypothesis is so if we move them at the same speed,then they would finally meet at the beginning of the cycle.

If we move them m+k Steps,then The tortoise'll be in the previous meeting point as we can see from the picture.

What's the hare? Because M+k is some multiples of n,then, the hare also move several cycle length and get to the meeting point.

What if we move them m steps?

The tortoise'll is at the beginningg of the Cycle,and the hare would go is k steps short of completing (q-2p) rotations.

Ince It started K steps in front of the cycle beginning, Hare would has to arrive at the cycle beginning.

Obviously M<m+k,so when they first Meet,they would get to the beginning of the cycle and we get the result.

public static int findduplicate (int[] nums) {int fast=0;int slow=0;//algorithm first part do {fast=nums[nums[fast]];//fast Go two steps at a time slow= Nums[slow];//slow one step at a time}while (slow!=fast);//eventually they meet//the second part of the algorithm int Finder=0;//finder set to the starting point while (finder!=fast) {// When they meet, they are at the point where cycle begins finder=nums[finder];fast=nums[fast];} return fast;}

  

Leetcode-cycle Detection,find the Duplicate number

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.