Why is this problem tagged as "medium difficulty"? It seems to very very easy.


  • 0
    S

    Below I share my O(n) C++ solution where n is the numbers in the array. We can't do better, can we? I am just wondering why this question is tagged as a medium difficulty? It seems to be a 101 problem, or am I missing something?

    class Solution {
    public:
        vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
            int n = nums.size();
            vector<string> result;
            if (n < 1) {
                result.push_back(makeRangeString(lower, upper));
                return result;
            }
    
            
            if (nums[0] > lower){
                result.push_back(makeRangeString(lower, nums[0] - 1));
            } 
            for (int i = 1; i < n; ++i){
                if (nums[i] > nums[i - 1] + 1){
                    result.push_back(makeRangeString(nums[i-1] + 1, nums[i] - 1));
                }
            }
            if (nums[n-1] < upper){
                result.push_back(makeRangeString(nums[n-1] + 1, upper));
            } 
            
            return result;
        }
        
        string makeRangeString(int l, int u){
            if (l == u){
                return to_string(l);    
            }
            return (to_string(l) + "->" + to_string(u));
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.