-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsliding_window_max_from_array.rb
More file actions
48 lines (40 loc) · 1.05 KB
/
sliding_window_max_from_array.rb
File metadata and controls
48 lines (40 loc) · 1.05 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
# Given an array and an integer K, find the maximum for each and every contiguous subarray of size k.
# Examples :
# Input: arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}, K = 3
# Output: 3 3 4 5 5 5 6
# Explanation:
# Maximum of 1, 2, 3 is 3
# Maximum of 2, 3, 1 is 3
# Maximum of 3, 1, 4 is 4 and so on.
def heapify_max(arr, index)
left = index * 2 + 1
right = index * 2 + 2
max = index
length = arr.length
max = left if left < length && arr[left] > arr[max]
max = right if right < length && arr[right] > arr[max]
return if max == index
arr[max], arr[index] = arr[index], arr[max]
heapify_max(arr, max)
end
def build_heap(arr)
length = arr.length
start = length / 2 - 1
start.downto(0).each do |i|
heapify_max(arr, i)
end
arr
end
def max_sliding_window(nums, k)
length = nums.length
return [] if length < k
heap = build_heap(nums[0, k])
output = [heap[0]]
Range.new(1, length - k).each do |i|
output << build_heap(nums[i...i + k])[0]
end
output
end
ar = [1, 2, 3, 1, 4, 5, 2, 3, 6]
k = 3
puts max_sliding_window(ar, 3).to_s