You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Runtime: 46 ms, faster than 86.21% of JavaScript online submissions for Summary Ranges.
4
+
// Time Complexity : O(N)
5
+
// Space Complexity : O(1)
2
6
varsummaryRanges=function(nums){
3
-
letresult=[];
4
-
letstartIdx=0;
5
-
6
-
for(leti=0;i<nums.length;i++){
7
-
letpeekRight=nums[i+1];
8
-
letpeekLeft=nums[i-1];
9
-
if(peekRight!==nums[i]+1&&peekLeft!==nums[i]-1){
10
-
result.push(`${nums[i]}`);
11
-
startIdx++;
12
-
}elseif(peekRight!==nums[i]+1){
13
-
result.push(`${nums[startIdx]}->${nums[i]}`);
14
-
startIdx=i+1;
15
-
}
16
-
17
-
}
18
-
returnresult;
7
+
// Create a list of string to store the output result...
8
+
constoutput=[];
9
+
// Start traversing the array from idx = 0 till idx < sizeofarray in a while loop.
10
+
letidx=0;
11
+
while(idx<nums.length){
12
+
// Initialize beg and last index for identifying the continuous element in the array...
13
+
letbeg,last;
14
+
// Mark the number at current index as beginning element of the range...
15
+
beg=nums[idx];
16
+
// Traverse the array beggining from current index & find the last element whose difference from previous element is exactly 1, i.e. nums[idx + 1] == nums[idx] + 1...
0 commit comments