Skip to content

Commit 49ea65d

Browse files
committed
Add soultion of SummaryRanges task
1 parent bfdf8fc commit 49ea65d

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package by.andd3dfx.common;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import lombok.AllArgsConstructor;
6+
7+
/**
8+
* <pre>
9+
* <a href="https://leetcode.com/problems/summary-ranges/description/">Task description</a>
10+
*
11+
* You are given a sorted unique integer array nums.
12+
*
13+
* A range [a,b] is the set of all integers from a to b (inclusive).
14+
*
15+
* Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums
16+
* is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.
17+
*
18+
* Each range [a,b] in the list should be output as:
19+
* "a->b" if a != b
20+
* "a" if a == b
21+
*
22+
* Example 1:
23+
* Input: nums = [0,1,2,4,5,7]
24+
* Output: ["0->2","4->5","7"]
25+
* Explanation: The ranges are:
26+
* [0,2] --> "0->2"
27+
* [4,5] --> "4->5"
28+
* [7,7] --> "7"
29+
*
30+
* Example 2:
31+
* Input: nums = [0,2,3,4,6,8,9]
32+
* Output: ["0","2->4","6","8->9"]
33+
* Explanation: The ranges are:
34+
* [0,0] --> "0"
35+
* [2,4] --> "2->4"
36+
* [6,6] --> "6"
37+
* [8,9] --> "8->9"
38+
* </pre>
39+
*/
40+
public class SummaryRanges {
41+
42+
public static List<String> summaryRanges(int[] nums) {
43+
if (nums.length == 0) {
44+
return List.of();
45+
}
46+
47+
List<Range> ranges = new ArrayList<>();
48+
Range draftRange = new Range(nums[0], nums[0]);
49+
ranges.add(draftRange);
50+
51+
for (var value : nums) {
52+
if ((value - draftRange.right) <= 1) {
53+
draftRange.right = value;
54+
continue;
55+
}
56+
57+
draftRange = new Range(value, value);
58+
ranges.add(draftRange);
59+
}
60+
61+
return ranges.stream()
62+
.map(Range::toString)
63+
.toList();
64+
}
65+
66+
@AllArgsConstructor
67+
public static class Range {
68+
69+
private int left;
70+
private int right;
71+
72+
@Override
73+
public String toString() {
74+
if (left == right) {
75+
return String.valueOf(left);
76+
}
77+
return left + "->" + right;
78+
}
79+
}
80+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package by.andd3dfx.common;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.List;
6+
import org.junit.Test;
7+
8+
public class SummaryRangesTest {
9+
10+
@Test
11+
public void testSummaryRanges() {
12+
assertThat(SummaryRanges.summaryRanges(new int[]{}))
13+
.isEqualTo(List.of());
14+
15+
assertThat(SummaryRanges.summaryRanges(new int[]{0, 1, 2, 4, 5, 7}))
16+
.isEqualTo(List.of("0->2", "4->5", "7"));
17+
18+
assertThat(SummaryRanges.summaryRanges(new int[]{0, 2, 3, 4, 6, 8, 9}))
19+
.isEqualTo(List.of("0", "2->4", "6", "8->9"));
20+
21+
assertThat(SummaryRanges.summaryRanges(new int[]{-2147483648, 0, 2, 3, 4, 6, 8, 9}))
22+
.isEqualTo(List.of("-2147483648->0", "2->4", "6", "8->9"));
23+
}
24+
}

0 commit comments

Comments
 (0)