[Daily Practice] - Josephus problem

Problem Description

0,1, n-1 which are arranged in a circle numbers n, starting from the number 0, delete the m-th digit each time from inside the circle. Find the circle where the last remaining digit.
The title comes from the leetcode, click to enter

Read title

For example, 0,1,2,3,4 five numbers in a circle, each deletion of three numbers starting from the number 0, the first four digits of the deleted sequence is 2,0,4,1, so the final the remaining number is 3.

A Solution

More intuitive solution

public int lastRemaining(int n, int m) {
        LinkedList<Integer> list = new LinkedList<>();
        // 全部加进去
        for (int i = 0; i < n; i++) {
            list.add(i);
        }
        // 只要链表长度不为 1,就不断循环
        while (list.size() != 1) {
            for (int i = 0; i < m; i++) {
            	// 取出第一个元素并删除
                Integer pre = list.pollFirst();
                if (i != m - 1) {
                	// 如果不是第m个,就加到末尾
                    list.add(pre);
                }
            }
        }
        return list.pollFirst();
}

Solution two

First, we set the n m-bit values continuously deleted, the resulting value of f(n,m)
the first digital deleted is (m-1)%ndenoted x
then the remaining n-1 becomes a digital (assumed here that n is greater than 2)

0 1 x-1 x+1 n-1

Because of the need from a recount after the number is deleted, we first into X + 1, the new numbers are arranged in the order

The original array Corresponding to the ordinal position
x+1 0
x+2 1
n-1 n-x-2
0 n-x-1
x-1 n-2

Why position 0 corresponds to nx-1?
The first step when we deleted a number that can be understood as this list is divided into two parts, the
length of the first part of x (because it is starting from 0),
then the length of the second part of it is n-x-1
now the first part of the mosaic behind the second portion,
then the first number of the first division, i.e. 0, becomes a n-x-1+1digital, i.e. numbers nx, because it is starting from zero, so that the corresponding n-x-1
key points:
Suppose the sequence corresponding to position a, the original value of B
then the relationship sequential positions a and B values, we can draw the formula:

B = (A+x+1) mod n

Can be obtained from the initial definition:
n-th digit. 1-m-th continuously remove the last value obtained f(n-1,m)
from the above equation we can be pushed back to its inverse value of (f(n-1,m)+x+1)%n
the value defined at the beginning is that we f(n-1,m)
will be x=(m-1)%nsubstituted into simplified available :

f(n,m)=f(n−1,m)+m) mod n,且f(1,m) = 0

Code

 public static int lastRemaining(int n, int m) {
        return n == 1 ? 0 : (lastRemaining(n-1,m) + m) % n;
    }

or

public int lastRemaining(int n, int m) {
    int flag = 0;   
    for (int i = 2; i <= n; i++) {
        flag = (flag + m) % i;
    }
    return flag;
}
Published 26 original articles · won praise 6 · views 2933

Guess you like

Origin blog.csdn.net/weixin_45676630/article/details/105211561