| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.PassThePillow; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class PassThePillowSimulation : IPassThePillow |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="n"></param> |
| | | 22 | | /// <param name="time"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public int PassThePillow(int n, int time) |
| | 7 | 25 | | { |
| | 7 | 26 | | var currentPerson = 1; |
| | | 27 | | |
| | 7 | 28 | | var isReversed = false; |
| | | 29 | | |
| | 7 | 30 | | var currentTime = 0; |
| | | 31 | | |
| | 3118 | 32 | | while (currentTime < time) |
| | 3111 | 33 | | { |
| | 3111 | 34 | | if (isReversed) |
| | 1547 | 35 | | { |
| | 1547 | 36 | | currentPerson--; |
| | 1547 | 37 | | } |
| | | 38 | | else |
| | 1564 | 39 | | { |
| | 1564 | 40 | | currentPerson++; |
| | 1564 | 41 | | } |
| | | 42 | | |
| | 3111 | 43 | | if (currentPerson == n) |
| | 606 | 44 | | { |
| | 606 | 45 | | isReversed = true; |
| | 606 | 46 | | } |
| | 2505 | 47 | | else if (currentPerson == 1) |
| | 602 | 48 | | { |
| | 602 | 49 | | isReversed = false; |
| | 602 | 50 | | } |
| | | 51 | | |
| | 3111 | 52 | | currentTime++; |
| | 3111 | 53 | | } |
| | | 54 | | |
| | 7 | 55 | | return currentPerson; |
| | 7 | 56 | | } |
| | | 57 | | } |