Why WA but works on my machine with same test case? (C solution)


  • 0
    H
    typedef struct {
        int data;
        int curr_min;
    } elmt;
    
    typedef struct {
        elmt *bottom;
        int size, top;
    } MinStack;
    
    void minStackCreate(MinStack *stack, int maxSize) {
        stack->bottom = (elmt*)malloc(sizeof(elmt)*maxSize);
        stack->top = -1;
        stack->size = maxSize;
        stack->bottom->curr_min = 0x7FFFFFFF;
    }
    
    void minStackPush(MinStack *stack, int element) {
        if (stack->top >= stack->size) return;
        stack->top++;
        stack->bottom[stack->top].data = element;
        if (element < stack->bottom[stack->top-1].curr_min)
            stack->bottom[stack->top].curr_min = element;
        else stack->bottom[stack->top].curr_min = stack->bottom[stack->top-1].curr_min;
    }
    
    void minStackPop(MinStack *stack) {
        if (stack->top == -1) return;
        stack->top--;
    }
    
    int minStackTop(MinStack *stack) {
        if (stack->top != -1)
            return stack->bottom[stack->top].data;
        else return 0;
    }
    
    int minStackGetMin(MinStack *stack) {
        return stack->bottom[stack->top].curr_min;
    }
    
    void minStackDestroy(MinStack *stack) {
        free(stack->bottom);
        stack->bottom = NULL;
    }
    

    WA with test case below.
    Input: push(2),push(0),push(3),push(0),getMin,pop,getMin,pop,getMin,pop,getMin
    Output: [0,0,0,0]
    Expected: [0,0,0,2]
    Could anybody help to debug? thanks..


Log in to reply
 

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