forked from LeetCode-in-Scala/LeetCode-in-Scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.scala
33 lines (29 loc) · 1.02 KB
/
Solution.scala
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
package g0201_0300.s0239_sliding_window_maximum
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Heap_Priority_Queue
// #Sliding_Window #Queue #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
// #2023_11_07_Time_1149_ms_(86.67%)_Space_83.5_MB_(46.67%)
object Solution {
def maxSlidingWindow(nums: Array[Int], k: Int): Array[Int] = {
val q = new java.util.ArrayDeque[(Int, Int)]()
def populate(num: Int, index: Int): Unit = {
while (!q.isEmpty && q.peekLast()._2 + k <= index) {
q.pollLast()
}
while (!q.isEmpty && (q.peekFirst()._1 <= num)) {
q.pollFirst()
}
q.addFirst((num, index))
}
if (k == 1) {
nums
} else {
for (i <- (0 until k - 1)) {
populate(nums(i), i)
}
((k - 1) until nums.size).map { i =>
populate(nums(i), i)
q.peekLast()._1
}.toArray
}
}
}