-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution509.go
More file actions
39 lines (32 loc) · 820 Bytes
/
solution509.go
File metadata and controls
39 lines (32 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package solution509
// ============================================================================
// 509. Fibonacci Number
// URL: https://leetcode.com/problems/fibonacci-number/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/509---Fibonacci-Number
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_fib
Benchmark_fib-24 88254508 16.30 ns/op 0 B/op 0 allocs/op
PASS
*/
var cache = map[int]int{1: 1}
func fib(n int) int {
if n <= 1 {
return n
}
v1, ok1 := cache[n-1]
v2, ok2 := cache[n-2]
if ok2 && ok1 {
return v1 + v2
} else if !ok2 && ok1 {
cache[n] = v1 + fib(n-2)
} else if ok2 {
cache[n] = fib(n-1) + v2
} else {
cache[n] = fib(n-1) + fib(n-2)
}
return cache[n]
}