LeetCode refers to Offer 62. The last remaining number in the circle

Joseph ring problem.
Idea: Kill them one by one, and then make up for the restoring position in the reverse direction.
Reference explanation

int lastRemaining(int n, int m){
    
    
    int pos = 0;
    for(int i = 2; i <= n; i++){
    
    
        pos = (pos + m) % i;
    }
    return pos;
}

Guess you like

Origin blog.csdn.net/qq_43078427/article/details/110748473