Child's Play (circle last remaining number) (Josephus problem)

Copyright Notice: Copyright: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/ysl_ysl123/article/details/90730577

Title Description

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 1

Circular list structure, in accordance with a topic description direct simulation game. The time complexity is O (nm), the spatial complexity is O (n).

Code (c ++)

class Solution {
public:
 struct Node{
        int val;
        Node* next;
    };
    int LastRemaining_Solution(int n, int m)
    {
        if(n<1||m<1) return -1;
        Node* head=new Node;
        head->val=0;
        head->next=NULL;
        Node* p=head;
        for(int i=1;i<n;i++){
            Node* node=new Node;
            node->val=i;
            node->next=NULL;
            p->next=node;
            p=p->next;
        }
        p->next=head;
        while(n!=1){
            for(int i=0;i<m-2;i++){
                head=head->next;
            }
            Node* dNode=head->next;
            head->next=dNode->next;
            delete dNode;
            head=head->next;
            n-=1;
        }
        return head->val;
    }
};

Ideas 2

Original blog link (Haitao's blog)
analysis of the law mathematically.
The first n digits (0,1 ..., n-1) is the last remaining numbers on the equation that is, f n and m (n, m).

In the n numbers, the first number to be deleted is the k = (m-1)% n, and the next counting number is k + 1, i.e., the next sequence from a k + 1, ..., n-1,0, ... k-1 to remove elements. The last remaining sequence number should also be a function of n and m. Because of this rule and the original sequence preceding the sequence is not the same (from the original sequence is a contiguous sequence of zero), and therefore referred to as the last remaining number f '(n-1, m). The last remaining numbers must be the same, so that f (n, m) = f '(n-1, m).

The next sequence of k + 1, ..., n- 1,0, ... k-1 as a mapping result of the mapping is formed from a series of 0 to n-2:
K + l-> 0
K + 2-> . 1
...
n--l-> NK-2
0 -> NK-. 1
...
K-l-> 2-n-
mapping is defined as p, then p (x) = (xk- 1)% n, i.e., if the mapping of the front number is x, then the number is mapped (xk-1)% n. Corresponding to the inverse mapping is P -1 (x) = (x + K +. 1) n-% (this case x is a number mapped).

Since the sequence and the original sequence is mapped after the same form, it is a continuous sequence starting from 0, and therefore still can be used to represent the function f, denoted by f (n-1, m) . According to our mapping rule, before the sequence number of the last remaining mapping F '(. 1-n-, m) = P -1 [F (. 1-n-, m)] = [F (n--. 1, m) + K +1]% n. The k = (m-1)% n obtained substituting f (n, m) = f '(n-1, m) = [f (n-1, m) + m]% n. Finally get a recursive formula.

That is, when n = 1, f (n, m) = 0; when n> 1, f (n, m) = [f (n-1, m) + m]% n.

The time complexity is O (n), the spatial complexity is O (1).

Code (c ++)

class Solution {
public:
    int LastRemaining_Solution(int n, int m)
    {
        if(n==0) return -1;
        if(n==1) return 0;
        return (LastRemaining_Solution(n-1,m)+m)%n;
    }
};

Guess you like

Origin blog.csdn.net/ysl_ysl123/article/details/90730577