首页 > 其他分享 >leetcode-2582-easy

leetcode-2582-easy

时间:2023-07-22 21:22:34浏览次数:28  
标签:2582 int pass leetcode person easy time line pillow

Pass the 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 个人,传递一轮要 n - 1 秒,最后统计轮次,偶数表示从 1 开始算,奇数表示从尾部开始算

    public int passThePillow(int n, int time) {
        int x = time / (n - 1);
        int y = time % (n - 1);
        if ((x & 1) == 1) {
            return n - y;
        } else {
            return y + 1;
        }
    }

标签:2582,int,pass,leetcode,person,easy,time,line,pillow
From: https://www.cnblogs.com/iyiluo/p/17574271.html

相关文章

  • Leetcode394. 字符串解码
    classSolution{public:stringdfs(strings,int&idx){stringstr;while(idx<s.size()){if(s[idx]==']'){idx++;returnstr;}......
  • EasyExcel
    依赖配置:<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel<artifactId><version>2.2.0-beta2</version></dependency>实体类配置:@DatapublicclassSubjectEeVo{@ExcelProperty(val......
  • INFINI Labs 产品更新 | Easysearch 新增分词插件、Gateway 支持邮件发送等功能
    INFINILabs产品又更新啦~,本次更新概要如下:Easysearch新增了分词插件、优化了生命周期管理功能等;Gateway新增smtp过滤器来支持邮件的发送,支持自动跳过因为异常关闭而损坏的磁盘队列文件等;Console新增熔断器监控指标、新增矩形树图(Treemap)、优化了探针Agent指标采集和集......
  • leetcode 栈与队列 232 225
    目录基本介绍四个问题232225基本介绍栈,先进后出队列,先进先出四个问题C++中stack是容器么?我们使用的stack是属于哪个版本的STL?我们使用的STL中stack是如何实现的?stack提供迭代器来遍历stack空间么?首先大家要知道栈和队列是STL(C++标准库)里面的两个数据结构。C++标准......
  • 流媒体视频融合平台EasyCVR更新版本后,首页无法打开的原因排查与解决
    EasyCVR视频融合平台基于云边端一体化架构,可支持多协议、多类型设备接入,包括:NVR、IPC、视频编码器、无人机、车载设备、智能手持终端、移动执法仪等。平台具有强大的数据接入、处理及分发能力,可在复杂的网络环境中,将分散的各类视频资源进行统一汇聚、整合、集中管理。有用户反馈......
  • [LeetCode] 1349. Maximum Students Taking Exam 参加考试的最大学生数
    Givena m *n matrix seats  thatrepresentseatsdistributions inaclassroom. Ifaseat is broken,itisdenotedby '#' characterotherwiseitisdenotedbya '.' character.Studentscanseetheanswersofthosesittingnexttothele......
  • codility 和 leetcode 对比
    根据网上的信息,codility和leetcode都是用于评估编程技能的在线平台,它们都提供了不同难度和类型的编程挑战,支持多种编程语言,并可以用于招聘和面试的过程中。不过,它们也有一些区别,比如:codility更专注于工程团队的技能评估,它提供了CodeCheck,CodeLive,和CodeEvent三个功......
  • leetcode872叶相似树
    这道题是考虑的深度优先搜索,使用递归vecotr和queue入队操作并不相同:vector只能使用push_back();queue既可以使用push()还可以使用push_back()voidFindLeaf(TreeNode*root,vector<int>&v){if(!root->left&&!root->right){v.push_back(root->val);re......
  • LeetCode 347. 前 K 个高频元素
    快排思想注意,这里是倒序排序,因此应该while(nums[i].cnt>x);classSolution{public:structelement{intval,cnt;element(inta,intb){val=a;cnt=b;}};vector<int>res;voidquick......
  • LeetCode -- 773. 滑动谜题
     启发式搜索 classSolution{structNode{stringstr;intx,y;intval;};intn=2,m=3;stringe="123450";intdx[4]={-1,0,1,0};intdy[4]={0,1,0,-1};intf(stringstr){intres=0;for(inti=0;i<......