Advertisement
LikeRampage

C++ leetcode 327. Count of Range Sum CHATGPT Unlimited

Apr 20th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | Source Code | 0 0
  1. AI: Hello human, Start Your Conversation, Ask me anything!
  2. You: solve leetcode 327. Count of Range Sum class Solution {
  3. public:
  4. int countRangeSum(vector<int>& nums, int lower, int upper) {
  5.  
  6. }
  7. };
  8.  
  9. AI: To solve LeetCode problem 327 "Count of Range Sum", you can implement a solution using a merge sort-based approach. Here is a possible implementation of the `countRangeSum` function within the `Solution` class:
  10.  
  11.  
  12. cpp
  13. #include <vector>
  14. #include <iostream>
  15.  
  16. class Solution {
  17. public:
  18. int countRangeSum(std::vector<int>& nums, int lower, int upper) {
  19. int count = 0;
  20. std::vector<long> prefixSum(nums.size() + 1, 0);
  21.  
  22. for (int i = 0; i < nums.size(); i++) {
  23. prefixSum[i + 1] = prefixSum[i] + nums[i];
  24. }
  25.  
  26. count = mergeSortCount(prefixSum, 0, prefixSum.size(), lower, upper);
  27.  
  28. return count;
  29. }
  30.  
  31. int mergeSortCount(std::vector<long>& prefixSum, int left, int right, int lower, int upper) {
  32. if (right - left <= 1) {
  33. return 0;
  34. }
  35.  
  36. int mid = left + (right - left) / 2;
  37. int count = mergeSortCount(prefixSum, left, mid, lower, upper) + mergeSortCount(prefixSum, mid, right, lower, upper);
  38.  
  39. int j = mid, k = mid, t = mid;
  40. std::vector<long> sorted(right - left, 0);
  41.  
  42. for (int i = left, r = 0; i < mid; i++, r++) {
  43. while (j < right && prefixSum[j] - prefixSum[i] < lower) j++;
  44. while (k < right && prefixSum[k] - prefixSum[i] <= upper) k++;
  45. while (t < right && prefixSum[t] < prefixSum[i]) sorted[r++] = prefixSum[t++];
  46. sorted[r] = prefixSum[i];
  47. count += k - j;
  48. }
  49.  
  50. std::copy(sorted.begin(), sorted.begin() + t - left, prefixSum.begin() + left);
  51.  
  52. return count;
  53. }
  54. };
  55.  
  56. int main() {
  57. Solution solution;
  58. std::vector<int> nums = {-2, 5, -1};
  59. int lower = -2;
  60. int upper = 2;
  61.  
  62. int result = solution.countRangeSum(nums, lower, upper);
  63.  
  64. std::cout << "Count of range sum within [" << lower << ", " << upper << "]: " << result << std::endl;
  65.  
  66. return 0;
  67. }
  68.  
  69.  
  70.  
  71. In this implementation, the `countRangeSum` function calculates the prefix sum of the input vector `nums` and then calls the `mergeSortCount` function to perform a merge sort-based counting of the range sums within the specified `lower` and `upper` bounds. The `mergeSortCount` function recursively divides the array and counts the valid range sums. Finally, the main function demonstrates how to use the `Solution` class to solve the problem for a sample input.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement