Skip to content

Commit efd4b1d

Browse files
committed
Added Fibonacci, GameScoring
1 parent 5d41b12 commit efd4b1d

File tree

11 files changed

+248
-3
lines changed

11 files changed

+248
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

DynamicProgramming/Fibonacci.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Fibonacci sequence using recursion and Memoization
3+
Returns the value of the nth Fibonacci number
4+
*/
5+
6+
package main
7+
8+
import ("fmt")
9+
10+
// Recursion
11+
func FibonacciRecursion(n int) int {
12+
if n == 0 || n == 1 {
13+
return 1
14+
}
15+
return FibonacciRecursion(n-1) + FibonacciRecursion(n-2)
16+
}
17+
18+
// Memoization can be done using hashmap or also by using an array
19+
func FibonacciMemoization(n int) int {
20+
var MemoizedArray = []int{1,1}
21+
if n == 0 || n == 1 {
22+
return 1
23+
}
24+
for i:=2;i<n+1;i++ {
25+
MemoizedArray = append(MemoizedArray,MemoizedArray[i-2]+MemoizedArray[i-1])
26+
}
27+
return MemoizedArray[n]
28+
}
29+
30+
func main() {
31+
n := 10
32+
valRecursion := FibonacciRecursion(n)
33+
valMemoization := FibonacciMemoization(n)
34+
fmt.Println(fmt.Sprintf("Fibonacci term number %d from recusion method is:%d",n,valRecursion))
35+
fmt.Println(fmt.Sprintf("Fibonacci term number %d from memoization method is:%d",n,valMemoization))
36+
}

DynamicProgramming/GameScoring.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Find the total number of ways a player can score n runs if he can only score runs from an array in a given shot
3+
This is a case of dynamic programming and can be done using two methods:
4+
1) Memoization using a hash map
5+
2) Bottom up using an array
6+
This class of problem can be extended to many other problems. This is a very important pattern.
7+
*/
8+
9+
package main
10+
11+
import ("fmt")
12+
13+
// Recursive Memoization
14+
func GameScoringMemoization(Total int, PossibleRuns []int, MemoizedMap map[int]int) int {
15+
if _,ok := MemoizedMap[Total]; ok {
16+
return MemoizedMap[Total]
17+
} else if Total < 0 {
18+
return 0
19+
} else {
20+
NumWays := 0
21+
for _, val := range PossibleRuns {
22+
NumWays += GameScoringMemoization(Total - val, PossibleRuns, MemoizedMap)
23+
}
24+
MemoizedMap[Total] = NumWays
25+
}
26+
return MemoizedMap[Total]
27+
}
28+
29+
// This approach uses an array to store the result and build on it. Returns the total number of ways
30+
func GameScoringBottomUp(Total int, PossibleRuns []int) int {
31+
var MemoizedArray = []int{1}
32+
// We iterate for i from 0 to Total and find the number of total number of ways to get to each step
33+
// We consider that the number of ways to get to 0 is 1
34+
for tot:=1;tot<Total+1;tot++ {
35+
NumWays := 0
36+
for _, val := range PossibleRuns {
37+
if (tot - val) >= 0 {
38+
NumWays += MemoizedArray[tot - val]
39+
} else {
40+
continue
41+
}
42+
}
43+
MemoizedArray = append(MemoizedArray,NumWays)
44+
}
45+
return MemoizedArray[Total]
46+
}
47+
48+
func main() {
49+
PossibleRuns := []int{5,6,7}
50+
Total := 11
51+
52+
53+
// Bottom up Approach
54+
NumWaysBottomUp := GameScoringBottomUp(Total, PossibleRuns)
55+
fmt.Println(fmt.Sprintf("Number of ways of getting to %d with possible runs %v and BottomUp Approach is %d",Total,PossibleRuns,NumWaysBottomUp))
56+
57+
58+
// Create a MemoizedMap and pass it to the Memoization Function
59+
var MemoizedMap = make(map[int]int)
60+
MemoizedMap[0] = 1
61+
NumWaysMemoization := GameScoringMemoization(Total, PossibleRuns, MemoizedMap)
62+
fmt.Println(fmt.Sprintf("Number of ways of getting to %d with possible runs %v and Memoization Approach is %d",Total,PossibleRuns,NumWaysMemoization))
63+
64+
}

DynamicProgramming/KnapSack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func KnapSackRecursion(ItemWeight,ItemValue []int,n,c int) int {
2424
}
2525
}
2626

27-
// Memoized version of the problem, which minimizes recusrsion
27+
// Memoized version of the problem, which minimizes recursion
2828
func KnapSackMemoization(MemoizedMap map[string]int,ItemWeight,ItemValue []int,n,c int) int {
2929
nc := fmt.Sprintf("%d:%d",n,c)
3030
if val,ok := MemoizedMap[nc];ok {

MathAndStats/AllSubset.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Find all the subsets of a given set
3+
Two methods will be discussed here
4+
1) Using the bit representation approach
5+
2) Using a recursion tree type of approach
6+
*/
7+
8+
package main
9+
10+
import ("fmt"
11+
"math"
12+
)
13+
14+
func GetBit(num int,bit uint) uint {
15+
temp := 1 << bit
16+
temp = temp & num
17+
if temp == 0 {
18+
return 0
19+
} else {
20+
return 1
21+
}
22+
}
23+
24+
func AllSubsetsBit(array []int) *[][]int {
25+
var SubsetsArray [][]int
26+
var ArrayLen uint = uint(len(array))
27+
NumSubsets := math.Pow(float64(2),float64(len(array)))
28+
for i:=0;i<int(NumSubsets);i++ {
29+
var SubArray []int
30+
for j:=uint(0);j<ArrayLen;j++ {
31+
bit := GetBit(i,j)
32+
if bit == 1 {
33+
SubArray = append(SubArray,array[j])
34+
}
35+
}
36+
SubsetsArray = append(SubsetsArray,SubArray)
37+
}
38+
return &SubsetsArray
39+
}
40+
41+
/*
42+
func AllSubsetRecursion(array []int)
43+
*/
44+
45+
func main() {
46+
array := []int{2,3,4}
47+
AllSubsetsBitPtr := AllSubsetsBit(array)
48+
//AllSubsetsBit(array)
49+
fmt.Println(*AllSubsetsBitPtr)
50+
}

MathAndStats/FindKthPermutation.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ func FindKthPermutation(num []int,k int,) *[]int {
4444
return &KthPermutation
4545
}
4646

47-
4847
func main() {
4948
array := []int{1,2,3,4}
5049
KthPermutationPtr := FindKthPermutation(array,8)

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# AlgorithmsGolang
1+
# AlgorithmsGolang
2+
3+
I have created this repository to provide those important patterns that will help people to prepare for interviews.
4+
5+
The algorithms here are not comprehensive and the repo is still a work in progress
6+

arrays/MergeOverlappingIntervals.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Merge overlapping intervals
3+
List of intervals is given in sorted order and we need to merge the overlapping intervals to give a larger interval
4+
If the intervals are not in sorted order we need to sort them first and then we can use the below approach
5+
Input: {{1,5},{3,7},{4,6},{6,8},{10,12},{11,15}}
6+
Output: {{1,8},{10,15}}
7+
*/
8+
9+
package main
10+
11+
import ("fmt"
12+
)
13+
14+
// Defining a tuple type to hold the intervals as go doesn't have a tuple by default
15+
type tuple struct {
16+
first,second interface{}
17+
}
18+
19+
func MergeOverlappingIntervals(arr *[]tuple) *[]tuple {
20+
outputArray := []tuple
21+
if len(*arr) == 0 {
22+
return arr
23+
}
24+
for _,val := range *arr {
25+
26+
}
27+
28+
}
29+
30+
func main() {
31+
array := []tuple{{1,5},{3,7},{4,6},{6,8},{10,12},{11,15}}
32+
fmt.Println(array)
33+
MergeOverlappingIntervals(&array)
34+
35+
}

linkedList/deleteNodeLinkedList.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Delete a node in a linkedlist with a given key (All the occurances)
3+
*/
4+
5+
package main
6+
7+
import ("fmt"
8+
"AlgorithmsGolang/LinkedList/LinkedList"
9+
)
10+
11+
func main() {
12+
13+
array := []interface{}{1,3,5,2,70,21,34}
14+
ll := LinkedList.ArrayToLinkedList(&array)
15+
ll.Remove()
16+
}

linkedList/mergeTwoLinkedLists.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
This solution is for a question on LeetCode, This wouldn't work directly
8+
*/
9+
10+
package main
11+
12+
import ("AlgorithmsGolang/linkedList/LinkedList"
13+
)
14+
15+
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
16+
mll := new(ListNode)
17+
temp := mll
18+
for (l1 != nil && l2 != nil) {
19+
e := new(ListNode)
20+
if (l1.Val <= l2.Val) {
21+
e.Val = l1.Val
22+
l1 = l1.Next
23+
} else {
24+
e.Val = l2.Val
25+
l2 = l2.Next
26+
}
27+
temp.Next = e
28+
temp = temp.Next
29+
}
30+
if l1 == nil && l2 == nil {
31+
return nil
32+
} else if l1 == nil {
33+
temp.Next = l2
34+
} else {
35+
temp.Next = l1
36+
}
37+
return mll.Next
38+
}

0 commit comments

Comments
 (0)