'''
keys: 
Solutions:
Similar: 
T:
S:
'''
from typing import List


class Solution:
    # more intuitive one
    def findMissingRanges1(self, nums: List[int], lower: int, upper: int) -> List[str]:
        res = []
        if not nums:
            res.append(self.helper(lower, upper))
            return res
        
        # find the range between lower and the first element in the array
        if nums[0] > lower:
            res.append(self.helper(lower, nums[0] - 1))
            
        # find the ranges between adjacent elements in the array
        for i in range(len(nums) - 1):
            if nums[i+1] > nums[i] + 1:
                res.append(self.helper(nums[i]+1, nums[i+1]-1))
        
        # find the range between upper and the last element in the array
        if nums[-1] < upper:
            res.append(self.helper(nums[-1]+1, upper))
            
        return res
            
    
    def helper(self, l, h):
        if l == h: 
            return str(l)
        else:    
            return str(l) + "->" + str(h)
        
        
    
    def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:
        nums = [lower-1] + nums + [upper+1] # corner case: [], 1, 1
        res = []
        
        for i in range(len(nums)-1):
            if nums[i+1] - nums[i] == 2:
                res.append(str(nums[i]+1))
            elif nums[i+1] - nums[i] > 2:
                res.append(str(nums[i]+1)+'->'+str(nums[i+1]-1))
        
        return res