A Missing Test Case


  • 1
    P

    Test Case is :
    [-55,-24,-18,-11,-7,-3,4,5,6,9,11,23,33], target = 0

    the answer should have been 0, but my solution tells -1.

    and my accepted solution is:

    public class Solution {
    public int ThreeSumClosest(int[] nums, int t) {
        int i = 0, j = nums.Length-1, id = 0, d = 0;
        int min = int.MaxValue, less = int.MaxValue, more = int.MaxValue;
        Array.Sort<int>(nums);
        while (i < j - 1)
        {
            less = more = int.MaxValue;
            d = t - nums[i] - nums[j];
            id = BSIn(nums, i+1, j-1, d);
            if (id > 0)
                return t;
            id = ~id;
            if (id > i && id < j)
                more = nums[id] - d;
            if (id - 1 > i)
                less = d - nums[id-1];
            d = nums[i] + nums[j] + (more > less ? d-less : d+more);
            if (d < t)
                ++i;
            else
                --j;
            min = Closer(t, min, d);
        }
        return min;
    }
    
    private int Closer(int t, int a, int b)
    {
        long at = Math.Abs((long)a - t);
        long bt = Math.Abs((long)b - t);
        return at >= bt ? b : a;
    }
    
    private int BSIn(int[] nums, int i, int j, int d)
    {
        int m = 0;
        while (i <= j)
        {
            m = i + (j - i) / 2;
            if (d == nums[m])
                return m;
            if (d < nums[m])
                j = m - 1;
            else
                i = m + 1;
        }
        return ~i;
    }
    

    }


  • 0

    @pizzacrystal Thanks, I have added your test case.


Log in to reply
 

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