当前位置:网站首页>Simulation volume leetcode [general] 1823 Find the winner of the game
Simulation volume leetcode [general] 1823 Find the winner of the game
2022-07-18 14:01:00 【Encounter simulation volume】
1823. Find the winner of the game
share n We play games together . All the guys in a circle , Press Clockwise order from 1 To n Number . To be precise , From i If you move one person clockwise, you will arrive at the (i+1) The location of famous friends , among 1 <= i < n , From n If you move one person clockwise, you will go back to the first 1 The location of famous friends .
The game follows the following rules :
From 1 Where are the famous friends Start .
Count clockwise k A little friend , Counting requires contain The little friend at the beginning . Count in circles one by one , Some buddies may be counted more than once .
The last one you count needs to leave the circle , And it's like losing the game .
If there is still more than one partner in the circle , From the little friend who just lost Next clockwise buddy Start , Back to step 2 Carry on .
otherwise , The last one in the circle wins the game .
Give you the total number of players in the game n , And an integer k , Back to the winner of the game .
Example 1:
Input :n = 5, k = 2
Output :3
explain : The operation steps of the game are as follows :
- Little friends 1 Start .
- Number of punctures in time 2 A little friend , That's my little friend 1 and 2 .
- buddy 2 Get out of the loop . Next time I'm a kid 3 Start .
- Number of punctures in time 2 A little friend , That's my little friend 3 and 4 .
- buddy 4 Get out of the loop . Next time I'm a kid 5 Start .
- Number of punctures in time 2 A little friend , That's my little friend 5 and 1 .
- buddy 1 Get out of the loop . Next time I'm a kid 3 Start .
- Number of punctures in time 2 A little friend , That's my little friend 3 and 5 .
- buddy 5 Get out of the loop . All that's left is little friends 3 . So my little friend 3 It's the winner of the game .
Example 2:
Input :n = 6, k = 5
Output :1
explain : The order in which little friends leave the circle :5、4、6、2、3 . buddy 1 It's the winner of the game .
Tips :
1 <= k <= n <= 500
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/find-the-winner-of-the-circular-game
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
class Solution:
def __init__(self):
""" Think in reverse , Each interval k Insert a person in position , His number is actually the distance from the first person """
pass
def findTheWinner(self, n: int, k: int) -> int:
next = 1
for time in range(1,n+1):
next = (next + k -1) % time +1
return next
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list turn node
return s.findTheWinner(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[5,2],
# [10,3],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- The writing method of like% variable% in Oracle SQL
- 阅读真题 | 真题阅读 做题记录 二
- Base64 encoding and decoding principle and C language implementation
- Basic knowledge of triode (Part 2) ②
- Three lines (spring daily question 59)
- Sword finger offer19 regular expression matching string dynamic programming
- Common and practical SQL statements
- CI(持续集成)、CD(持续部署)
- 2018 Jiangsu Provincial Information and future programming expert competition test question -- (New) chicken and rabbit in the same cage standard schedule
- Iptables mask access to a port of IP
猜你喜欢

Interview problem: how to close an order without using a scheduled task?

职场必备 | 123页华为内部项目管理PPT

用过useEffect,useLayoutEffect吗
![[brand special session] breakthrough across x, new opportunities for audio and video cohesion](/img/4f/632ce4b2cd49b9a3f25a0d63f78d3c.jpg)
[brand special session] breakthrough across x, new opportunities for audio and video cohesion

【MySQL】——数据库的基本查询练习

kettle版本8.2的中文乱码问题

騰訊員工發帖找對象,錶示偏愛程序員!評論火了......丨黑馬頭條

视频处理及编解码硬件系统优化设计

【Jmeter】Jmeter 设置默认语言为中文

MySQL - ER model
随机推荐
CI(持续集成)、CD(持续部署)
phpmyadmin 4.8.1远程文件包含漏洞之[GWCTF 2019]我有一个数据库
记一篇IT培训日记068-心里有点不平衡
T100接口开发步骤简介
英语 | 阅读的逻辑 解题笔记
Function overloading
SQL must know must know challenge answer
Binary tree, traversal
SSH based online mall
web一些实用的网址
Differences between collections and collections
Les employés de Tencent postent pour trouver des objets, ce qui indique une préférence pour les programmeurs! Les commentaires sont en feu... 丨 Black Horse Headlines
Ubuntu 18.04 install mysql5.7.35 with tar package
Quarkus practice 2: develop rest ful services to realize simple curd functions
Wechat applet training | Chinese dictation tool based on cloud database
npm与node版本不匹配 更新了npm 结果报错node和npm不匹配 怎么退回之前的npm 版本?或者说怎么查看node适配的npm版本
音频焦点仲裁策略
Can ping command still play like this?
视频处理及编解码硬件系统优化设计
二叉树的广度遍历