-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path015-jake.rb
70 lines (60 loc) · 1.83 KB
/
015-jake.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@cached_latices = {[0, 0] => 1}
def lattice(rows, columns)
if rows == 0 || columns == 0
1
else
# Retrieve the "down_path" from cache or calculate it via recursion
if @cached_latices[[rows - 1, columns]]
down_path = @cached_latices[[rows - 1, columns]]
else
down_path = lattice(rows - 1, columns)
@cached_latices[[rows - 1, columns]] = down_path
end
# Retrieve the "right_path" from cache or calculate it via recursion
if @cached_latices[[rows, columns - 1]]
right_path = @cached_latices[[rows, columns - 1]]
else
right_path = lattice(rows, columns - 1)
@cached_latices[[rows, columns - 1]] = right_path
end
down_path + right_path
end
end
#
# Here is a shorter solution. It uses a some smart-ass syntax tricks, but
# should otherwise be readable. Key difference between implementations:
#
# A function memoizes *itself*. It does not care about the outcome of other
# function calls. This is the truly essential point that I think your soluition
# does not account for, which makes things far more complex.
#
# That said, if I were to truly approach this problem, I wouldn't use
# memoization. Instead, I'd try to walk diagonals (and note that this is really
# a Pascal's Triangle problem). By "walk diagonals" I mean calculate the value
# of one diagonal from the previous set. i.e.:
#
# a b c d
# b c d e
# c d e f
# d e f g
#
# That would involve seven function calls, each one examining the value of all
# points sharing the same letter.
#
# Anyway, I hope you find this useful.
#
@cache = {}
def lattice2(rows, cols)
@cache[[rows, cols]] ||= begin
if rows == 0 || cols == 0
1
else
lattice2(rows - 1, cols) + # up path
lattice2(rows, cols - 1) # left path
end
end
end
rows = 20
columns = 20
puts lattice(rows, columns)
puts lattice2(20, 20)