-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmax_slice.rb
More file actions
62 lines (55 loc) · 1.25 KB
/
max_slice.rb
File metadata and controls
62 lines (55 loc) · 1.25 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
60
61
62
a = [5, -7, 3, 5, -2, 4, -1]
def prefix_sum(a)
p = [0]
(1..a.size).each do |k|
p[k] = p[k - 1] + a[k - 1]
end
p
end
# p3 = a0 + a1 + a2
# p4 = a0 + a1 + a2 + a3
# p5 = a0 + a1 + a2 + a3 + a4
# p6 = a0 + a1 + a2 + a3 + a4 + a5
# sum between index x and y
# x = 3, y = 5
# p6 - p3
# p[y + 1] - p[x]
# O(n * n) - # with prefix sums
def slow_max_slice(a)
max_slice = 0
prefix = prefix_sum(a)
a.size.times do |p|
for q in p..(a.size - 1)
sum = prefix[q + 1] - prefix[p]
max_slice = [max_slice, sum].max
end
end
max_slice
end
# O(n * n) --- w/o prefix sums
def slow_maximal_slice(a)
max_slice = 0
a.size.times do |p|
sum = 0
for q in p..(a.size - 1)
sum += a[q]
max_slice = [max_slice, sum].max
end
end
max_slice
end
# O(n)
def fast_max_slice(a)
max_slice = 0 # max sum of all slices so far
# max ending compares max slice vs. max slice + current element
# Do we let current element into max_slice sum or not?
# We compare to 0 because if num makes max_ending < 0,
# we just ignore all numbers in array up to num! Start fresh.
max_ending = 0
a.each do |num|
max_ending = [0, max_ending + num].max
max_slice = [max_slice, max_ending].max
end
max_slice
end
p fast_max_slice(a)