Are the test cases broken?


  • 0
    J

    I did a simple binary search solution in c++ but it gets reject by the final test case when I submit. However, the same test case works on my local machine AND when I use the "Run Code" button. Here is the code:

    class Solution {
    public:
        int findDuplicate(vector<int>& nums) {
            int l = 1;
            int h = nums.size()-1;
            int m;
            int count = 0;
            while (h-l>0) {
                m = (l+h)/2;
                count = 0;
                for (int i = 0; i <= nums.size(); i++) {
                    if (nums[i]>m && nums[i]<=h) count++;
                }
                if (count > h-m) {
                    l = m+1;
                }
                else {
                    h = m;
                }
            }
            return l;
        }
    };
    

    The test case is:

    [26,2,9,20,31,7,14,32,37,15,29,6,12,38,48,22,19,45,42,40,1,12,25,36,39,30,35,4,27,12,49,16,47,3,44,41,8,17,21,23,10,43,12,34,24,28,33,13,46,11]

    When I use "Run Code" or compile on my local machine, this outputs 12 (the correct answer). When I hit submit, the system claims my code gave the incorrect answer of: 49. Is this a glitch in leetcode's backend or did I actually manage to do something wrong. I'll point out that this solution passes all the other test cases...


  • 0

    Using nums[i] when i is nums.size() is a bad idea.


  • 0
    J

    Ah. Hence the unpredictable behavior. Thanks for catching that!


Log in to reply
 

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