Last active
December 11, 2015 00:49
-
-
Save vstarck/4519449 to your computer and use it in GitHub Desktop.
stack.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Stack { | |
private $mins = array(); | |
private $arr = array(); | |
private $currentMin = null; | |
private $size = 0; | |
public function add($n) { | |
$this->size++; | |
if(is_null($this->currentMin) || $n < $this->currentMin) { | |
$this->currentMin = $n; | |
} | |
$this->mins[] = $this->currentMin; | |
$this->arr[] = $n; | |
} | |
public function pop() { | |
if($this->size > 0) $this->size--; | |
array_pop($this->mins); | |
$mins = $this->mins; | |
$this->currentMin = array_pop($mins); | |
return array_pop($this->arr); | |
} | |
function size() { | |
return $this->size; | |
} | |
public function getMin() { | |
$mins = $this->mins; | |
return array_pop($mins); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment