Skip to content

Commit 94ae374

Browse files
committed
hotel capacity algorithm
1 parent ddfc967 commit 94ae374

File tree

3 files changed

+141
-21
lines changed

3 files changed

+141
-21
lines changed

src/Algorithm/Various/Misc.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace doganoo\PHPAlgorithms\Algorithm\Various;
27+
28+
use doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException;
29+
use doganoo\PHPAlgorithms\Common\Util\Comparator;
30+
use doganoo\PHPAlgorithms\Datastructure\Lists\ArrayLists\ArrayList;
31+
32+
33+
/**
34+
* Class Misc
35+
* @package doganoo\PHPAlgorithms\Algorithm\Various
36+
*/
37+
class Misc {
38+
39+
/**
40+
* This method is imaginary, in order to have a better understanding how the
41+
* underlying algorithm works. It can also be seen as a hotline algorithm
42+
* (do we have enough persons to respond incoming calls) or for an parking
43+
* system, for instance.
44+
*
45+
* @param ArrayList $arrivals
46+
* @param ArrayList $departures
47+
* @param int $maxCapacity
48+
* @return bool
49+
* @throws IndexOutOfBoundsException
50+
*/
51+
public function hotelCapacity(ArrayList $arrivals, ArrayList $departures, int $maxCapacity): bool {
52+
/*
53+
* lets imagine we have a hotel booking system.
54+
* The $arrivals list holds the days on that persons
55+
* arrive.
56+
* The $departures list holds the days on that
57+
* persons leave the hotel.
58+
*/
59+
$arrivalSize = $n = $arrivals->size();
60+
$departureSize = $departures->size();
61+
62+
// base case: if the lists have an unequal
63+
// number of elements, stop processing
64+
if (Comparator::notEquals($arrivalSize, $departureSize)) return false;
65+
66+
/*
67+
* We need to sort the lists. This is critical
68+
* for the following alogorithm, as we need to
69+
* know the chronological sequence.
70+
*/
71+
$arrivals->sort();
72+
$departures->sort();
73+
74+
$aIndex = 0;
75+
$bIndex = 0;
76+
$capacity = 0;
77+
78+
while ($aIndex < $n && $bIndex < $n) {
79+
$aVal = $arrivals->get($aIndex);
80+
$bVal = $departures->get($bIndex);
81+
82+
/*
83+
* This is simple logic: if the arrival day
84+
* is less than the departure day, we have
85+
* one more guest in the hotel. Therefore, we
86+
* need to increment $capacity.
87+
*
88+
* Otherwise (departure day is less than
89+
* or equal arrival day), we have one
90+
* room freed and can decrement.
91+
*/
92+
Comparator::lessThan($aVal, $bVal) ?
93+
$capacity++ :
94+
$capacity--;
95+
96+
// If we exhausted the maximum capacity, we
97+
// return false
98+
if (Comparator::greaterThan($capacity, $maxCapacity)) return false;
99+
100+
$aIndex++;
101+
$bIndex++;
102+
}
103+
104+
return true;
105+
}
106+
107+
}

src/Datastructure/Graph/Tree/AVLTree.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AVLTree extends BinarySearchTree {
1717
* @return bool
1818
*/
1919
public function insert(?IBinaryNode $node) {
20+
return false;
2021
if (false === $node instanceof Node) return false;
2122

2223
$inserted = parent::insert($node);
@@ -88,6 +89,7 @@ public function insert(?IBinaryNode $node) {
8889
}
8990

9091
public function insertValue($value): bool {
92+
return false;
9193
$avlNode = new Node($value);
9294
return $this->insert($avlNode);
9395
}

src/Datastructure/Lists/ArrayLists/ArrayList.php

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,21 @@
2626
namespace doganoo\PHPAlgorithms\Datastructure\Lists\ArrayLists;
2727

2828

29+
use function array_diff;
30+
use function array_fill;
31+
use function array_filter;
32+
use const ARRAY_FILTER_USE_BOTH;
33+
use function array_slice;
34+
use function array_values;
35+
use ArrayIterator;
36+
use function count;
2937
use doganoo\PHPAlgorithms\Algorithm\Sorting\TimSort;
3038
use doganoo\PHPAlgorithms\Common\Exception\IndexOutOfBoundsException;
3139
use doganoo\PHPAlgorithms\Common\Interfaces\IComparable;
3240
use doganoo\PHPAlgorithms\Common\Util\Comparator;
41+
use function in_array;
42+
use IteratorAggregate;
43+
use JsonSerializable;
3344
use Traversable;
3445

3546
/**
@@ -47,7 +58,7 @@
4758
*
4859
* @package doganoo\PHPAlgorithms\Lists\ArrayLists
4960
*/
50-
class ArrayList implements \IteratorAggregate, \JsonSerializable, IComparable {
61+
class ArrayList implements IteratorAggregate, JsonSerializable, IComparable {
5162
/**
5263
* @const DEFAULT_ARRAY_SIZE
5364
*/
@@ -83,7 +94,7 @@ private function ensureCapacity(int $newCapacity): bool {
8394
}
8495

8596
$array = $this->array;
86-
$this->array = \array_fill(0, $newCapacity, null);
97+
$this->array = array_fill(0, $newCapacity, null);
8798
for ($i = 0; $i < $this->size(); $i++) {
8899
$this->array[$i] = $array[$i];
89100
}
@@ -125,10 +136,10 @@ public function isEmpty(): bool {
125136

126137
public function length(): int {
127138
$array = $this->array;
128-
$array = \array_filter($array, function ($value, $key) {
139+
$array = array_filter($array, function ($value, $key) {
129140
return $value !== null;
130-
}, \ARRAY_FILTER_USE_BOTH);
131-
return \count($array);
141+
}, ARRAY_FILTER_USE_BOTH);
142+
return count($array);
132143
}
133144

134145
/**
@@ -206,7 +217,7 @@ public function lastIndexOf($value) {
206217
$array[] = $key;
207218
}
208219
}
209-
return \count($array) === 0 ? null : $array;
220+
return count($array) === 0 ? null : $array;
210221
}
211222

212223
/**
@@ -231,9 +242,9 @@ public function remove($key): bool {
231242
*/
232243
public function containsKey(int $key): bool {
233244
$array = $this->array;
234-
$array = \array_filter($array, function ($value, $key) {
245+
$array = array_filter($array, function ($value, $key) {
235246
return $value !== null;
236-
}, \ARRAY_FILTER_USE_BOTH);
247+
}, ARRAY_FILTER_USE_BOTH);
237248
return array_key_exists($key, $array);
238249
}
239250

@@ -260,7 +271,7 @@ public function removeRange(int $start, int $end): bool {
260271
$removed &= $this->remove($key);
261272
}
262273
}
263-
$this->array = \array_values($this->array);
274+
$this->array = array_values($this->array);
264275
return $removed;
265276
}
266277

@@ -273,7 +284,7 @@ public function removeRange(int $start, int $end): bool {
273284
public function retainAll(ArrayList $arrayList): bool {
274285
$newArray = [];
275286
foreach ($arrayList as $value) {
276-
if (\in_array($value, $this->array)) {
287+
if (in_array($value, $this->array)) {
277288
$newArray[] = $value;
278289
}
279290
}
@@ -311,7 +322,7 @@ public function subList(int $start, int $end): ArrayList {
311322
return $arrayList;
312323
}
313324
//TODO preserve keys?
314-
$array = \array_slice($this->array, $start, $end - $start, true);
325+
$array = array_slice($this->array, $start, $end - $start, true);
315326
$arrayList->addAllArray($array);
316327
return $arrayList;
317328
}
@@ -350,7 +361,7 @@ public function add($item): bool {
350361
* @return bool
351362
*/
352363
public function addToIndex(int $index, $item): bool {
353-
if (\count($this->array) === $this->size()) {
364+
if (count($this->array) === $this->size()) {
354365
$this->ensureCapacity($this->size() * 2 + 1);
355366
}
356367
$this->array[$index] = $item;
@@ -385,8 +396,8 @@ public function addAll(ArrayList $arrayList): bool {
385396
* @since 5.0.0
386397
*/
387398
public function getIterator() {
388-
$array = \array_slice($this->array, 0, $this->length(), true);
389-
return new \ArrayIterator($array);
399+
$array = array_slice($this->array, 0, $this->length(), true);
400+
return new ArrayIterator($array);
390401
}
391402

392403
/**
@@ -400,7 +411,7 @@ public function getIterator() {
400411
public function jsonSerialize() {
401412
return [
402413
"default_capacity" => ArrayList::DEFAULT_CAPACITY
403-
, "size" => \count($this->array)
414+
, "size" => count($this->array)
404415
, "length" => $this->length()
405416
, "content" => $this->array,
406417
];
@@ -435,9 +446,9 @@ public function inList($value): bool {
435446
*/
436447
public function compareTo($object): int {
437448
if ($object instanceof ArrayList) {
438-
if (\count(\array_diff($this->array, $object->array)) === 0) return 0;
439-
if (\count($this->array) < \count($object->array)) return -1;
440-
if (\count($this->array) > \count($object->array)) return 1;
449+
if (count(array_diff($this->array, $object->array)) === 0) return 0;
450+
if (count($this->array) < count($object->array)) return -1;
451+
if (count($this->array) > count($object->array)) return 1;
441452
}
442453
return -1;
443454
}
@@ -446,14 +457,14 @@ public function compareTo($object): int {
446457
* @return bool
447458
*/
448459
public function sort(): bool {
449-
$array = \array_filter($this->array, function ($value, $key) {
460+
$array = array_filter($this->array, function ($value, $key) {
450461
return $value !== null;
451-
}, \ARRAY_FILTER_USE_BOTH);
462+
}, ARRAY_FILTER_USE_BOTH);
452463

453464

454465
$timSort = new TimSort();
455466
$array = $timSort->sort($array);
456-
$this->array = \array_fill(0, self::DEFAULT_CAPACITY, null);
467+
$this->array = array_fill(0, self::DEFAULT_CAPACITY, null);
457468
$this->addAllArray($array);
458469
return true;
459470
}

0 commit comments

Comments
 (0)