aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-07-08 00:39:05 -0600
committerJack Sangdahl <[email protected]>2024-07-08 00:39:05 -0600
commit9fe7844d8621cd8cc30b5269e9236e3ac51a1542 (patch)
tree8d91f107d32c4d32193d8af21cf1bbba0617db10
parente7289f0016e3004f76759546dbabf1387fa70e65 (diff)
downloadleetcode-9fe7844d8621cd8cc30b5269e9236e3ac51a1542.tar.gz
leetcode-9fe7844d8621cd8cc30b5269e9236e3ac51a1542.zip
1823. find the winner of the circular game
-rw-r--r--find_winner_circular_game.c23
-rw-r--r--find_winner_circular_game.py29
2 files changed, 52 insertions, 0 deletions
diff --git a/find_winner_circular_game.c b/find_winner_circular_game.c
new file mode 100644
index 0000000..a659fc3
--- /dev/null
+++ b/find_winner_circular_game.c
@@ -0,0 +1,23 @@
+// 1823. Find the Winner of the Circular Game
+#include "leetcode.h"
+
+/*
+ * there are 'n' friends that are playing a game. the friends are sitting in a
+ * circule and are numbered from 1 to 'n' in clockwise order. more formally,
+ * moving clockwise from the i'th friend brings you to the '(i + 1)'th friend
+ * for '1 <= i < n', and moving clockwise from the n'th friend brings you to the
+ * 1st friend. given the number of friends, 'n' and an integer 'k', return the
+ * winner of the game.
+ */
+
+int findTheWinner(int n, int k) {
+ int ans = 0;
+ for (int i = 1; i <= n; i++)
+ ans = (ans + k) % i;
+ return ans + 1;
+}
+
+int main() {
+ printf("%d\n", findTheWinner(5, 2)); // expect: 3
+ printf("%d\n", findTheWinner(6, 5)); // expect: 1
+}
diff --git a/find_winner_circular_game.py b/find_winner_circular_game.py
new file mode 100644
index 0000000..9e6a621
--- /dev/null
+++ b/find_winner_circular_game.py
@@ -0,0 +1,29 @@
+# 1823. Find the Winner of the Circular Game
+
+"""
+there are 'n' friends that are playing a game. the friends are sitting in a
+circule and are numbered from 1 to 'n' in clockwise order. more formally,
+moving clockwise from the i'th friend brings you to the '(i + 1)'th friend
+for '1 <= i < n', and moving clockwise from the n'th friend brings you to the
+1st friend. given the number of friends, 'n' and an integer 'k', return the
+winner of the game.
+"""
+
+
+class Solution(object):
+ def findTheWinner(self, n, k):
+ """
+ :type n: int
+ :type k: int
+ :rtype: int
+ """
+ ans = 0
+ for i in range(1, n + 1):
+ ans = (ans + k) % i
+ return ans + 1
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.findTheWinner(5, 2))
+ print(obj.findTheWinner(6, 5))