From the course: Advanced Go Programming: Data Structures, Code Architecture, and Testing

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Solution: Counting paths

Solution: Counting paths

(upbeat music) - [Instructor] Let's have a look at my solution to the counting paths with a given cost challenge. I haven't modified any of the provided code, only changing the countPaths function to use dynamic programming. You can see the solution on line 60 to 100. We will be working with a recursive function so begin by checking the base cases first. On lines 62 to 64, check whether the cost is negative. In this case, we've exceeded the provided cost and we haven't found a valid path. We return zero in this case. Next, on lines 67 to 69, try to fetch a pre-computed path count from the maze if this subproblem has been solved already. If a value is found as indicated by the returned OK value, simply return the fetched value and spare all later recomputation. Next, on line 71 to 80, first retrieve the cost of the current element, then handle the case that the destination has been reached. If we've reached the…

Contents