C++ code get runtime error when running [1,1,1,1,1,5,5,5,5,5] however passed test using visual studio


  • 0
    J
    int maximumGap(vector<int> &num) {
        int len = num.size();
    if (len < 2)
    	return 0;
    int *mins = new int[len - 1];
    int *maxs = new int[len - 1];
    int mmin = num[0]; int mmax = num[0];
    for (int i = 1; i<len; i++)
    {
    	if (mmin > num[i])
    		mmin = num[i];
    	if (mmax < num[i])
    		mmax = num[i];
    }
    
    for (int i = 0; i<len - 1; i++)
    {
    	mins[i] = maxs[i] = -1;
    }
    
    double gap = (mmax - mmin + 1) / (len - 1);
    for (int i = 0; i<len; i++)
    {
    	int idx = (int)((num[i] - mmin) / gap);
    
    	if (num[i] < mins[idx] || mins[idx] == -1)
    		mins[idx] = num[i];
    	if (num[i] > maxs[idx])
    		maxs[idx] = num[i];
    }
    
    int mgap = 0;
    int left = mins[0];
    for (int i = 0; i<len - 1; i++)
    {
    	if (mins[i] >= 0)
    	{
    		int temp = max(mins[i] - left, maxs[i] - mins[i]);
    		if (temp > mgap)
    			mgap = temp;
    		left = maxs[i];
    	}
    }
    return mgap;
    }

Log in to reply
 

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