[LeetCode-Sword Finger Offer] 62. The last remaining number in the circle

1. Topic

The n numbers of 0,1,n-1 are arranged in a circle, starting from the number 0, and deleting the mth number from this circle each time. Find the last number remaining in the circle.

For example, the 5 numbers 0, 1, 2, 3, and 4 form a circle. Each time the third number is deleted from the number 0, the first 4 numbers to be deleted are 2, 0, 4, and 1, so at the end The remaining number is 3.

Example 1:

输入: n = 5, m = 3
输出: 3

Example 2:

输入: n = 10, m = 17
输出: 2

limit:

  • 1 <= n <= 10^5
  • 1 <= m <= 10^6

Two, solve

1. Violent simulation (barely passed)

Ideas:

The current delete position idx, the next delete position is idx + m.
However, since the current position number is deleted, the following number will move forward by one, so the actual next position is idx + m - 1. At the end of the number, it will count from the beginning, and finally take the modulus, that's it (idx + m - 1) % n.

Attached: ArrayList.remove() code
1


  • LinkedListThe remove()time complexity of LinkedList.remove() VS ArrayList.remove() is O (n) O(n)O ( n ) , same as ArrayList. ButArrayListthe memory space of continuous copies, compared toLinkedLista large number of non-contiguous address access,ArrayListperformance is OK.

Code:

class Solution {
    
    
    public int lastRemaining(int n, int m) {
    
    
        ArrayList<Integer> list = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
    
    
            list.add(i);
        }
        int idx = 0;
        while (n > 1) {
    
    
            idx = (idx + m - 1) % n;
            list.remove(idx);
            n--;
        }
        return list.get(0);
    }
}

Time complexity: O (n 2) O(n^2)O ( n2 )
Space complexity: O (n) O(n)O ( n )

2. Mathematical solution

Ideas:

1. This problem is a famous Joseph ring problem with mathematical solutions.

Full text key reminder: Only care about the change of the serial number of the person who is finally alive .

2. Question: Give each person a number (index value), and replace each person with letters .
The following is N=8,m=3an example: F(n, m)expressed as the index number of the last remaining person, so only care about the change of the index number of the last remaining person:
1
starting from 8 people, kill one person at a time (position is index), Remove the person who was killed (index), then start with the first person after that person (index+1), and renumber it.

  • The first time C was killed, the number of people became 7, and D was the beginning (the number of G that survived was changed from 6 to 3)
  • The second time F was killed, the number of people became 6, and G was the beginning (the number of G that survived eventually changed from 3 to 0)
  • The third time A was killed, the number of people became 5, and B was the beginning (the number of G that survived eventually changed from 0 to 3)
  • By analogy, when there is only one person left, his number must be 0! (Emphasis)

3. The number of the person who is finally alive is reversed

Knowing the change process of G's index number, then reverse the process from N=7 to N=8.
1
From the above, we can get: f (8, 3) = [f (7, 3) + 3]% 8 f(8, 3) = [f(7, 3)+3] \% 8f(8,3)=[f(7,3)+3 ] % 8
generalization, that is,f (n, m) = [f (n − 1, m) + m]% mf(n, m) = [f(n-1, m) + m] \% mf(n,m)=[f(n1,m)+m]%m

4. Derivation of recursive formula

f ( n , m ) = { 0  n=0 [ f ( n − 1 , m ) + m ] % n  n>1 f(n, m)=\left\{ \begin{aligned} 0 && \text{ n=0}\\ [f(n-1, m) + m]\%n && \text{ n>1} \end{aligned} \right. f(n,m)={ 0[f(n1,m)+m]%n n=0 n>1

A more logical derivation can be found in 7-28 Monkey King (20 points) , and a very strict mathematical derivation can be found in the proof of the recurrence formula for the Joseph ring problem .

Code:

class Solution {
    
    
    public int lastRemaining(int n, int m) {
    
    
        int ans = 0;
        // 最后一轮剩下2个人,所以从2开始反推
        for (int i = 2; i <= n; i++) {
    
    
            ans = (ans + m) % i;
        }
        return ans;
    }
}

Time complexity: O (n) O(n)O ( n )
space complexity: O (1) O(1)O ( 1 )

Three, reference

1. Java solves the Joseph ring problem and tells you why the simulation will time out!
2. Solve the Joseph ring with an example from another perspective

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/110951459