-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
delannoy_number.jl
52 lines (42 loc) · 1.44 KB
/
delannoy_number.jl
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
"""
Julia program to find the Delannoy number for a given rectangular grid.
A Delannoy number describes the number of paths from the southwest corner
(0, 0) of a rectangular grid to the northeast corner (m, n) using only single steps north, northeast, or east.
Delannoy number is named after the French mathematican Henri Delannoy.
"""
function delannoy_number(r, c)
# Initialize the dp array with '0' as value.
dp = zeros(Int, c + 1, r + 1)
for i in 1:c
dp[1,i] = 1
end
for i in 2:c+1
dp[i,1] = 1
end
# From Each point calculate the number of paths, to the north-east point, that can be reached by
# traversing through the immediate right or immediate top or immediate top-right point.
for i in 2:c+1
for j in 2:r+1
dp[i,j] = dp[i-1,j] + dp[i-1,j-1] + dp[i,j-1]
end
end
return dp[c+1,r+1]
end
print("Enter the co-ordinates of the north-east corner.\nEnter the row co-ordinate: ")
x = readline()
x = parse(Int, x)
print("Enter the column co-ordinate: ")
y = readline()
y = parse(Int, y)
res = delannoy_number(x, y)
print("The Delannoy number of the given grid is $res.")
"""
Time Complexity - O(x * y), where `x`, `y` is the given co-ordinates.
Space Complexity - O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE I
Enter the co-ordinates of the north-east corner.
Enter the row co-ordinate: 5
Enter the column co-ordinate: 6
The Delannoy number of the given grid is 3653.
"""