-
Notifications
You must be signed in to change notification settings - Fork 0
/
slide_window.rb
51 lines (46 loc) · 923 Bytes
/
slide_window.rb
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
# Find max sum of window size of k
require 'pry'
require 'benchmark'
def brute_force
max_sum = 0
array = Array(1..10000000)
k = 3
array.each_with_index do |_, index|
window_sum = 0
array[index...index + k].each do |j|
window_sum += j
end
if window_sum > max_sum
max_sum = window_sum
end
end
puts max_sum
end
def slide_window
array = Array(1..100000)
size = array.size
k = 3 # window size
i = 0 # window start pointer
j = 0 # window end pointer
sum = 0 # each window sum
max_sum = 0
while j < size
sum = sum + array[j] # sum of elements in window
window_size = j - i + 1
if window_size < k
j = j + 1
elsif window_size == k
max_sum = sum
sum = sum - array[i]
j = j + 1
i = i + 1
end
end
puts max_sum
end
Benchmark.bm do |x|
x.report { brute_force }
end
Benchmark.bm do |x|
x.report { slide_window }
end