_php example of a stack face test with PHP solution

Source: Internet
Author: User

Objective

Encounter an interview problem, the topic probably means as follows:

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

Preliminary ideas

1. To achieve the Min function (1) operation, the first idea is to calculate the current minimum, then think of a value to save the current stack of the minimum elements, and then push and pop operations to maintain this value. So min,push are all O (1), but pop is not, if the current pop-up is the minimum value, you need to look up the current element of the minimum value, this is not O (1).

2. And the above method does not use another stack, and then think of: In a stack stored in the order of the elements, also in the push and pop operations to maintain this ordered stack, as shown:

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 method O (1) of the complexity.

At that time I felt sure to cache the minimum information in another stack, but I don't know if it was because I didn't eat or how, thinking froze.

Correct solution

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

The biggest feature of the stack operation is that only the top element of the stack can be manipulated, and it is not just good to use a secondary stack to cache the minimum value of each stack operation. This way each time the pop operation, both sides of the pop-up can be; because the top of the stack is the minimum of the current stack, the push operation is also only necessary to compare the elements of the stack and the top of the secondary stack. So push, pop, Min are all O (1) operation. As shown in figure:

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

<?php/** * uses an auxiliary stack, O (1) complexity to find the smallest number in the stack * @hack class to simulate the stack through an array * * @author Laiwenhui/class strack{/** * Data Stack, save
  storage stack data; * * @var array/private $_arrdata = Array ();
  /** * Auxiliary stack, storing the bottom value information of each layer in the data group stack; * * @var array/private $_arrmin = Array ();
  /** * Stack Top unit * * @var int */private $_top=-1;
    /** * out 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 stack * @param int $element * @return BOOL */Public Function push ($element) {$element = Intval ($
    Element);
      If the stack is empty, direct into the stack if ($this->_top = = 1) {Array_push ($this->_arrdata, $element);
      Array_push ($this->_arrmin, $element);
      $this->_top++;
    return true;
    //Is not NULL to determine whether the value of the stack is smaller than the minimum stack $min = $this->_arrmin[$this->_top]; The minimum value $currentMin is calculated by comparison $element < $min?
    $element: $min;
    The minimum value in the current stack Array_push ($this->_arrmin, $currentMin);
    Data into the stack Array_push ($this->_arrdata, $element);

    $this->_top++;
  return true;  /** * Find 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 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 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 ^_^

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.