iT邦幫忙

2024 iThome 鐵人賽

DAY 13
0
自我挑戰組

Leetcode 解題之旅:逐日攻克系列 第 13

每日一LeetCode(13)

  • 分享至 

  • xImage
  •  

3178. Find the Child Who Has the Ball After K Seconds

題目敘述:

You are given two positive integers n and k. There are n children numbered from 0 to n - 1 standing in a queue in order from left to right.

Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches either end of the line, i.e. child 0 or child n - 1, the direction of passing is reversed.

Return the number of the child who receives the ball after k seconds.

class Solution {
public:
    int numberOfChild(int n, int k) 
    {
        n--;
        int rounds=k/n;
        int rem = k % n;
        if(rounds%2==0)
        {
            return rem;
        }
        else
        {
            return (n-rem);
        }
    }
};

上一篇
每日一LeetCode(12)
下一篇
每日一LeetCode(14)
系列文
Leetcode 解題之旅:逐日攻克17
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言