| | | 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.ToggleLightBulbs; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ToggleLightBulbsBitmask : IToggleLightBulbs |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="bulbs"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public IList<int> ToggleLightBulbs(IList<int> bulbs) |
| | 2 | 24 | | { |
| | 2 | 25 | | Int128 state = 0; |
| | | 26 | | |
| | 16 | 27 | | for (var i = 0; i < bulbs.Count; i++) |
| | 6 | 28 | | { |
| | 6 | 29 | | var bulb = bulbs[i]; |
| | | 30 | | |
| | 6 | 31 | | state ^= Int128.One << bulb; |
| | 6 | 32 | | } |
| | | 33 | | |
| | 2 | 34 | | bulbs.Clear(); |
| | | 35 | | |
| | 408 | 36 | | for (var i = 0; i < 101; i++) |
| | 202 | 37 | | { |
| | 202 | 38 | | if (((state >> i) & 1) > 0) |
| | 2 | 39 | | { |
| | 2 | 40 | | bulbs.Add(i); |
| | 2 | 41 | | } |
| | 202 | 42 | | } |
| | | 43 | | |
| | 2 | 44 | | return bulbs; |
| | 2 | 45 | | } |
| | | 46 | | } |