Skip to content

Commit

Permalink
dev: leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
n-ae committed Jan 11, 2024
1 parent 16fa815 commit 8c7eb9b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions leetcode/509.fibonacci-number.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
func fib(n int) int {
var result [31]int
result[0] = 0
result[1] = 1

if n > 1 {
result[n] = tailRec(n, [2]int{result[0], result[1]})
}

return result[n]
}

func tailRec(n int, window [2]int) int {
if n == 1 {
return window[1]
}

return tailRec(n-1, [2]int{window[1], window[0] + window[1]})
}

0 comments on commit 8c7eb9b

Please sign in to comment.