| | | 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.NumberOfPeopleAwareOfSecret; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class NumberOfPeopleAwareOfSecretLineSweep : INumberOfPeopleAwareOfSecret |
| | | 16 | | { |
| | | 17 | | private const int Modulo = 1_000_000_007; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time complexity - O(n) |
| | | 21 | | /// Space complexity - O(n) |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="n"></param> |
| | | 24 | | /// <param name="delay"></param> |
| | | 25 | | /// <param name="forget"></param> |
| | | 26 | | /// <returns></returns> |
| | | 27 | | public int PeopleAwareOfSecret(int n, int delay, int forget) |
| | 2 | 28 | | { |
| | 2 | 29 | | var length = n + forget + 2; |
| | | 30 | | |
| | 2 | 31 | | Span<long> peopleWhoLearnOnDay = stackalloc long[length]; |
| | | 32 | | |
| | 2 | 33 | | peopleWhoLearnOnDay[1] = 1; |
| | | 34 | | |
| | 2 | 35 | | Span<long> sharingDelta = stackalloc long[length]; |
| | | 36 | | |
| | 2 | 37 | | long currentKnowCount = 1; |
| | | 38 | | |
| | 2 | 39 | | var personDelay = delay + 1; |
| | | 40 | | |
| | 2 | 41 | | if (personDelay < length) |
| | 2 | 42 | | { |
| | 2 | 43 | | sharingDelta[personDelay] = (sharingDelta[personDelay] + 1) % Modulo; |
| | 2 | 44 | | } |
| | | 45 | | |
| | 2 | 46 | | var personForget = forget + 1; |
| | | 47 | | |
| | 2 | 48 | | if (personForget < length) |
| | 2 | 49 | | { |
| | 2 | 50 | | sharingDelta[personForget] = (sharingDelta[personForget] - 1 + Modulo) % Modulo; |
| | 2 | 51 | | } |
| | | 52 | | |
| | 2 | 53 | | long currentSharers = 0; |
| | | 54 | | |
| | 20 | 55 | | for (var day = 2; day <= n; day++) |
| | 8 | 56 | | { |
| | 8 | 57 | | currentSharers = (currentSharers + sharingDelta[day]) % Modulo; |
| | | 58 | | |
| | 8 | 59 | | peopleWhoLearnOnDay[day] = currentSharers; |
| | | 60 | | |
| | 8 | 61 | | currentKnowCount = (currentKnowCount + peopleWhoLearnOnDay[day]) % Modulo; |
| | | 62 | | |
| | 8 | 63 | | var forgetDay = day - forget; |
| | | 64 | | |
| | 8 | 65 | | if (forgetDay >= 1) |
| | 3 | 66 | | { |
| | 3 | 67 | | currentKnowCount = (currentKnowCount - peopleWhoLearnOnDay[forgetDay]) % Modulo; |
| | | 68 | | |
| | 3 | 69 | | if (currentKnowCount < 0) |
| | 0 | 70 | | { |
| | 0 | 71 | | currentKnowCount += Modulo; |
| | 0 | 72 | | } |
| | 3 | 73 | | } |
| | | 74 | | |
| | 8 | 75 | | var shareStartDay = day + delay; |
| | 8 | 76 | | var shareEndDay = day + forget; |
| | | 77 | | |
| | 8 | 78 | | if (shareStartDay < length) |
| | 8 | 79 | | { |
| | 8 | 80 | | sharingDelta[shareStartDay] = (sharingDelta[shareStartDay] + peopleWhoLearnOnDay[day]) % Modulo; |
| | 8 | 81 | | } |
| | | 82 | | |
| | 8 | 83 | | if (shareEndDay < length) |
| | 8 | 84 | | { |
| | 8 | 85 | | sharingDelta[shareEndDay] = (sharingDelta[shareEndDay] - peopleWhoLearnOnDay[day] + Modulo) % Modulo; |
| | 8 | 86 | | } |
| | 8 | 87 | | } |
| | | 88 | | |
| | 2 | 89 | | return (int)(currentKnowCount % Modulo); |
| | 2 | 90 | | } |
| | | 91 | | } |