Got a runtime error


  • 0
    S

    My code runs well and get correct answer in my machine, but I got a runtime error from OJ and the message says the input is
    push(-3), getMin.

    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define SIZE 10
    
    typedef struct {
    	int *s;
    	int top;
    	int min;
    } MinStack;
    
    void minStackCreate(MinStack *stack, int maxSize) {
    	stack->s = (int *)malloc(sizeof(int) * maxSize);
    	stack->min = INT_MAX;
    	stack->top = 0;
    }
    
    void minStackPush(MinStack *stack, int element) {
    	stack->s[(stack->top)++] = element;
    	if (element < stack->min)
    	{
    		stack->min = element;
    	}
    }
    
    int isEmpty(MinStack *stack)
    {
    	return (stack->top == 0);
    }
    
    void updateMin(MinStack *stack)
    {
    	int i = 0, upper = stack->top;
    	int min = INT_MAX;
    
    	if (isEmpty(stack))
    	{
    		stack->min = INT_MIN;
    	}else{
    		for (i = 0; i < upper; i++)
    		{
    			if (stack->s[i] < min)
    			{
    				min = stack->s[i];
    			}
    		}
    		stack->min = min;
    	}
    }
    
    void minStackPop(MinStack *stack) {
    	stack->top--;
    	if (stack->s[stack->top] == stack->min)
    	{
    		updateMin(stack);
    	}
    }
    
    int minStackTop(MinStack *stack) {
    	return stack->s[(stack->top)-1];
    }
    
    int minStackGetMin(MinStack *stack) {
    	return stack->min;
    }
    
    void minStackDestroy(MinStack *stack) {
    	free(stack->s);
    	free(stack);
    }

Log in to reply
 

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