Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rajeevranjancom / Leetcode_Java Public Notifications You must be signed in to change notification settings Fork 0 Star 0 Code Issues 0 Pull requests 0 Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbsLeetcode_Java/327_CountOfRangeSum327.javaCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory107 lines (94 loc) · 3.04 KB masterBreadcrumbsLeetcode_Java/327_CountOfRangeSum327.javaCopy pathTopFile metadata and controlsCodeBlame107 lines (94 loc) · 3.04 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107/** * Given an integer array nums, return the number of range sums that lie in * [lower, upper] inclusive. * * Range sum S(i, j) is defined as the sum of the elements in nums between * indices i and j (i ≤ j), inclusive. * * Note: * A naive algorithm of O(n2) is trivial. You MUST do better than that. * * Example: * Given nums = [-2, 5, -1], lower = -2, upper = 2, * Return 3. * * The three ranges are : [0, 0], [2, 2], [0, 2] and their respective sums * are: -2, -1, 2. * */ public class CountOfRangeSum327 { public int countRangeSum(int[] nums, int lower, int upper) { int len = nums.length; if (nums == null || len == 0) return 0; int[][] dp = new int[len][len]; int res = 0; for (int i=0; i<len; i++) { dp[i][i] = nums[i]; if (nums[i] >= lower && nums[i] <= upper) res++; } for (int j=1; j<len; j++) { int ii = 0; int jj = j; while (jj < len && ii < len) { long curr = (long) dp[ii][jj-1] + (long) dp[ii+1][jj] - (long) dp[ii+1][jj-1]; // System.out.println(ii + ", " + jj + ": " + dp[ii][jj-1] + " + " + dp[ii+1][jj] + " = " + curr); if (curr >= lower && curr <= upper) res++; dp[ii][jj] = (int) curr; ii++; jj++; } } return res; } /** * https://discuss.leetcode.com/topic/33738/share-my-solution */ int count = 0; int lower; int upper; public int countRangeSum2(int[] nums, int lower, int upper) { long[] sum = new long[nums.length + 1]; long[] temp = new long[nums.length + 1]; sum[0] = 0; this.lower = lower; this.upper = upper; for (int i = 1; i <= nums.length; i++) { sum[i] = sum[i - 1] + (long) nums[i - 1]; } mergesort(sum, 0, sum.length - 1, temp); return count; } private void mergesort(long[] sum, int start, int end, long[] temp) { if (start >= end) { return; } int mid = start + (end - start) / 2; mergesort(sum, start, mid, temp); mergesort(sum, mid + 1, end, temp); merge(sum, start, mid, end, temp); } private void merge(long[] sum, int start, int mid, int end, long[] temp) { int right = mid + 1; int index = start; int low = mid + 1, high = mid + 1; for (int left = start; left <= mid; left++) { while (low <= end && sum[low] - sum[left] < lower) { low++; } while (high <= end && sum[high] - sum[left] <= upper) { high++; } while (right <= end && sum[right] < sum[left]) { temp[index++] = sum[right++]; } temp[index++] = sum[left]; count += high - low; } while (right <= end) { temp[index++] = sum[right++]; } for (int i = start; i <= end; i++) { sum[i] = temp[i]; } } } You can’t perform that action at this time.