| | | 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.RangeSumOfSortedSubarraySums; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class RangeSumOfSortedSubarraySumsBruteForce : IRangeSumOfSortedSubarraySums |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n^2 log n) |
| | | 19 | | /// Space complexity - O(n^2) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="nums"></param> |
| | | 22 | | /// <param name="n"></param> |
| | | 23 | | /// <param name="left"></param> |
| | | 24 | | /// <param name="right"></param> |
| | | 25 | | /// <returns></returns> |
| | | 26 | | public int RangeSum(int[] nums, int n, int left, int right) |
| | 3 | 27 | | { |
| | 3 | 28 | | var sumsList = new List<int>(); |
| | | 29 | | |
| | 30 | 30 | | for (var i = 0; i < n; i++) |
| | 12 | 31 | | { |
| | 12 | 32 | | var sum = 0; |
| | | 33 | | |
| | 84 | 34 | | for (var j = i; j < n; j++) |
| | 30 | 35 | | { |
| | 30 | 36 | | sum += nums[j]; |
| | | 37 | | |
| | 30 | 38 | | sumsList.Add(sum); |
| | 30 | 39 | | } |
| | 12 | 40 | | } |
| | | 41 | | |
| | 3 | 42 | | var sortedSums = sumsList.Order().ToArray(); |
| | | 43 | | |
| | 3 | 44 | | var rangeSum = 0; |
| | | 45 | | |
| | 40 | 46 | | for (var i = left - 1; i < right; i++) |
| | 17 | 47 | | { |
| | 17 | 48 | | rangeSum += sortedSums[i]; |
| | 17 | 49 | | rangeSum %= 1000000007; |
| | 17 | 50 | | } |
| | | 51 | | |
| | 3 | 52 | | return rangeSum; |
| | 3 | 53 | | } |
| | | 54 | | } |