-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathXOfAKindInADeckOfCards.php
More file actions
59 lines (50 loc) · 1.27 KB
/
XOfAKindInADeckOfCards.php
File metadata and controls
59 lines (50 loc) · 1.27 KB
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
<?php
declare(strict_types=1);
namespace leetcode;
class XOfAKindInADeckOfCards
{
public static function hasGroupsSizeX(array $deck): bool
{
if (empty($deck)) {
return false;
}
[$ans, $map] = [0, []];
foreach ($deck as $val) {
$map[$val] = isset($map[$val]) ? $map[$val] + 1 : 1;
}
foreach ($map as $val) {
$ans = self::helper($ans, $val);
}
return $ans > 1;
}
public static function hasGroupsSizeX2(array $deck): bool
{
if (empty($deck)) {
return false;
}
$map = [];
foreach ($deck as $val) {
$map[$val] = isset($map[$val]) ? $map[$val] + 1 : 1;
}
$lowest = PHP_INT_MAX;
foreach ($map as $val) {
$lowest = min($lowest, $val);
}
for ($i = $lowest; $i >= 2; $i--) {
$flag = true;
foreach ($map as $val) {
if ($val % $i !== 0) {
$flag = false;
}
}
if ($flag) {
return true;
}
}
return false;
}
private static function helper(int $a, int $b): int
{
return $b > 0 ? self::helper($b, $a % $b) : $a;
}
}