-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign_circular_queue.php
97 lines (81 loc) · 1.52 KB
/
design_circular_queue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Design Circular Queue
* @link https://leetcode.com/problems/design-circular-queue
*
* Approach:
* Queue built using Arrays and that is it!
*
* Time complexity:
* enQueue() - O(1)
* deQueue() - O(n)
* Beats: 100%
* Runtime: 60-100ms
*
* Space complexity - O(n)
*/
class MyCircularQueue {
private int $size;
private array $queue = [];
/**
* @param Integer $k
*/
function __construct($k) {
$this->size = $k;
}
/**
* @param Integer $value
* @return Boolean
*/
function enQueue($value) {
if ($this->isFull()) {
return false;
}
$this->queue[] = $value;
return true;
}
/**
* @return Boolean
*/
function deQueue() {
if ($this->isEmpty()) {
return false;
}
$queue = [];
for ($i = 1; $i < count($this->queue); $i++) {
$queue[] = $this->queue[$i];
}
$this->queue = $queue;
return true;
}
/**
* @return Integer
*/
function Front() {
if ($this->isEmpty()) {
return -1;
}
return $this->queue[0];
}
/**
* @return Integer
*/
function Rear() {
if ($this->isEmpty()) {
return -1;
}
return $this->queue[count($this->queue) - 1];
}
/**
* @return Boolean
*/
function isEmpty() {
return count($this->queue) === 0;
}
/**
* @return Boolean
*/
function isFull() {
return $this->size === count($this->queue);
}
}