返回介绍

solution / 1800-1899 / 1823.Find the Winner of the Circular Game / README_EN

发布于 2024-06-17 01:03:14 字数 4697 浏览 0 评论 0 收藏 0

1823. Find the Winner of the Circular Game

中文文档

Description

There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings you to the 1st friend.

The rules of the game are as follows:

  1. Start at the 1st friend.
  2. Count the next k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once.
  3. The last friend you counted leaves the circle and loses the game.
  4. If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat.
  5. Else, the last friend in the circle wins the game.

Given the number of friends, n, and an integer k, return _the winner of the game_.

 

Example 1:

Input: n = 5, k = 2
Output: 3
Explanation: Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.

Example 2:

Input: n = 6, k = 5
Output: 1
Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.

 

Constraints:

  • 1 <= k <= n <= 500

 

Follow up:

Could you solve this problem in linear time with constant space?

Solutions

Solution 1

class Solution:
  def findTheWinner(self, n: int, k: int) -> int:
    if n == 1:
      return 1
    ans = (k + self.findTheWinner(n - 1, k)) % n
    return n if ans == 0 else ans
class Solution {
  public int findTheWinner(int n, int k) {
    if (n == 1) {
      return 1;
    }
    int ans = (findTheWinner(n - 1, k) + k) % n;
    return ans == 0 ? n : ans;
  }
}
class Solution {
public:
  int findTheWinner(int n, int k) {
    if (n == 1) return 1;
    int ans = (findTheWinner(n - 1, k) + k) % n;
    return ans == 0 ? n : ans;
  }
};
func findTheWinner(n int, k int) int {
  if n == 1 {
    return 1
  }
  ans := (findTheWinner(n-1, k) + k) % n
  if ans == 0 {
    return n
  }
  return ans
}
class LinkNode {
  public val: number;
  public next: LinkNode;

  constructor(val: number = 0, next?: LinkNode) {
    this.val = val;
    this.next = next;
  }
}

function findTheWinner(n: number, k: number): number {
  if (k === 1) {
    return n;
  }
  const dummy = new LinkNode(0);
  let cur = dummy;
  for (let i = 1; i <= n; i++) {
    cur.next = new LinkNode(i);
    cur = cur.next;
  }
  cur.next = dummy.next;

  cur = dummy;
  let count = 0;
  while (cur.next != cur) {
    count++;
    if (count === k) {
      cur.next = cur.next.next;
      count = 0;
    } else {
      cur = cur.next;
    }
  }
  return cur.val;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文