My code gives "false" for case "[0], 0, 0" in local machines, but it fails in judge system


  • 0
    R

    My code output false for test case "[0], 0, 0" in both Windows and Linux, but it fails in judge system. Anyone explain the reason?

    struct ValueIdxPair {
        int value;
        int idx;
    };
    
    bool operator<(const ValueIdxPair& a, const ValueIdxPair& b)
    {
        return a.value < b.value;
    }
    
    class Solution {
    private:
        int binarySearch(const vector<ValueIdxPair>& vec, int s, int e, int v)
        {
            int mid;
            while (s + 1 < e) {
                mid = (s + e) / 2;
                if (vec[mid].value == v) {
                    return mid;
                }
                else if (vec[mid].value < v) {
                    s = mid;
                }
                else {
                    e = mid - 1;
                }
            }
    
            if (v <= vec[e].value && v > vec[s].value) {
                return e;
            }
            else if (v <= vec[s].value) {
                return s;
            }
    
            return -1;
        }
    
    public:
        bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
            int i, j, p;
            int l = nums.size();
            vector<ValueIdxPair> vec(l);
            for (i = 0; i < l; i++) {
                vec[i].value = nums[i];
                vec[i].idx = i;
            }
    
            sort(vec.begin(), vec.end());
    
            for (i = 0; i < l; i++) {
                j = binarySearch(vec, i + 1, l - 1, vec[i].value + t);
                if (j >= 0) {
                    for (p = i + 1; p <= j; p++) {
                        if (abs(vec[i].idx - vec[p].idx) <= k) {
                            return true;
                        }
                    }
                }
            }
    
            return false;
        }
    };

Log in to reply
 

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