-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicProgramming.go
144 lines (110 loc) · 2.79 KB
/
dynamicProgramming.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package algo
// max finds the greatest number from an array
func max(slice []int) int {
if len(slice) == 1 {
return slice[0]
}
maxRemainder := max(slice[1:])
if slice[0] > maxRemainder {
return slice[0]
}
return maxRemainder
}
func maxInefficient(slice []int) int {
if len(slice) == 1 {
return slice[0]
}
if slice[0] > maxInefficient(slice[1:]) {
return slice[0]
}
return maxInefficient(slice[1:])
}
func fibInefficient(n int) int {
if n == 0 || n == 1 {
return n
}
return fibInefficient(n-2) + fibInefficient(n-1)
}
// fibMemoization implements a recursive fibonacci function with memoization
// Because this is O(2N) - 1, we can set the size of the map we give:
// memo := make(map[int]int, 2*n-1)
func fibMemoization(num int, memo map[int]int) int {
if num == 0 || num == 1 {
return num
}
if _, recorded := memo[num]; !recorded {
memo[num] = fibMemoization(num-2, memo) + fibMemoization(num-1, memo)
}
return memo[num]
}
// fib also uses memoization and a closure to run recursion.
func fib(number int) int {
memo := make(map[int]int, 2*number-1)
var recurse func(n int, mem map[int]int) int
recurse = func(n int, mem map[int]int) int {
if n == 0 || n == 1 {
return n
}
if _, recorded := mem[n]; !recorded {
mem[n] = recurse(n-2, mem) + recurse(n-1, mem)
}
return mem[n]
}
return recurse(number, memo)
}
func fibBottomUp(n int) int {
if n <= 1 {
return n
}
a, b := 0, 1
for i := 2; i <= n; i++ {
next := a + b
a = b
b = next
}
return b
}
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Exercises
// addUntil100 returns the sum of an []int, ignoring the number
// if adding it to the current sum exceeds 100.
func addUntil100(ints []int) int {
if len(ints) == 0 {
return 0
}
sumOfRemainders := addUntil100(ints[1:])
if ints[0]+sumOfRemainders > 100 {
return sumOfRemainders
}
return ints[0] + sumOfRemainders
}
// golomb calculates the nth number from a "Golomb Sequence"
// https://en.wikipedia.org/wiki/Golomb_sequence
func golomb(number int) int {
memo := make(map[int]int)
var recurse func(n int, mem map[int]int) int
recurse = func(n int, mem map[int]int) int {
if n == 1 {
return 1
}
if _, recorded := mem[n]; !recorded {
mem[n] = 1 + recurse(n-recurse(recurse(n-1, mem), mem), mem)
}
return mem[n]
}
return recurse(number, memo)
}
func uniquePathsMemo(rows, columns int) int {
memo := make(map[[2]int]int)
var recurse func(r, c int, mem map[[2]int]int) int
recurse = func(r, c int, mem map[[2]int]int) int {
if r == 1 || c == 1 {
return 1
}
if _, recorded := mem[[2]int{r, c}]; !recorded {
mem[[2]int{r, c}] = recurse(r-1, c, mem) + recurse(r, c-1, mem)
}
return mem[[2]int{r, c}]
}
return recurse(rows, columns, memo)
}