leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1508.cpp (564B)


0 class Solution {
1 static const int MOD = 1E9 + 7;
3 public:
4 int rangeSum(const vector<int> &nums, int n, int left, int right) const {
5 vector<int> vec;
6 for (int i = 0; i < n; i++) {
7 for (int j = i, acc = 0; j < n; j++) {
8 vec.push_back(acc += nums[j]);
9 }
10 }
12 nth_element(vec.begin(), vec.begin() + left - 1, vec.end());
13 nth_element(vec.begin() + left, vec.begin() + right - 1, vec.end());
14 return accumulate(begin(vec) + left - 1, begin(vec) + right, 0ll) % MOD;
15 }
16 };