Skip to content

Commit d4431ee

Browse files
committed
A bunch of edits
1 parent 1041fab commit d4431ee

File tree

8 files changed

+395
-4
lines changed

8 files changed

+395
-4
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Maximum Subsequence of Non Adjacent Elements
3+
4+
Example - [1,3,10,14,-5,-3,2,-2,3]
5+
Output - 22 from elements [3,14,2,3]
6+
7+
We will use dynamic programming to solve this problem
8+
*/
9+
10+
package main
11+
12+
import ("fmt")
13+
14+
func Max(a,b int) int {
15+
if a>=b {
16+
return a
17+
}
18+
return b
19+
}
20+
21+
func MaxSubsequenceNonAdjacent(array []int) int {
22+
var maxSoFarArray = make([]int,len(array))
23+
maxSoFarArray[0] = array[0]
24+
maxSoFarArray[1] = Max(maxSoFarArray[0],array[1])
25+
for idx,val := range array {
26+
if (idx == 0 || idx == 1) {
27+
continue
28+
} else {
29+
maxSoFarArray[idx] = Max(maxSoFarArray[idx-1],maxSoFarArray[idx-2]+val)
30+
}
31+
}
32+
return maxSoFarArray[len(array)-1]
33+
}
34+
35+
func main() {
36+
array := []int{1,6,10,14,-5,-1,2,-1,3}
37+
//array := []int{1,-1,6,-4,2,2}
38+
maxSubsequenceNonAdjacentSum := MaxSubsequenceNonAdjacent(array)
39+
fmt.Println("The Maximum sum of Non adjacent subsequence is:",maxSubsequenceNonAdjacentSum)
40+
}

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
# AlgorithmsGolang
22

3+
I started working on this repo as a way to showcase my skills in algorithms and data structures. During the process I realized that there are some common pattern which can be extended to many type of algorithms. Any problem can be converted into one of these base patterns.
4+
5+
The number of problems I have here is more than the base patterns.
6+
37
I have created this repository to provide those important patterns that will help people to prepare for interviews.
48

59
The algorithms here are not comprehensive and the repo is still a work in progress
610

11+
## Patterns
12+
13+
### Dynamic Programming
14+
- Fibonacci Problem
15+
- Frog Jumping Problem
16+
- Coin Change Problem
17+
- Game Scoring Problem
18+
19+
###

StackAndQueue/StackUsingQueue.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Implemet a Stack using Queues
3+
4+
Method 1: Use two Queues to implement a stack. PUSH:O() POP:O()
5+
6+
Method 2: Use two Queues to implement a stack. PUSH:O() POP:O()
7+
8+
"github.com/golang-collections/collections/stack"
9+
type Stack
10+
func New() *Stack
11+
func (this *Stack) Len() int
12+
func (this *Stack) Peek() interface{}
13+
func (this *Stack) Pop() interface{}
14+
func (this *Stack) Push(value interface{})
15+
16+
"github.com/golang-collections/go-datastructures/queue"
17+
type Queue
18+
func New(hint int64) *Queue
19+
func (q *Queue) Dispose()
20+
func (q *Queue) Disposed() bool
21+
func (q *Queue) Empty() bool
22+
func (q *Queue) Get(number int64) ([]interface{}, error)
23+
func (q *Queue) Len() int64
24+
func (q *Queue) Put(items ...interface{}) error
25+
func (q *Queue) TakeUntil(checker func(item interface{}) bool) ([]interface{}, error)
26+
27+
*/
28+
29+
package main
30+
31+
import ("github.com/golang-collections/go-datastructures/queue"
32+
"github.com/golang-collections/collections/stack"
33+
)
34+
35+
type StackUsingQueue struct {
36+
// Initialized
37+
}
38+
39+
func (*)
40+
41+
func main() {
42+
43+
}

arrays/maxInSlidingWindow.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ func maxInSlidingWindow(arr *[]int,windowSize int) {
6262
}
6363

6464
func main() {
65-
arr := []int{-4,2,-5,1,-1,6}
66-
67-
68-
maxInSlidingWindow(&arr,3)
65+
//arr := []int{-4,2,-5,1,-1,6}
66+
arr := []int{1,3,-1,-3,5,3,6,7}
67+
k := 3
68+
69+
maxInSlidingWindow(&arr,k)
6970

7071
//fmt.Println(dll)
7172
//fmt.Println(l.Front())

arrays/maxSingleSellProfit.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Find maximum single sell profit
3+
Given a list of stock prices for n days, find the maximum profit with a single buy/sell activity.
4+
5+
Description:
6+
Given a list of day stock prices (integers for simplicity), find the maximum single sell profit.
7+
8+
We need to maximize the single buy/sell profit and in case we can't make any profit, we'll try to minimize the loss.
9+
For below examples, buy and sell prices for making maximum profit are highlighted.
10+
*/
11+
12+
package main
13+
14+
import ("fmt"
15+
"math"
16+
)
17+
18+
type BuySell struct {
19+
buy,sell int
20+
}
21+
22+
func maxSingleSellProfit(array []int) *BuySell {
23+
currentBuy := array[0]
24+
globalSell := array[1]
25+
globalProfit := globalSell - currentBuy
26+
currentProfit := math.MinInt64
27+
var BS = new(BuySell)
28+
29+
for i:=1;i<len(array);i++ {
30+
currentProfit = array[i] - currentBuy
31+
if (currentProfit > globalProfit) {
32+
globalProfit = currentProfit
33+
globalSell = array[i]
34+
}
35+
36+
if (array[i] < currentBuy) {
37+
currentBuy = array[i]
38+
}
39+
}
40+
BS.buy = globalSell - globalProfit
41+
BS.sell = globalSell
42+
return BS
43+
}
44+
45+
46+
func main() {
47+
array := []int{21,12,11,9,6,3}
48+
//array := []int{12,5,9,19}
49+
BS := maxSingleSellProfit(array)
50+
fmt.Println("BuyPrice:",BS.buy,"SellPrice:",BS.sell)
51+
}

arrays/mergeSort.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Sorting an Array through Merge Sort
3+
*/
4+
5+
package main
6+
7+
import ("fmt")
8+
9+
func MergeSort(array []int) []int {
10+
11+
}
12+
13+
func main() {
14+
array := []int{7,2,4,9,10,1,3,1,24,12}
15+
sortedArray := MergeSort(array)
16+
fmt.Println(sortedArray)
17+
}

binarySearchTree/BST.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Implementation of a binary search tree operations (Insert, Search, Delete)
3+
*/
4+
5+
package main
6+
7+
import ("fmt"
8+
//"container/list"
9+
)
10+
11+
type Element struct {
12+
13+
// The value in stored in the element
14+
Value interface{}
15+
left, right *Element
16+
17+
// The binary search tree to which the element belongs
18+
bst *Bst
19+
}
20+
21+
type Bst struct {
22+
root Element
23+
}
24+
25+
// Creates a new Bst Element and initializes it
26+
func New() *Bst {
27+
return new(Bst).Init()
28+
}
29+
30+
func (b *Bst) Init() *Bst {
31+
b.root.right = nil
32+
b.root.left = nil
33+
return b
34+
}
35+
36+
func (b *Bst) Insert(val interface{}) {
37+
temp := &b.root
38+
for true {
39+
if (temp.Value == nil) {
40+
temp.Value = val
41+
temp.bst = b
42+
return
43+
} else if (val.(int) >= temp.Value.(int) && temp.right == nil) {
44+
temp.right = new(Element)
45+
temp.right.Value = val
46+
temp.right.bst = b
47+
return
48+
} else if (val.(int) < temp.Value.(int) && temp.left == nil) {
49+
temp.left = new(Element)
50+
temp.left.Value = val
51+
temp.left.bst = b
52+
return
53+
} else if (val.(int) >= temp.Value.(int) && temp.right != nil) {
54+
temp = temp.right
55+
} else {
56+
temp = temp.left
57+
}
58+
}
59+
}
60+
61+
// Returns if a value exists in the Binary Search Tree
62+
func (b *Bst) Search(val interface{}) bool {
63+
temp := &b.root
64+
for true {
65+
if (temp.Value.(int) == val.(int)) {
66+
return true
67+
} else if (val.(int) >= temp.Value.(int) && temp.right != nil) {
68+
temp = temp.right
69+
} else if (val.(int) < temp.Value.(int) && temp.left != nil) {
70+
temp = temp.left
71+
} else {
72+
return false
73+
}
74+
}
75+
return false
76+
}
77+
78+
func (b *Bst) Remove(val interface{}) {
79+
80+
}
81+
82+
// Inorder Traversal (Left, Root, Right)
83+
func (b *Bst) Inorder(root *Element) {
84+
if (root == nil) {
85+
return
86+
}
87+
b.Inorder(root.left)
88+
fmt.Println(root.Value)
89+
b.Inorder(root.right)
90+
}
91+
92+
func (b *Bst) Preorder(root *Element) {
93+
if (root == nil) {
94+
return
95+
}
96+
fmt.Println(root.Value)
97+
b.Preorder(root.left)
98+
b.Preorder(root.right)
99+
}
100+
101+
func (b *Bst) Postorder(root *Element) {
102+
if (root == nil) {
103+
return
104+
}
105+
b.Postorder(root.left)
106+
b.Postorder(root.right)
107+
fmt.Println(root.Value)
108+
}
109+
110+
func (b *Bst) Levelorder() {
111+
112+
}
113+
114+
/*
115+
func ArrayToBST(array []int) {
116+
117+
}
118+
*/
119+
120+
func main() {
121+
// Instantiate a random BST with the root element
122+
array := []int{5,10,12,7,6,21,35,45,22,100}
123+
b := New()
124+
for _, val := range array {
125+
b.Insert(val)
126+
}
127+
fmt.Println(b.Search(7))
128+
b.Inorder(&b.root)
129+
b.Preorder(&b.root)
130+
b.Postorder(&b.root)
131+
//fmt.Println(b.root)
132+
//fmt.Println(b.root.right)
133+
}

0 commit comments

Comments
 (0)