This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
func Benchmark_tabulation_stairs(b *testing.B) { | |
n := 1000 | |
b.ReportAllocs() | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tabulation | |
func tabulation_getminstairs(n int) int { | |
table := make([]int, n+1) | |
for i := 2; i <= n; i++ { | |
if (i%2 == 0) && i%3 != 0 { | |
table[i] = 1 + int(math.Min(float64(table[i-1]), float64(table[i/2]))) | |
} else if (i%3 == 0) && i%2 != 0 { | |
table[i] = 1 + int(math.Min(float64(table[i-1]), float64(table[i/3]))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
func Benchmark_tabulation_knapsack(b *testing.B) { | |
capacity := 50 | |
value := []int{60, 100, 120} | |
weight := []int{10, 20, 30} | |
n := 3 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tabulation | |
func tabulation_knapsack(weight, value []int, capacity, n int) int { | |
table := make([][]int, n+1) | |
for i := 0; i <= n; i++ { | |
table[i] = make([]int, capacity+1) | |
} | |
for i := 0; i <= n; i++ { | |
for j := 0; j <= capacity; j++ { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
) | |
func Benchmark_fibonacci(b *testing.B) { | |
b.ReportAllocs() | |
for i:=0; i<b.N;i++{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Recursion | |
func fibonacci(n int) int { | |
if n <= 2 { | |
return 1 | |
} | |
return fibonacci(n-1) + fibonacci(n-2) | |
} | |
// Slice Append - Dynamic | |
func fibDynamic(n int) int { |