Skip to content

Instantly share code, notes, and snippets.

@vstarck
Last active December 11, 2015 00:49
Show Gist options
  • Save vstarck/4519449 to your computer and use it in GitHub Desktop.
Save vstarck/4519449 to your computer and use it in GitHub Desktop.
stack.php
<?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