We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e8baf50 commit 72a77c9Copy full SHA for 72a77c9
Easy/Sum of Variable Length Subarrays.java
@@ -0,0 +1,19 @@
1
+class Solution {
2
+ public int subarraySum(int[] nums) {
3
+ int n = nums.length;
4
+ int[] prefixSum = new int[n];
5
+ int sum = 0;
6
+ int result = 0;
7
+ for (int i = 0; i < n; i++) {
8
+ sum += nums[i];
9
+ prefixSum[i] = sum;
10
+ int start = Math.max(0, i - nums[i]);
11
+ int subarraySum = prefixSum[i];
12
+ if (start > 0) {
13
+ subarraySum -= prefixSum[start - 1];
14
+ }
15
+ result += subarraySum;
16
17
+ return result;
18
19
+}
0 commit comments