| | 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.ZeroArrayTransformation1; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ZeroArrayTransformation1PrefixSum : IZeroArrayTransformation1 |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n + m) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums"></param> |
| | 22 | | /// <param name="queries"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public bool IsZeroArray(int[] nums, int[][] queries) |
| 2 | 25 | | { |
| 2 | 26 | | var prefixSum = new int[nums.Length + 1]; |
| | 27 | |
|
| 12 | 28 | | foreach (var query in queries) |
| 3 | 29 | | { |
| 3 | 30 | | prefixSum[query[0]]++; |
| 3 | 31 | | prefixSum[query[1] + 1]--; |
| 3 | 32 | | } |
| | 33 | |
|
| 2 | 34 | | var count = 0; |
| | 35 | |
|
| 10 | 36 | | for (var i = 0; i < nums.Length; i++) |
| 4 | 37 | | { |
| 4 | 38 | | count += prefixSum[i]; |
| | 39 | |
|
| 4 | 40 | | if (count < nums[i]) |
| 1 | 41 | | { |
| 1 | 42 | | return false; |
| | 43 | | } |
| 3 | 44 | | } |
| | 45 | |
|
| 1 | 46 | | return true; |
| 2 | 47 | | } |
| | 48 | | } |