Josephus Problem

flashcards

LCOF 62. Last Number Left in the Cicle

约瑟夫环问题代码
?

class Solution:
  def lastRemaining(self, n: int, m: int) -> int:
    ans = 0
    for i in range(2, n + 1):
      # k = m % i - 1
      # ans = (ans + k + 1) % i
      ans = (ans + m) % i
    return ans


Last update : May 28, 2023
Created : May 28, 2023