Re: [閒聊] 每日leetcode

作者: eden6197 (:))   2024-08-04 14:32:45
題目:
問你subarray的和sort之後
left到right加起來是多少
思路:
照做
把subarray 通通弄出來之後
sort加起來
姆咪
```cpp
class Solution {
public:
int rangeSum(vector<int>& nums, int n, int left, int right)
{
vector<int> sum(n,0);
vector<int> paper(n * (n + 1) / 2 , 0);
sum[0] = nums[0];
for(int i = 1 ; i < n ; i ++)
{
sum[i]+=sum[i-1];
sum[i]+=nums[i];
}
int p = 0;
for(int i = 0 ; i < n ; i ++)
{
paper[p] = sum[i];
p++;
for(int j = i+1 ; j < n ; j ++)
{
paper[p] = sum[j] - sum[i];
p++;
}
}
sort(paper.begin() , paper.end());
int res = 0;
for(int i = left -1 ; i < right ; i ++)
{
res += paper[i];
res %= 1000000007;
}
return res;
}
};
```
作者: nozomizo (希霙)   2024-08-04 14:34:00
什麼!
作者: SAKIASHIZAWA (芦澤サキ)   2024-08-04 14:34:00
雞哥多才多藝 竟然也會寫code

Links booklink

Contact Us: admin [ a t ] ucptt.com