Data Structures & AlgorithmsHardICPC regional · grid DP
Problem: How many monotone lattice paths from (0,0) to (2,2) exist on a 3 x 3 grid if the moves are right, down or diagonal (down-right)? Run the DP.
Problem: How many monotone lattice paths from (0,0) to (2,2) exist on a 3 x 3 grid if the moves are right, down or diagonal (down-right)? Run the DP.
dp = [[0] * 3 for _ in range(3)]
for i in range(3):
for j in range(3):
if i == 0 and j == 0:
dp[i][j] = 1
continue
v = 0
if i:
v += dp[i - 1][j]
if j:
v += dp[i][j - 1]
if i and j:
v += dp[i - 1][j - 1]
dp[i][j] = v
print(dp[2][2])What is printed?- A. 6
- B. 12
- C. 13
- D. 20
Correct answer: C. 13
With diagonal moves allowed, the count is the Delannoy number D(2,2) = 13. Ignoring the diagonal move gives the trap 6.
More Coding Interview Problems questions
- Problem: What is the sum of i (5 - i) for i from 1 to 4? Run the code. python print(sum(i (5 - i) for i in range(1, 5))) What is printed?
- Problem: What is the sum of gcd(i, j) over all pairs 1 <= i < j <= 4? Run the code. python from math import gcd print(sum(gcd(i, j) for i…
- Problem: What is the digit sum of 2^100? Run the code. python print(sum(int(c) for c in str(2 100))) What is printed?
- Problem: How many triples (a, b, c) with 1 <= a <= b <= c <= 10 have a + b + c = 12? Run the code. python print(sum(1 for a in range(1…
- Problem: What is the largest prime factor of 600851475143? Run the trial-division code. python n = 600851475143 f = 2 last = 1 while f f…
- Problem: How many integers in [1, 999] are divisible by 7 but not by 14? Run the code. python print(sum(1 for x in range(1, 1000) if x % 7…