Cattle off network - to prove safety office- Child's Play (circle last remaining number)

Title: Children's Day each year, cow-off will prepare some small gifts to visit the orphanage children, this year is also true. As an experienced veteran off HF cattle, naturally we prepared some games. Among them, there is a game like this: First, let the children surrounded by a large circle. He then randomly assigned a number m, so that number of children 0 gettin number. Cried every time m-1 the children sing a song to be out of the line, then any of the gift box in choosing a gift, and not return to the circle, starting with his next child, continue 0 ... m-1 countin ... go on ... until the last remaining child, you can not perform, and get the cattle off the rare "Detective Conan" Collector's Edition (limited places oh! _ ). You want to try next, which the children will get the gifts it? (NOTE: The number of children is from 0 to n-1).
Ideas: to simulate a circular linked list using the list. Every time scanning to the end of the list when you want to move to the head of the list iterator.
However, this method requires a number m each deletion step, there are n numbers, the time complexity is O (mn). At the same time this method also requires a secondary analog loop chains, the spatial complexity is O (n).

class Solution {
public:
	int LastRemaining_Solution(int n, int m)
	{
        if(n<1||m<1) return -1;
		list<int> num;
		for (int i = 0; i < n; ++i)
			num.push_back(i);
		list<int>::iterator iter = num.begin();
		while (num.size()>1)
		{
			for (int i = 1; i < m; ++i)
			{
				iter++;
				if (iter == num.end())
					iter = num.begin();
			}
			list<int>::iterator next = ++iter;
			if (next == num.end())
				next = num.begin();
			--iter;
			num.erase(iter);
			iter = next;
				
		}
		return *iter;

	}
};

Solution II:
Thinking:
first assumed that there are n numbers, and the first number the deleted denoted k, the number of remaining deleted after the first number:
0,1,2,3, ...,. 1-K, k, k + 1, k + 2, ... n.
Because after deleting k, k is from a lower k + 1 re-counted, so we can build a relationship mapping, as follows:
Here Insert Picture Description
Every delete an element should build on these mappings again before basis. Based on the graph and can find an inverse mapping of the mapping, i.e. the mapped digital known, can be determined before a digital mapping relationship is: (x + k + 1) % n, (x digital mapped , n is the size of the array before mapping).
From the above theory: When the last remaining figures, this time the digital maps to 0, we need only step by step to solve the digital inverse mapping can know the value of the original array. That is, we can use the following recurrence relations, he said:
Here Insert Picture Description

class Solution {
public:
    int LastRemaining_Solution(int n, int m)
    {
        if(n<1||m<1)
            return -1;
        int last=0;
        for(int i =2;i<=n;++i)
            last=(last+m)%i;
        return last;
    }
};

Guess you like

Origin blog.csdn.net/qq_43387999/article/details/91352504