| | 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.RangeSumQueryImmutable; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class RangeSumQueryImmutableBruteForce : IRangeSumQueryImmutable |
| | 16 | | { |
| | 17 | | private readonly int[] _nums; |
| | 18 | |
|
| 0 | 19 | | public RangeSumQueryImmutableBruteForce(int[] nums) |
| 0 | 20 | | { |
| 0 | 21 | | _nums = nums; |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Time complexity - O(right − left + 1) |
| | 26 | | /// Space complexity - O(1) |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="left"></param> |
| | 29 | | /// <param name="right"></param> |
| | 30 | | /// <returns></returns> |
| | 31 | | public int SumRange(int left, int right) |
| 0 | 32 | | { |
| 0 | 33 | | var sum = 0; |
| | 34 | |
|
| 0 | 35 | | for (var i = left; i <= right; i++) |
| 0 | 36 | | { |
| 0 | 37 | | sum += _nums[i]; |
| 0 | 38 | | } |
| | 39 | |
|
| 0 | 40 | | return sum; |
| 0 | 41 | | } |
| | 42 | | } |