Stack for min value in O (1) time

Source: Internet
Author: User

Idea: 1: Add two fields to the data structure of the stack, such as

typedef struct {


int Data[max]; // all numbers in the storage stack

int top; // the top element of the storage stackDatathe position in the array

int min; // Minimum Value

int second;// sub-decimal value

}stack;

Pop,push the top element of the stack, so it's O (1)

Min Time takes the Min field of the stack, so is also O (1)

everyPushwhen comparing, if the currentPushthe element ratioStack->minSmall, replace with the current elementStack->min, with the originalminReplaceSecond. If the currentPushthe element ratioStack->minlarge, but thanSecondSmall, replace with the current elementStack->second. Thus reaching theminthe effect,The procedure is as follows

int push (Stack * s,int x) {

ASSERT (S!=null);

if (S->top>=max)// Prevent Stack Overflow

{

printf ("stackoverload!");

return-1;

}

s->data[s->top++]=x;//Pushthe basic function of thexthe position of the top element of the stack is self-increasing.1

if (x < s->min)//If the newly pressed x is smaller than the current min, replace min
S->second = s->min;
s->min=x;



else if (x< s->second) // Otherwise, if x is smaller than second , this replaces second



S->second= x;
// otherwise,minand theSecondIt 's not the same .
return 0;

}
everyPopthe time to compare, ifPopthe element ismin, you useSecondReplacemin(it's used here. )Second). Thus reaching theminthe effect,The procedure is as follows
int pop (stack *s,int *x)

{

ASSERT (S!=null);

if (s->top<=0)

{

printf ("stackempty!");

return-1;

}

(*x) =s->data[s->top--];//Popthe basic function of thexThe position of the top element of the stack is self-reducing .1
if (x==s->min)// ifxexactly equalsmin, it is necessary toSecondGivemin

s->min=s->second;
return 0;

}

int min (Stack *s,int *x)

{

ASSERT (S!=null);

(*x) =s->min;

return 0;

}

Idea 2: Set the secondary stack to the bottom, record the minimum value for each state, get the current value of the secondary stack each time it is inserted, and compare the inserted values

If the small is inserted into the minimum stack is it, otherwise is the original minimum value, in this way, pop,push,min three are

O (1) algorithm.

if the maximum value is required, then set another ass2 to record the maximum value of the current stack.

typedef struct {

int Data[max];

int top;

}stack;

STATIC int Push_stack (stack *s,int x)

{

ASSERT ((s!=null));

if (S->top>=max)

{

printf ("Stack overload");

return-1;

}

s->data[s->top++]=x;

return 0;

}

STATIC int pop_stack (Stack *s,int *x)

{

ASSERT (S!=null);

if (s->top<=0)

{

printf ("Stack Empty");

return-1;

}

(*x) =s->data[s->top--];

return 0;

}

int push (stack *main,stack *ass,int x)

{

ASSERT ((main!=null) && (ass!=null));

int temp;

Pop_stack (ass,&temp);

Push_stack (MAIN,X);

if (x<temp)

{

Push_stack (ass,temp);

Push_stack (ASS,X);

}

Else

{

Push_stack (ass,temp);

Push_stack (ass,temp);

}

return 0;

}

int pop (stack *main,stack *ass,int *x)

{

ASSERT ((main!=null) && (ass!=null));

int temp;

Pop_stack (ass,&temp);

Pop_stack (MAIN,X);

return 0;

}

int min (Stack *ass,int *x)

{

ASSERT ((main!=null) && (ass!=null));

Pop_stack (ASS,X);

Push_stack (*X);

return 0;
}

Stack for min value in O (1) time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.