class Solution:
def findTheWinner(self, n: int, k: int) -> int:
res = 0
for i in range(2, n + 1):
res = (res + k) % i
return res + 1
class Solution {
public:
int findTheWinner(int n, int k) {
int res = 0;
for (int i = 2; i <= n; ++i) {
res = (res + k) % i;
}
return res + 1;
}
};
class Solution {
public int findTheWinner(int n, int k) {
int res = 0;
for (int i = 2; i <= n; i++) {
res = (res + k) % i;
}
return res + 1;
}
}
var findTheWinner = function(n, k) {
let res = 0;
for (let i = 2; i <= n; i++) {
res = (res + k) % i;
}
return res + 1;
};
You are given two integers, n
and k
. There are n
friends sitting in a circle, numbered from 1 to n
in clockwise order. The game proceeds as follows:
k
people clockwise (including the current person).k
-th person is removed from the circle and the count resumes from the next person.Return the number of the person who is the last remaining (the winner).
Constraints:
k
≤ n
≤ 500
At first glance, this problem resembles the classic Josephus problem, where people are eliminated in a circle at fixed intervals. The naive approach would be to simulate the process: maintain a list of people, repeatedly remove every k
-th person, and continue until one remains.
However, simulating the entire process can be inefficient for larger n
, as removing elements from a list or array repeatedly can be costly. This leads us to consider if there's a mathematical or recursive way to find the winner's position without simulating every step.
The key insight is that the problem has a recursive structure: the winner of n
people with step k
can be determined from the winner of n-1
people, adjusted for the new circle size. This recursive relationship is the heart of the Josephus problem and enables an efficient solution.
To solve the problem efficiently, we use the recursive formula for the Josephus problem:
f(n, k)
be the zero-based index of the winner in a circle of size n
with step size k
.f(1, k) = 0
(with only one person, they are the winner).n > 1
, f(n, k) = (f(n-1, k) + k) % n
.This formula works by:
n-1
people.k
(the step size), and taking modulo n
to wrap around the circle.
To implement this efficiently, we can use an iterative approach, starting with res = 0
for one person and incrementally building up to n
, each time updating res
as (res + k) % i
for i
from 2 to n
.
Finally, since the problem uses 1-based numbering, we return res + 1
as the winner.
Let's walk through an example with n = 5
and k = 2
:
Using the iterative formula:
Final answer: res + 1 = 2 + 1 = 3 (which matches our manual simulation).
Brute-force approach:
The optimized approach is much more efficient and suitable for large values of n
.
The "Find the Winner of the Circular Game" problem is a variant of the Josephus problem. By recognizing its recursive structure, we can avoid brute-force simulation and instead use a simple iterative formula to compute the winner in O(n) time and O(1) space. The key insight is mapping the problem to its mathematical recurrence, which leads to an elegant and efficient solution.