首页 > 其他分享 >[LeetCode] 2582. Pass the Pillow

[LeetCode] 2582. Pass the Pillow

时间:2023-09-26 13:55:57浏览次数:39  
标签:index int 枕头 Pass LeetCode person time pillow Pillow

There are n people standing in a line labeled from 1 to n. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.

  • For example, once the pillow reaches the nth person they pass it to the n - 1th person, then to the n - 2th person and so on.

Given the two positive integers n and time, return the index of the person holding the pillow after time seconds.

Example 1:

Input: n = 4, time = 5
Output: 2
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
Afer five seconds, the pillow is given to the 2nd person.

Example 2:

Input: n = 3, time = 2
Output: 3
Explanation: People pass the pillow in the following way: 1 -> 2 -> 3.
Afer two seconds, the pillow is given to the 3rd person.

Constraints:

  • 2 <= n <= 1000
  • 1 <= time <= 1000

递枕头。

n 个人站成一排,按从 1 到 n 编号。

最初,排在队首的第一个人拿着一个枕头。每秒钟,拿着枕头的人会将枕头传递给队伍中的下一个人。一旦枕头到达队首或队尾,传递方向就会改变,队伍会继续沿相反方向传递枕头。

  • 例如,当枕头到达第 n 个人时,TA 会将枕头传递给第 n - 1 个人,然后传递给第 n - 2 个人,依此类推。

给你两个正整数 n 和 time ,返回 time 秒后拿着枕头的人的编号。

这道题我给出两种思路,一是模拟,二是数学。

模拟的思路是用一个 boolean flag 表示到底是往左走还是往右走,往右走 index++,往左走 index--,走到 index == 1 和 index == n 的时候 flag 要换方向。

时间O(time)

空间O(1)

Java实现

 1 class Solution {
 2     public int passThePillow(int n, int time) {
 3         int index = 1;
 4         boolean right = true;
 5         while (index >= 1 && index <= n && time != 0) {
 6             if (index == n) {
 7                 index--;
 8                 right = false;
 9             } else if (index == 1) {
10                 index++;
11                 right = true;
12             } else {
13                 index = right == true ? index + 1 : index - 1;
14             }
15             time--;
16         }
17         return index;
18     }
19 }

 

数学的思路如下。我们如果需要知道最后时间用完的时候走到哪个位置,我们需要知道两个信息,一是到底是往左走还是往右走,二是每走一轮需要消耗多少步。仔细观察,从 1 走到 n 需要 n - 1 步,同样的,从 n 走到 1 也需要 n - 1 步,所以我们可以把 time 除以 (n - 1) 就知道走了几轮,记为 round;把 time 模 (n - 1) 就知道最后一轮(最后一次掉头之后)走了几步,记为 mod。如果 round 是奇数那么说明停下之前是从左往右走的,反之则是从右往左走的。需要知道方向的原因是因为这决定了到底是从左边还是从右边计算最后走了几步。

时间O(1)

空间O(1)

Java实现

 1 class Solution {
 2     public int passThePillow(int n, int time) {
 3         int round = time / (n - 1);
 4         int mod = time % (n - 1);
 5         if (round % 2 == 1) {
 6             return n - mod;
 7         }
 8         return mod + 1;
 9     }
10 }

 

LeetCode 题目总结

标签:index,int,枕头,Pass,LeetCode,person,time,pillow,Pillow
From: https://www.cnblogs.com/cnoodle/p/17729923.html

相关文章

  • LeetCode54.螺旋数组
    本题关键在于模拟数组螺旋的步骤,使用 flag 二维数组标识矩阵某位置是否被访问过,使用 turn 变量指示当前寻找的方向, turn 为0时,代表向右查找, turn 为1时,代表向下查找, turn 为2时,代表向左查找, turn 为3时,代表向上查找,具体的代码如下:classSolution{publicList<......
  • 算法训练day20 LeetCode654
    算法训练day20LeetCode654.617.700.98654.最大二叉树题目654.最大二叉树-力扣(LeetCode)题解代码随想录(programmercarl.com)使用递归返回节点地址,输入父节点地址,数组终止条件是输入地数组为空单层操作:如果输入数组为空,则返回父节点地址否则找到数组中最大......
  • LeetCode 918. 环形子数组的最大和
    环形子数组的最大和(medium)题目链接:918.环形子数组的最大和题目描述:给定一个长度为n的环形整数数组nums,返回nums的非空子数组的最大可能和。环形数组意味着数组的末端将会与开头相连呈环状。形式上,nums[i]的下一个元素是nums[(i+1)%n],nums[i]的前一个元素......
  • LeetCode 53. 最大子数组和
    最大子数组和(medium)题目链接:53.最大子数组和题目描述:给你一个整数数组nums,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。子数组是数组中的一个连续部分。示例1:输入:nums=[-2,1,-3,4,-1,2,1,-5,4]输出:6解释:连续子数组[4,-1,2,1]的和最大......
  • leetcode21. 合并两个有序链表
    合并两个有序链表题目链接21.合并两个有序链表将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。示例1:输入:l1=[1,2,4],l2=[1,3,4]输出:[1,1,2,3,4,4]示例2:输入:l1=[],l2=[]输出:[]示例3:输入:l1=[],l2=[0]输出:[0......
  • OGG MA - Not Able To Log InAdmin server ERROR: User name 'oggadmin' or password
    ogg的密码文件可能会损坏需要修复就新建一个新的ogg微服务并且把密码文件考到问问题的地方进行覆盖,我觉得这个是一个bugTherecommendationistoCreateadummyMAinstallationonthesameserverordifferenttestserver.Thencopythewallet[cwallet.sso]fromthis......
  • [LeetCode] 1353. Maximum Number of Events That Can Be Attended 最多可以参加的会
    Youaregivenanarrayof events where events[i]=[startDayi,endDayi].Everyevent i startsat startDayi andendsat endDayi.Youcanattendanevent i atanyday d where startTimei<=d<=endTimei.Youcanonlyattendoneeventatanytime ......
  • #yyds干货盘点# LeetCode程序员面试金典:除自身以外数组的乘积
    题目:给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在  32位 整数范围内。请 不要使用除法,且在 O(n) 时间复杂度内完成此题。 示......
  • #yyds干货盘点# LeetCode程序员面试金典:全 O(1) 的数据结构
    1.简述:请你设计一个用于存储字符串计数的数据结构,并能够返回计数最小和最大的字符串。实现 AllOne 类:AllOne() 初始化数据结构的对象。inc(Stringkey) 字符串 key 的计数增加 1 。如果数据结构中尚不存在 key ,那么插入计数为 1 的 key 。dec(Stringkey) 字符串 k......
  • OpenLDAP:使用Self Service Password管理用户密码
    安装dockeryum-yinstalldocker拉取镜像dockerpullgrams/ltb-self-service-password编辑配置文件<?php#==============================================================================#LTBSelfServicePassword##Copyright(C)2009ClementOUDOT#Co......