Test Failed
Push — master ( 09d5fd...972873 )
by Jinyun
04:54
created

SlidingWindowMaximum   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
eloc 45
c 1
b 0
f 0
dl 0
loc 98
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B maxSlidingWindow3() 0 17 7
B maxSlidingWindow2() 0 21 9
A maxSlidingWindow() 0 17 6
B maxSlidingWindow4() 0 19 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class SlidingWindowMaximum
8
{
9
    /**
10
     * Note: Time Limit Exceeded.
11
     *
12
     * @param array $nums
13
     * @param int $k
14
     *
15
     * @return array
16
     */
17
    public static function maxSlidingWindow(array $nums, int $k): array
18
    {
19
        if (empty($nums) || $k <= 0) {
20
            return [];
21
        }
22
        $ans = [];
23
        for ($i = $k - 1, $n = count($nums); $i < $n; $i++) {
24
            $max = $nums[$i];
25
            for ($j = 1; $j < $k; $j++) {
26
                if ($nums[$i - $j] > $max) {
27
                    $max = $nums[$i - $j];
28
                }
29
            }
30
            $ans[$i - $k + 1] = $max;
31
        }
32
33
        return $ans;
34
    }
35
36
    /**
37
     * Note: Time Limit Exceeded.
38
     *
39
     * @param array $nums
40
     * @param int $k
41
     *
42
     * @return array
43
     */
44
    public static function maxSlidingWindow2(array $nums, int $k): array
45
    {
46
        $ans = [];
47
        if (empty($nums) || $k <= 0) {
48
            return $ans;
49
        }
50
        $queue = [];
51
        for ($i = 0, $n = count($nums); $i < $n; $i++) {
52
            if (!empty($queue) && current($queue) === $i - $k) {
53
                array_shift($queue);
54
            }
55
            while (!empty($queue) && $nums[$queue[count($queue) - 1]] < $nums[$i]) {
56
                array_pop($queue);
57
            }
58
            $queue[] = $i;
59
            if ($i >= $k - 1) {
60
                $ans[] = $nums[current($queue)];
61
            }
62
        }
63
64
        return $ans;
65
    }
66
67
    public static function maxSlidingWindow3(array $nums, int $k): array
68
    {
69
        if (empty($nums) || $k <= 0) {
70
            return [];
71
        }
72
        [$ans, $queue] = [[], new \SplMaxHeap()];
73
        foreach ($nums as $i => $num) {
74
            while (!$queue->isEmpty() && $queue->top()[1] <= $i - $k) {
75
                $queue->extract();
76
            }
77
            $queue->insert([$num, $i]);
78
            if ($i >= $k - 1) {
79
                array_push($ans, $queue->top()[0]);
80
            }
81
        }
82
83
        return $ans;
84
    }
85
86
    public static function maxSlidingWindow4(array $nums, int $k): array
87
    {
88
        if (empty($nums) || $k <= 0) {
89
            return [];
90
        }
91
92
        [$ans, $queue] = [[], new \SplPriorityQueue()];
93
        $queue->setExtractFlags($queue::EXTR_BOTH);
94
        foreach ($nums as $i => $num) {
95
            while (!$queue->isEmpty() && $queue->top()['data'][1] <= $i - $k) {
96
                $queue->extract();
97
            }
98
            $queue->insert([$num, $i], $num);
99
            if ($i >= $k - 1) {
100
                array_push($ans, $queue->top()['data'][0]);
101
            }
102
        }
103
104
        return $ans;
105
    }
106
}
107