Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function that takes a pair of indices for a 2D array and prints out the number at the provided indices where each number

Write a function that takes a pair of indices for a 2D array and prints out the number at the provided indices where each number is the sum of the value to the left and above itself. the first row and the first column are filled with 1s. So the value at (row, col) is (row-1, col) + (row,col-1) :(0,0) is 1, (1,1) is 2, (2,1) is 3, (5,3) is 56 and so on.

Currently, I have a O(m*n) solution, but I need to optimize this further. this is my O(m*n) solution using dynamic programming:

from functools import lru_cache

def uniquePaths(m: int, n: int): @lru_cache(maxsize=None) def dp(row, col): print(".") if row == 0 or col == 0: return 1

return dp(row - 1, col) + dp(row, col - 1)

return dp(m, n)

row = int(input('row? ')) col = int(input('col? '))

print(uniquePaths(row, col))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions

Question

9. Describe the characteristics of power.

Answered: 1 week ago

Question

10. Describe the relationship between communication and power.

Answered: 1 week ago