Skip to content

Commit

Permalink
chore: add problem down to zero
Browse files Browse the repository at this point in the history
  • Loading branch information
leometzger committed Aug 13, 2023
1 parent b85bce3 commit 203d4f6
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
71 changes: 71 additions & 0 deletions datastructures/down_to_zero_ii.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package datastructures

// https://www.hackerrank.com/challenges/down-to-zero-ii/problem

// !! Needs optimization to pass all tests !!

type DownToZeroQueue struct {
items []*DownToZeroItem
}

type DownToZeroItem struct {
value int32
step int32
}

func (q *DownToZeroQueue) isEmpty() bool { return len(q.items) == 0 }
func (q *DownToZeroQueue) enqueue(value *DownToZeroItem) { q.items = append(q.items, value) }
func (q *DownToZeroQueue) dequeue() *DownToZeroItem {
if len(q.items) == 0 {
return nil
}
result := q.items[0]
q.items = q.items[1:]
return result
}

func downToZero(n int32) int32 {
visited := make(map[int32]bool)
queue := &DownToZeroQueue{}
queue.enqueue(&DownToZeroItem{
value: 1,
step: 1,
})

for !queue.isEmpty() {
item := queue.dequeue()
if visited[item.value] {
continue
}

visited[item.value] = true

if item.value+1 == n {
return item.step + 1
}

if item.value+1 < n {
queue.enqueue(&DownToZeroItem{
value: item.value + 1,
step: item.step + 1,
})
}

for i := 2; i <= int(item.value); i++ {
if i*int(item.value) > int(n) {
break
}

if item.value*int32(i) == n {
return item.step + 1
}

queue.enqueue(&DownToZeroItem{
value: item.value * int32(i),
step: item.step + 1,
})
}
}

return 1
}
72 changes: 72 additions & 0 deletions datastructures/down_to_zero_ii_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package datastructures

import (
"testing"

"github.com/stretchr/testify/assert"
)

type DownToZeroIITestCase struct {
n int32
result int32
}

func TestDownToZeroII(t *testing.T) {
tests := []DownToZeroIITestCase{
{n: 3, result: 3},
{n: 4, result: 3},
{n: 10, result: 5},
{n: 266574, result: 8},
{n: 603900, result: 7},
{n: 847224, result: 7},
{n: 484982, result: 9},
{n: 937693, result: 8},
{n: 596249, result: 9},
{n: 991347, result: 9},
{n: 872132, result: 9},
{n: 386794, result: 8},
{n: 302076, result: 9},
{n: 51784, result: 9},
{n: 373072, result: 9},
{n: 580459, result: 9},
{n: 894812, result: 9},
{n: 42386, result: 9},
{n: 633287, result: 10},
{n: 309619, result: 9},
{n: 549380, result: 8},
{n: 667351, result: 10},
{n: 314003, result: 9},
{n: 855076, result: 9},
{n: 472574, result: 11},
{n: 332264, result: 8},
{n: 299566, result: 8},
{n: 344188, result: 10},
{n: 556652, result: 8},
{n: 226615, result: 10},
{n: 802657, result: 9},
{n: 5, result: 4},
{n: 10, result: 5},
{n: 5, result: 4},
{n: 1, result: 1},
{n: 1, result: 1},
{n: 10, result: 5},
{n: 10, result: 5},
{n: 7, result: 5},
{n: 8, result: 4},
{n: 8, result: 4},
{n: 8, result: 4},
{n: 4, result: 3},
{n: 1, result: 1},
{n: 2, result: 2},
{n: 4, result: 3},
{n: 4, result: 3},
{n: 9, result: 4},
{n: 2, result: 2},
{n: 9, result: 4},
{n: 3, result: 3},
}

for _, test := range tests {
assert.Equal(t, test.result, downToZero(test.n))
}
}

0 comments on commit 203d4f6

Please sign in to comment.