Skip to content

Commit

Permalink
day 12 recursive stride order doesn't matter, but for iterative dynam…
Browse files Browse the repository at this point in the history
…ic programming it does.
  • Loading branch information
vaskoz committed Sep 2, 2018
1 parent 3ac099d commit 83db7b8
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions day12/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "sort"
// UniqueClimbs calculates the number of possible ways to climb the steps.
// Assumes that we have the given number of possible strides.
// A stride is the number of steps possible.
// Assumes strides are in sorted order.
// Runs in exponential time.
func UniqueClimbs(steps int, strides []int) int {
if steps < 0 {
Expand All @@ -23,10 +22,12 @@ func UniqueClimbs(steps int, strides []int) int {
// UniqueClimbsDS doesn't use recursion and instead iterates using memoization of subproblem results.
// Time complexity is O(K*N) where K is the number of possible strides and N is the number of steps.
func UniqueClimbsDS(steps int, strides []int) int {
sort.Ints(strides)
copyStrides := make([]int, len(strides))
copy(copyStrides, strides)
sort.Ints(copyStrides)
stepData := make([]int, steps+1)
stepData[0] = 1
return uniqueClimbsDS(steps, strides, stepData)[len(stepData)-1]
return uniqueClimbsDS(steps, copyStrides, stepData)[steps]
}

func uniqueClimbsDS(steps int, strides, stepData []int) []int {
Expand Down

0 comments on commit 83db7b8

Please sign in to comment.