-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathSummaryRanges.java
111 lines (80 loc) · 2.8 KB
/
SummaryRanges.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Source: https://leetcode.com/problems/summary-ranges/
Basic Idea:
Get the first and last index of range
eg:
nums[] = {1,2,3,5,7,8}
Here, first index of range is 0(for element 1) and
the last index of range is 2(for element 3)
Now, since first and last index here are not equal(i.e. firstIndexOfRange != lastIndexOfRange),
append the element at start index of range with the element at last index of range
i.e. "1->3"
But what if there is only single element in the range
eg:
nums[] = {1,2,3,5,7,8}
Here, element 5 at 3rd index is the only single element in it's range
In this case, the first and last index of the range will be same. So,
append only that element i.e. "5"
*/
/*
1st approach (Two pointer)
Time: O(n), where n is the size of the given array (nums)
Space: O(1), in-place, though we are using extra List(rangeList), but we will still treat space as O(1) and not O(n)
because we usually don't consider the output in the space complexity i.e. only temporary spaces which are used to get the desired output are considered.
*/
class Solution {
public List<String> summaryRanges(int[] nums) {
int len = nums.length;
if(len == 0) {
return new ArrayList<>();
}
StringBuilder range = new StringBuilder();
List rangesList = new ArrayList<>();
for(int i = 0; i < len; ++i) {
int rangeStartIndex = i;
// to get the last index of range
while((i + 1) < len && nums[i] == nums[i + 1] - 1) {
++i;
}
range.append(nums[rangeStartIndex]);
// to check if there is not only a single sorted element
if(i != rangeStartIndex) {
range.append("->").append(nums[i]);
}
rangesList.add(range.toString());
range.setLength(0);
}
return rangesList;
}
}
/*
2nd approach (Two pointer, but more Optimized)
Time: O(n), where n is the size of the given array (nums)
Space: O(1), in-place, though we are using extra List(rangeList), but we will still treat space as O(1) and not O(n)
because we usually don't consider the output in the space complexity i.e. only temporary spaces which are used to get the desired output are considered.
*/
class Solution {
public List<String> summaryRanges(int[] nums) {
int len = nums.length;
if(len == 0) {
return new ArrayList<>();
}
StringBuilder range = new StringBuilder();
List rangesList = new ArrayList<>();
for(int i = 0; i < len;) {
range.append(nums[i]);
int rangeStartIndex = ++i;
// to get the last index of range
while(i < len && nums[i] == nums[i - 1] + 1) {
++i;
}
// to check if there is not only a single sorted element
if(i != rangeStartIndex) {
range.append("->").append(nums[i - 1]);
}
rangesList.add(range.toString());
range.setLength(0);
}
return rangesList;
}
}