Count obstacle-free grid paths
Return the number of paths from the top-left to bottom-right of a rows by cols grid moving only right or down. A zero dimension returns zero.
counts = [1, 1, 1]
for column in range(1, len(counts)):
counts[column] += counts[column-1]
print(counts)Reason about the state
Each cell receives paths from above and left. Reuse one row of counts so memory is proportional to the number of columns. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the number of paths from the top-left to bottom-right of a rows by cols grid moving only right or down. A zero dimension returns zero.
The explanation opens here once every check passes.