{"id":9970,"date":"2023-03-05T07:42:34","date_gmt":"2023-03-05T15:42:34","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9970"},"modified":"2023-03-05T07:43:05","modified_gmt":"2023-03-05T15:43:05","slug":"leetcode-2582-pass-the-pillow","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2582-pass-the-pillow\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2582. Pass the Pillow"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;people standing in a line labeled from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>. 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.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, once the pillow reaches the&nbsp;<code>n<sup>th<\/sup><\/code>&nbsp;person they pass it to the&nbsp;<code>n - 1<sup>th<\/sup><\/code>&nbsp;person, then to the&nbsp;<code>n - 2<sup>th<\/sup><\/code>&nbsp;person and so on.<\/li><\/ul>\n\n\n\n<p>Given the two positive integers&nbsp;<code>n<\/code>&nbsp;and&nbsp;<code>time<\/code>, return&nbsp;<em>the index of the person holding the pillow after&nbsp;<\/em><code>time<\/code><em>&nbsp;seconds<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, time = 5\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> People pass the pillow in the following way: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 3 -&gt; 2.\nAfer five seconds, the pillow is given to the 2<sup>nd<\/sup> person.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3, time = 2\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> People pass the pillow in the following way: 1 -&gt; 2 -&gt; 3.\nAfer two seconds, the pillow is given to the 3<sup>r<\/sup><sup>d<\/sup> person.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= n &lt;= 1000<\/code><\/li><li><code>1 &lt;= time &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>It takes n &#8211; 1 seconds from 1 to n and takes another n &#8211; 1 seconds back from n to 1.<br>So one around takes 2 * (n &#8211; 1) seconds. We can mod time with 2 *  (n &#8211; 1).<\/p>\n\n\n\n<p>After that if time &lt; n &#8211; 1 answer is time + 1, otherwise answer is n &#8211; (time &#8211; (n &#8211; 1))<\/p>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int passThePillow(int n, int time) {\n    time %= (2 * (n - 1));\n    return time > n - 1 ? n - (time - (n - 1)) : time + 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;people standing in a line labeled from&nbsp;1&nbsp;to&nbsp;n. The first person in the line is holding a pillow initially. Every second, the person holding the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,31,158],"class_list":["post-9970","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-math","tag-mod","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9970"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9970\/revisions"}],"predecessor-version":[{"id":9972,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9970\/revisions\/9972"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}