| | | 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 ToggleLightBulbsLookup : 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 | | Span<bool> bulbsLookup = stackalloc bool[101]; |
| | | 26 | | |
| | 16 | 27 | | for (var i = 0; i < bulbs.Count; i++) |
| | 6 | 28 | | { |
| | 6 | 29 | | var bulb = bulbs[i]; |
| | | 30 | | |
| | 6 | 31 | | bulbsLookup[bulb] = !bulbsLookup[bulb]; |
| | 6 | 32 | | } |
| | | 33 | | |
| | 2 | 34 | | bulbs.Clear(); |
| | | 35 | | |
| | 404 | 36 | | for (var i = 1; i < bulbsLookup.Length; i++) |
| | 200 | 37 | | { |
| | 200 | 38 | | if (bulbsLookup[i]) |
| | 2 | 39 | | { |
| | 2 | 40 | | bulbs.Add(i); |
| | 2 | 41 | | } |
| | 200 | 42 | | } |
| | | 43 | | |
| | 2 | 44 | | return bulbs; |
| | 2 | 45 | | } |
| | | 46 | | } |