From 70b4a51a14501dec1508a696fae824059dcd1ac5 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Fri, 5 Jul 2024 21:24:06 -0600 Subject: 2582. pass the pillow --- pass_the_pillow.c | 23 +++++++++++++++++++++++ pass_the_pillow.py | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 pass_the_pillow.c create mode 100644 pass_the_pillow.py diff --git a/pass_the_pillow.c b/pass_the_pillow.c new file mode 100644 index 0000000..2ffe2d3 --- /dev/null +++ b/pass_the_pillow.c @@ -0,0 +1,23 @@ +// 2582. Pass the Pillow +#include "leetcode.h" + +/* + * 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 passe it to the next person standing in the line. once the + * pillow reaching the end of the line, the direction changes and people + * continue passing the pillow in the opposite direction. given the two positive + * integers 'n' and 'time', return the index of the peson holding the pillow + * after 'time' seconds. + */ + +int passThePillow(int n, int time) { + int rounds = time / (n - 1), ans = 0; + ans = !(rounds % 2) ? 1 + time % (n - 1) : n - time % (n - 1); + return ans; +} + +int main() { + printf("%d\n", passThePillow(4, 5)); // expect: 2 + printf("%d\n", passThePillow(3, 2)); // expect: 3 +} diff --git a/pass_the_pillow.py b/pass_the_pillow.py new file mode 100644 index 0000000..def9788 --- /dev/null +++ b/pass_the_pillow.py @@ -0,0 +1,27 @@ +# 2582. 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 passe it to the next person standing in the line. once the +pillow reaching the end of the line, the direction changes and people +continue passing the pillow in the opposite direction. given the two positive +integers 'n' and 'time', return the index of the peson holding the pillow +after 'time' seconds. +""" + + +class Solution(object): + def passThePillow(self, n, time): + """ + :type n: int + :type time: int + :rtype: int + """ + return n - abs(n - 1 - time % (n * 2 - 2)) + + +if __name__ == "__main__": + obj = Solution() + print(obj.passThePillow(4, 5)) + print(obj.passThePillow(3, 2)) -- cgit v1.2.3