
PrefixSum
文章平均质量分 71
flyatcmu
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Intervals Between Identical Elements
You are given a0-indexedarray ofnintegersarr.Theintervalbetween two elements inarris defined as theabsolute differencebetween their indices. More formally, theintervalbetweenarr[i]andarr[j]is|i - j|.Returnan arrayintervalsof length...原创 2021-12-26 14:45:14 · 332 阅读 · 0 评论 -
Interval Sum
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers[start, end]. For each query, calculate the sum number between index sta...原创 2019-12-09 13:38:33 · 464 阅读 · 0 评论 -
Shortest Subarray with Sum at Least K
Return thelengthof the shortest, non-empty, contiguoussubarray ofAwith sum at leastK.If there is no non-empty subarray with sum at leastK, return-1.Example 1:Input: A = [1], K = 1Output: 1Example 2:Input: A = [1,2], K = 4Output: -1...原创 2020-06-25 10:44:35 · 396 阅读 · 0 评论 -
Number of Submatrices That Sum to Target
Given amatrix, and atarget, return the number of non-empty submatrices that sum totarget.A submatrixx1, y1, x2, y2is the set of all cellsmatrix[x][y]withx1 <= x <= x2andy1 <= y <= y2.Two submatrices(x1, y1, x2, y2)and(x1', y1',...原创 2020-06-24 13:03:53 · 363 阅读 · 0 评论 -
Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrixmatrixand an integerk, find the max sum of a rectangle in thematrixsuch that its sum is no larger thank.Example:Input: matrix = [[1,0,1],[0,-2,3]], k = 2Output: 2 Explanation:Because the sum of rectangle [[0, 1], [-...原创 2020-06-23 12:32:39 · 253 阅读 · 0 评论 -
Maximum Side Length of a Square with Sum Less than or Equal to Threshold
Given am x nmatrixmatand an integerthreshold. Return the maximum side-length of a square with a sum less than or equal tothresholdor return0if there is no such square.Example 1:Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]],...原创 2020-06-17 12:34:47 · 226 阅读 · 0 评论 -
Find Two Non-overlapping Sub-arrays Each With Target Sum
Given an array of integersarrand an integertarget.You have to findtwo non-overlapping sub-arraysofarreach with sum equaltarget. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays ismini...原创 2020-06-16 12:24:37 · 330 阅读 · 0 评论 -
New 21 Game
Alice plays the following game, loosely based on the card game "21".Alice starts with0points, and draws numbers while she has less thanKpoints. During each draw, she gains an integer number of points randomly from the range[1, W], whereWis an int...原创 2020-06-15 12:44:29 · 254 阅读 · 0 评论 -
Split Array with Equal Sum
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions:0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should b原创 2020-05-29 13:35:27 · 290 阅读 · 0 评论 -
Range Sum Query 2D - Immutable
Given a 2D matrixmatrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1,col1) and lower right corner (row2,col2).The above rectangle (with the red border) is defined by (row1, col1) =(2, 1)and (row2, col2) ...原创 2020-05-26 10:25:04 · 161 阅读 · 0 评论 -
[LeetCode] Continuous Subarray Sum
Given a list ofnon-negativenumbers and a targetintegerk, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple ofk, that is, sums up to n*k where n is also aninteger.Example 1:Input: [23, ...原创 2020-05-26 04:41:54 · 274 阅读 · 0 评论 -
Maximum Sum of 3 Non-Overlapping Subarrays
In a given arraynumsof positive integers, find three non-overlapping subarrays with maximum sum.Each subarray will be of sizek, and we want to maximize the sum of all3*kentries.Return the result as a list of indices representing the starting posit...原创 2020-05-15 13:26:17 · 233 阅读 · 0 评论 -
Friends Of Appropriate Ages
Some people will make friend requests. Thelist of their ages is given andages[i]is the age of theith person.Person A will NOT friend request person B (B != A) if any of the following conditions are true:age[B]<= 0.5 * age[A]+ 7 age[B]> a...原创 2020-05-12 11:49:33 · 220 阅读 · 0 评论 -
Product of the Last K Numbers
Implement the classProductOfNumbersthat supports two methods:1.add(int num)Adds the numbernumto the back of the current list of numbers.2.getProduct(int k)Returns the product of the lastknumbers in the current list. You can assume that alwa...原创 2020-05-12 04:23:37 · 240 阅读 · 0 评论 -
Find Pivot Index
Given an array of integersnums, write a method that returns the "pivot" index of this array.We define the pivot index as the index where the sum of the numbers to the left of the index is equal to ...原创 2020-05-06 13:26:27 · 276 阅读 · 0 评论 -
Maximum Size Subarray Sum Equals k
Given an arraynumsand a target valuek, find the maximum length of a subarray that sums tok. If there isn't one, return 0 instead.Note:The sum of the entirenumsarray is guaranteed to fit withi...原创 2020-04-22 13:53:31 · 231 阅读 · 0 评论 -
Subarray Sum Equals K
Given an array of integers and an integerk, you need to find the total number of continuous subarrays whose sum equals tok.Example 1:Input:nums = [1,1,1], k = 2Output: 2思路:用prefixsum记得用n+1 s...原创 2020-03-30 06:31:12 · 318 阅读 · 0 评论 -
Range Sum Query - Immutable
Given an integer arraynums, find the sum of the elements between indicesiandj(i≤j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRan原创 2016-07-12 10:33:22 · 331 阅读 · 0 评论 -
Prefix Sum 总结
Interval SumGiven an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers[start, end]. For each query, calculate the sum number bet...原创 2020-03-23 14:41:54 · 4629 阅读 · 0 评论 -
Matrix Block Sum
Given am * nmatrixmatand an integerK, return a matrixanswerwhere eachanswer[i][j]is the sum of all elementsmat[r][c]fori - K <= r <= i + K, j - K <= c <= j + K, and(r, c)is a...原创 2020-02-06 15:04:54 · 394 阅读 · 0 评论 -
Sliding Window Matrix Maximum
Given an array ofn * mmatrix, and a moving matrix window (sizek * k), move the window from top left to botton right at each iteration, find the maximum sum inside the window at each moving.Return...原创 2019-12-17 13:43:30 · 183 阅读 · 0 评论 -
Submatrix Sum
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number.If there are multiple answers, you can return ...原创 2019-12-16 14:01:23 · 457 阅读 · 0 评论 -
Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number.ExampleExample1Input: [-3,1,1,-3,5] Output: [0,2]Explanation: [0,2],...原创 2019-12-16 12:27:30 · 276 阅读 · 1 评论 -
[lintcode] Stone Game
There is a stone game.At the beginning of the game the player picksnpiles of stones in a line.The goal is to merge the stones in one pile observing the following rules:At each step of the game,t...原创 2019-12-14 11:21:16 · 224 阅读 · 0 评论 -
[lintcode] Stone Game II
There is a stone game.At the beginning of the game the player picks n piles of stonesin a circle.The goal is to merge the stones in one pile observing the following rules:At each step of the game...原创 2019-12-14 11:28:21 · 240 阅读 · 0 评论 -
Continuous Subarray Sum II
Given an circular integer array (the next element of the last element is the first element), find a continuous subarray in it, where the sum of numbers is the biggest. Your code should return the inde...原创 2019-12-16 10:20:43 · 129 阅读 · 0 评论 -
[LintCode] Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplic...原创 2019-12-16 09:51:26 · 182 阅读 · 0 评论 -
Subarray Sum II
Given an positive integer arrayAand an interval. Return the number of subarrays whose sum is in the range of given interval.ExampleExample 1:Input: A = [1, 2, 3, 4], start = 1, end = 3Output...原创 2019-12-16 03:26:39 · 384 阅读 · 0 评论 -
Subarray Sum
Given an integer array, find a subarray where the sum of numbers iszero. Your code should return the index of the first number and the index of the last number.ExampleExample 1:Input: [-3, 1,...原创 2019-12-15 11:54:14 · 345 阅读 · 0 评论