Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aa1f683

Browse files
add 1508
1 parent bec9eae commit aa1f683

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1508|[Range Sum of Sorted Subarray Sums](https://leetcode.com/problems/range-sum-of-sorted-subarray-sums/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1508.java) | |Medium|Array, Sort|
1112
|1507|[Reformat Date](https://leetcode.com/problems/reformat-date/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1507.java) | |Easy|String|
1213
|1502|[Can Make Arithmetic Progression From Sequence](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1502.java) | |Easy|Array, Sort|
1314
|1496|[Path Crossing](https://leetcode.com/problems/path-crossing/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1496.java) | |Easy|String|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class _1508 {
8+
public static class Solution1 {
9+
public int rangeSum(int[] nums, int n, int left, int right) {
10+
List<Long> list = new ArrayList<>();
11+
for (int i = 0; i < nums.length; i++) {
12+
long sum = nums[i];
13+
list.add(sum);
14+
for (int j = i + 1; j < nums.length; j++) {
15+
sum += nums[j];
16+
list.add(sum);
17+
}
18+
}
19+
Collections.sort(list);
20+
long result = 0;
21+
for (int i = left - 1; i < right; i++) {
22+
result += list.get(i);
23+
}
24+
return (int) result % 1000000007;
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1508;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1508Test {
10+
private static _1508.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1508.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(13, solution1.rangeSum(new int[]{1, 2, 3, 4}, 4, 1, 5));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)