Skip to content

Commit

Permalink
Merge 886b3d3 into 36687a4
Browse files Browse the repository at this point in the history
  • Loading branch information
dalmirdasilva committed Jan 20, 2021
2 parents 36687a4 + 886b3d3 commit 18eca22
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -10,3 +10,5 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
go.mod
.idea
19 changes: 19 additions & 0 deletions day2/problem.go
Expand Up @@ -16,6 +16,25 @@ func ProductsExceptI(arr []int) []int {
return result
}

// ProductsExceptINoDivisionLinear returns an array with the products of all numbers
// except the value at the i-th position. Without using division.
// O(N) time complexity.
// O(1) space complexity.
func ProductsExceptINoDivisionLinear(arr []int) []int {
result := make([]int, len(arr))
total := 1
for i := 0; i < len(arr); i++ {
result[i] = total
total *= arr[i]
}
total = 1
for i := len(arr) - 1; i >= 0; i-- {
result[i] *= total
total *= arr[i]
}
return result
}

// ProductsExceptINoDivision returns an array with the products of all numbers
// except the value at the i-th position. Without using division.
// O(N^2) time complexity.
Expand Down
15 changes: 15 additions & 0 deletions day2/problem_test.go
Expand Up @@ -21,6 +21,14 @@ func TestProductsExceptI(t *testing.T) {
}
}
})
t.Run("ProductsExceptINoDivisionLinear", func(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := ProductsExceptINoDivisionLinear(tc.input); !reflect.DeepEqual(result, tc.expected) {
t.Error("didn't get what I expected")
}
}
})
t.Run("ProductsExceptINoDivision", func(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
Expand All @@ -39,6 +47,13 @@ func BenchmarkProductsExceptI(b *testing.B) {
}
}
})
b.Run("ProductsExceptINoDivisionLinear", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
ProductsExceptINoDivisionLinear(tc.input)
}
}
})
b.Run("ProductsExceptINoDivision", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
Expand Down
14 changes: 8 additions & 6 deletions day3/problem.go
Expand Up @@ -30,22 +30,24 @@ func Serialize(root *Node) string {
return strings.Join(serialized, " ")
}

func preorderDeserialize(serialized []string) *Node {
func preorderDeserialize(serialized []string) (*Node, int) {
if len(serialized) == 0 || serialized[0] == "-1" {
return nil
return nil, 1
}
v, err := strconv.Atoi(serialized[0])
if err != nil {
panic("bad serialized data cannot be deserialized")
}
node := &Node{val: v}
node.left = preorderDeserialize(serialized[1:])
node.right = preorderDeserialize(serialized[2:])
return node
advanceLeft, advanceRight := 0, 0
node.left, advanceLeft = preorderDeserialize(serialized[1:])
node.right, advanceRight = preorderDeserialize(serialized[1+advanceLeft:])
return node, 1 + advanceLeft + advanceRight
}

// Deserialize converts a string into a general binary tree.
func Deserialize(data string) *Node {
serialized := strings.Split(data, " ")
return preorderDeserialize(serialized)
result, _ := preorderDeserialize(serialized)
return result
}
72 changes: 47 additions & 25 deletions day3/problem_test.go
@@ -1,19 +1,41 @@
package day3

import (
// "log"
"testing"
)

func TestSerializeAndDeserialize(t *testing.T) {
t.Parallel()
root := &Node{val: 20,
left: nil,
right: &Node{val: 8, left: nil,
right: &Node{val: 10, left: nil,
right: &Node{val: 5}}},
root := &Node{
val: 20,
left: &Node{
val: 5,
left: &Node{
val: 10,
left: nil,
right: &Node{
val: 5,
left: nil,
right: nil,
},
},
right: nil,
},
right: &Node{
val: 8,
left: nil,
right: &Node{
val: 10,
left: nil,
right: &Node{
val: 5,
left: nil,
right: nil,
},
},
},
}
expected := "20 -1 8 -1 10 -1 5 -1 -1"
expected := "20 5 10 -1 5 -1 -1 -1 8 -1 10 -1 5 -1 -1"
var result string
if result = Serialize(root); result != expected {
t.Errorf("Expected %v but got %v", expected, result)
Expand Down Expand Up @@ -49,21 +71,21 @@ func TreeEquality(t1, t2 *Node) bool {
}

// inorder is a method to debug your inorder traversal of the tree
//func inorder(n *Node) {
//if n == nil {
//return
//}
//if n.left != nil {
//log.Println("going left")
//} else {
//log.Println("nothing to the left")
//}
//inorder(n.left)
//log.Println(n.val)
//if n.right != nil {
//log.Println("going right")
//} else {
//log.Println("nothing to the right")
//}
//inorder(n.right)
//}
// func inorder(n *Node) {
// if n == nil {
// return
// }
// if n.left != nil {
// log.Println("going left")
// } else {
// log.Println("nothing to the left")
// }
// inorder(n.left)
// log.Println(n.val)
// if n.right != nil {
// log.Println("going right")
// } else {
// log.Println("nothing to the right")
// }
// inorder(n.right)
// }

0 comments on commit 18eca22

Please sign in to comment.