Incorrect solution accepted


  • 0
    D

    This solution is accepted but should not be. This solution ONLY works if the input has no negative ints but for whatever reason is still accepted. This was my first attempt with the idea thinking we could remove any of the array that is larger than the target and then running the unordered_map from twoSum problem 1. Can only remove numbers larger than target if we know there aren't negatives which we do not.

    unordered_map<int, int> m;
            vector<int> ret;
            int low = 0;
            int high = numbers.size()-1;
            int mid = low + (high-low) /2;
            if(numbers[numbers.size()-1] > target) { 
            while(numbers[mid] < target) {
                low = mid + 1;
                mid = low + (high-low) / 2;
            }
            } else {
                mid = numbers.size()-1;
            }
            int complement = 0;
            for(int i = 0; i < mid; i++) {
                complement = target - numbers[i];
                if(m.find(complement) != m.end()) {
                    ret.push_back(m.find(complement)->second+1);
                    ret.push_back(i+1);
                    return ret;
                } else {
                    m.emplace(numbers[i], i);
                }
            }
            return ret;
    
    

Log in to reply
 

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