PHP solution for a stack of face questions _php tutorial

Source: Internet
Author: User
Preface

Encounter an interview problem, the topic is probably the following:

Using two ordinary stacks to implement a special stack, so that the pop, push, min three functions are the complexity of the O (1) operation, the Min function is to obtain the minimum value of the current stack.

Preliminary ideas

1. To implement the Min function as (1) operation, the first idea was to calculate the current minimum value beforehand, and then think of a value to save the minimum element in the current stack, and then push and pop operations to maintain the value. So the Min,push are all O (1), but the pop is not, if the current popup is the minimum value, you need to find the minimum value of the current element, this is not O (1).

2. And the above method does not use another stack, and then think: in a stack to store the ordered elements, also in the push and pop operations to maintain the ordered stack,

But in this case min operation is O (1), but push, pop operation because to maintain this ordered stack, how can not think of a way to the complexity of O (1).

At the time I felt sure it was in the other stack to cache the minimum information, but do not know because there is no food or how to, the mind froze.

Correct solution

Encounter problems can not solve, feel very uncomfortable, so eat time and began to think how to fully justify the characteristics of the stack, effective cache minimum information, so that min operation to use.

The most important feature of the stack operation is that only the top element of the stack can be manipulated, and it is not just as good to think of the minimum value for each stack operation with a secondary stack cache. In this way each pop operation, both sides of the pop-up can be, because the stack top of the auxiliary stack of the most current stack of the minimum value, the push operation is also only need to compare the stack element and the top element of the auxiliary stack can be. So push, pop, Min are all O (1) operation.

The text may not be clear, on the code, the following is the implementation of PHP, through the array to simulate the stack.

<?php/** * Using a secondary stack, O (1) complexity to find the minimum number in the stack * @hack class to emulate the stack by an array * * @author laiwenhui */class strack{/** * Data stack, storage stack data; *  * @var Array */private $_arrdata = Array ();  /** * Auxiliary stack, storing the value information of each layer in the data stack; * * @var array */private $_arrmin = Array ();  /** * Stack Top unit * * @var int */private $_top=-1;    /** * out of Stack * @return Bool|int */Public Function pop () {if ($this->_top = = =-1) {return false;    } array_pop ($this->_arrmin);    $this->_top--;  Return Array_pop ($this->_arrdata); }/** * into the stack * @param int $element * @return BOOL */Public Function push ($element) {$element = Intval ($element    );      If the stack is empty, directly into the stack if ($this->_top = = = 1) {Array_push ($this->_arrdata, $element);      Array_push ($this->_arrmin, $element);      $this->_top++;    return true;    }//Not NULL to determine whether the value of the stack is smaller than the minimum stack $min = $this->_arrmin[$this->_top]; Compare to find the minimum $currentMin = $element < $min?    $element: $min; The minimum value in the current stack is in the stack Array_pusH ($this->_arrmin, $currentMin);    Data into the stack Array_push ($this->_arrdata, $element);    $this->_top++;  return true;     }/** * The minimum value of the current stack space * @return Bool|int */Public Function min () {if ($this->_top = = =-1) {return false;  } return $this->_arrmin[$this->_top]; }}

Use the following:
Copy the Code code as follows:
$obj = new Strack ();
$obj->push (12);
$obj->push (56);
$obj->push (23);
$obj->push (89);
$obj->push (4);
Var_dump ($obj->min ());
$obj->pop ();
Var_dump ($obj->min ());
$obj->push (8);
Var_dump ($obj->min ());

The output is:
Copy the Code code as follows:
Int (4)
Int (12)
Int (8)

OK, meet the requirements.

Do you have any other better way to achieve, if there is, please tell me ^_^

http://www.bkjia.com/PHPjc/824668.html www.bkjia.com true http://www.bkjia.com/PHPjc/824668.html techarticle the preface encountered an interview problem, the topic is probably the following: Using two ordinary stacks to implement a special stack, so that pop, push, min three functions are the complexity of the O (1) operation, min function ...

  • 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.