| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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.ShortestSubarrayWithORAtLeastK2; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ShortestSubarrayWithORAtLeastK2SlidingWindow : IShortestSubarrayWithORAtLeastK2 |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums"></param> |
| | 22 | | /// <param name="k"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int MinimumSubarrayLength(int[] nums, int k) |
| 3 | 25 | | { |
| 3 | 26 | | var minLength = int.MaxValue; |
| 3 | 27 | | var windowStart = 0; |
| 3 | 28 | | var windowEnd = 0; |
| 3 | 29 | | var bitCounts = new int[32]; |
| | 30 | |
|
| 11 | 31 | | while (windowEnd < nums.Length) |
| 8 | 32 | | { |
| 8 | 33 | | UpdateBitCounts(bitCounts, nums[windowEnd], 1); |
| | 34 | |
|
| 14 | 35 | | while (windowStart <= windowEnd && ConvertBitCountsToNumber(bitCounts) >= k) |
| 6 | 36 | | { |
| 6 | 37 | | minLength = Math.Min(minLength, windowEnd - windowStart + 1); |
| | 38 | |
|
| 6 | 39 | | UpdateBitCounts(bitCounts, nums[windowStart], -1); |
| | 40 | |
|
| 6 | 41 | | windowStart++; |
| 6 | 42 | | } |
| | 43 | |
|
| 8 | 44 | | windowEnd++; |
| 8 | 45 | | } |
| | 46 | |
|
| 3 | 47 | | return minLength == int.MaxValue ? -1 : minLength; |
| 3 | 48 | | } |
| | 49 | |
|
| | 50 | | private static void UpdateBitCounts(int[] bitCounts, int number, int delta) |
| 14 | 51 | | { |
| 924 | 52 | | for (var bitPosition = 0; bitPosition < 32; bitPosition++) |
| 448 | 53 | | { |
| 448 | 54 | | if (((number >> bitPosition) & 1) != 0) |
| 16 | 55 | | { |
| 16 | 56 | | bitCounts[bitPosition] += delta; |
| 16 | 57 | | } |
| 448 | 58 | | } |
| 14 | 59 | | } |
| | 60 | |
|
| | 61 | | private static int ConvertBitCountsToNumber(int[] bitCounts) |
| 10 | 62 | | { |
| 10 | 63 | | var result = 0; |
| | 64 | |
|
| 660 | 65 | | for (var bitPosition = 0; bitPosition < 32; bitPosition++) |
| 320 | 66 | | { |
| 320 | 67 | | if (bitCounts[bitPosition] != 0) |
| 16 | 68 | | { |
| 16 | 69 | | result |= 1 << bitPosition; |
| 16 | 70 | | } |
| 320 | 71 | | } |
| | 72 | |
|
| 10 | 73 | | return result; |
| 10 | 74 | | } |
| | 75 | | } |