Bug? All test cases passed but memory limit exceeded.


  • 1
    Y

    All test cases passed, but memory limit exceeded. I am using JavaScript. What happened?

    0_1480994694814_Screen Shot 2016-12-05 at 22.21.32.png

    My code:

    let isSubsequence = function (s, t) {
        let set = [];
    
        for (let i = 0; i < t.length; i++) {
            if (set[t.charCodeAt(i) - 97]) set[t.charCodeAt(i) - 97].push(i);
            else set[t.charCodeAt(i) - 97] = [i];
        }
    
        let prevIndex = -1;
    
        for (let i = 0; i < s.length; i++) {
            let j = set[s.charCodeAt(i) - 97];
    
            if (!j) return false;
    
            let index = binarySearch(j, prevIndex);
    
            if (index === j.length) return false;
            else prevIndex = j[index];
        }
    
        return true;
    };
    
    //Find the index to insert num.
    function binarySearch(nums, num) {
        if (num < nums[0]) {
            return 0;
        } else if (num >= nums[nums.length - 1]) {
            return nums.length;
        } else {
            let left = 0, right = nums.length - 1;
    
            while (left + 1 < right) {
                let mid = Math.floor((left + right) / 2);
    
                if (nums[mid] > num) right = mid;
                else left = mid;
            }
    
            return right;
        }
    }

  • 0

    @ealonwang You could label the language you are using, actually, to ease some burden from the helpers.


  • 1
    Y

    @LHearen Thanks. I just did that.


Log in to reply
 

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