【LeetCode 算法】Find the Losers of the Circular Game 找出转圈游戏输家

这篇具有很好参考价值的文章主要介绍了【LeetCode 算法】Find the Losers of the Circular Game 找出转圈游戏输家。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Find the Losers of the Circular Game 找出转圈游戏输家

问题描述:

n 个朋友在玩游戏。这些朋友坐成一个圈,按 顺时针方向1n 编号。从第 i 个朋友的位置开始顺时针移动 1 步会到达第 (i + 1) 个朋友的位置(1 <= i < n),而从第 n 个朋友的位置开始顺时针移动 1 步会回到第 1 个朋友的位置。

游戏规则如下:

1 个朋友接球。

  • 接着,第 1 个朋友将球传给距离他顺时针方向 k 步的朋友。
  • 然后,接球的朋友应该把球传给距离他顺时针方向 2 * k 步的朋友。
  • 接着,接球的朋友应该把球传给距离他顺时针方向 3 * k 步的朋友,以此类推。

换句话说,在第 i 轮中持有球的那位朋友需要将球传递给距离他顺时针方向 i * k 步的朋友。

当某个朋友第 2 次接到球时,游戏结束。

在整场游戏中没有接到过球的朋友是 输家

给你参与游戏的朋友数量 n 和一个整数 k ,请按升序排列返回包含所有输家编号的数组 answer 作为答案。

1 < = k < = n < = 50 1 <= k <= n <= 50 1<=k<=n<=50

分析

这个问题是一次周赛的Q1。

这样的问题很讨厌,因为它太简单了,直接模拟就可以了,但是大部分的人潜意识中,会被导向约瑟夫环

没错,当时这个问题花了近半小时,虽然AC,但是代码太挫了。

可能是周赛的影响,今天的每日就很流畅。这个模拟需要注意的是它的No.是从1开始到n。而且是一个环,所以取模是必然的。

这时候就需要一个长度n的标识数组,来记录是否访问过。
所以编号为 i i i的人,就会处于数组 i − 1 i-1 i1的位置上。
那么下一个可以被访问的人, j = ( i + r ∗ k ) j = (i+r*k)%n j=(i+rk), i i i j j j都是索引
在不停的访问中,一定会终止,即某个位置第二次被访问到。
此时把所有未被标记的位置从小到大的记录到结果中.

目前这个问题似乎,没有纯数学的思路,只能模拟

代码

模拟

public int[] circularGameLosers(int n, int k) {
        boolean[] mark = new boolean[n];
        int r = 0;//圈数
        for(int i = 0;!mark[i];i =(i+r*k)%n){
            mark[i] = true;
            r++;
        }
        int[] ans = new int[n-r];
        for(int i=0,j=0;i<n;i++){
            if(!mark[i]) ans[j++]= i+1;
        }
        return ans;
    }

时间复杂度 O ( N ) O(N ) O(N)

空间复杂度 O ( N ) O(N) O(N)

Tag

Array

Hash文章来源地址https://www.toymoban.com/news/detail-651774.html

到了这里,关于【LeetCode 算法】Find the Losers of the Circular Game 找出转圈游戏输家的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • LeetCode --- 1732. Find the Highest Altitude 解题报告

    There is a biker going on a road trip. The road trip consists of  n + 1  points at different altitudes. The biker starts his trip on point  0  with altitude equal  0 . You are given an integer array  gain  of length  n  where  gain[i]  is the  net gain in altitude  between points  i ​​​​​​ and  i + 1  for all ( 0 = i n) . Return 

    2024年02月02日
    浏览(12)
  • LeetCode 1385. Find the Distance Value Between Two Arrays

    Given two integer arrays  arr1  and  arr2 , and the integer  d ,  return the distance value between the two arrays . The distance value is defined as the number of elements  arr1[i]  such that there is not any element  arr2[j]  where  |arr1[i]-arr2[j]| = d . Example 1: Example 2: Example 3: Constraints: 1 = arr1.length, arr2.length = 500 -1000 = ar

    2024年02月10日
    浏览(9)
  • leetcode - 2616. Minimize the Maximum Difference of Pairs

    You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs. Note that for a pair of elements at the index i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents th

    2024年02月13日
    浏览(9)
  • 【问题解决】 Could not find a package configuration file provided by “OpenCV“ with any of the following n

    【问题解决】 Could not find a package configuration file provided by “OpenCV“ with any of the following n

    编译依赖于opencv的包时报错 Could not find a package configuration file provided by “OpenCV” with any of the following names: OpenCVConfig.cmake opencv-config.cmake 这个问题是找不到 “OpenCVConfig.cmake” 或 “opencv-config.cmake” 文件,主要是找不到 OpenCV 路径而导致的。 造成这个问题的主要原因就是没有安

    2024年03月18日
    浏览(11)
  • LeetCode //C - 2095. Delete the Middle Node of a Linked List

    LeetCode //C - 2095. Delete the Middle Node of a Linked List

    You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list. The middle node of a linked list of size n is the [ n / 2 ] t h [n / 2]^{th} [ n /2 ] t h node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x. For n = 1, 2, 3, 4, and 5, the middle nodes a

    2024年02月02日
    浏览(25)
  • LeetCode 2490. Circular Sentence【字符串】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月12日
    浏览(6)
  • LeetCode //C - 918. Maximum Sum Circular Subarray

    Given a circular integer array nums of length n, return the maximum possible sum of a non-empty subarray of nums. A circular array means the end of the array connects to the beginning of the array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n]. A subarray may only include each element

    2024年02月07日
    浏览(10)
  • LeetCode 918. Maximum Sum Circular Subarray【数组,动态规划】中等

    LeetCode 918. Maximum Sum Circular Subarray【数组,动态规划】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月15日
    浏览(10)
  • Starting the Docker Engine 一直转圈

    参考 Docker永远在“docker desktop starting .”Settings 一直在转圈 从上述方案中总结,注销码头工人桌面,是最推荐的修复方式。 1、关闭 Docker  2、在命令终端中使用。右键选择命令终端-点击“以管理员身份运行” 执行以下两行命令 3、在此之后退出命令终端。然后,重新启动

    2024年02月04日
    浏览(12)
  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

    Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 1. 解题思路 2. 代码实现 题目链接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 这一题我的思路上就是一个二分的思路,先确定一个上下界,然后不断通过二分来找到最大的price不超过k的值。 因此,剩下的

    2024年01月20日
    浏览(10)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包