Skip to content

Commit

Permalink
Vector and Priority queue assert (#320)
Browse files Browse the repository at this point in the history
* feat: hnsw

* handle error

* feat: vector and priority queue assert

* check both vectors

* fmt

* remove vector.go
  • Loading branch information
friendlymatthew authored Jun 3, 2024
1 parent 0de05c0 commit c0a23a4
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions pkg/hnsw/pq_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package hnsw

import (
"testing"
)

func TestPQ(t *testing.T) {

t.Run("bricks and ladders || min heap", func(t *testing.T) {
type Case struct {
heights []int
bricks int
ladders int
expected int
}

cases := [3]Case{
{
heights: []int{4, 2, 7, 6, 9, 14, 12},
bricks: 5,
ladders: 1,
expected: 4,
},
{
heights: []int{4, 12, 2, 7, 3, 18, 20, 3, 19},
bricks: 10,
ladders: 2,
expected: 7,
},
{
heights: []int{14, 3, 19, 3},
bricks: 17,
ladders: 0,
expected: 3,
},
}

for _, c := range cases {
res, err := furthestBuildings(c.heights, c.bricks, c.ladders)
if err != nil {
t.Fatal(err)
}

if res != c.expected {
t.Errorf("got %d, want %d", res, c.expected)
}
}

})
}

func furthestBuildings(heights []int, bricks, ladders int) (int, error) {

ladderJumps := NewBaseQueue(MinComparator{})

for idx := 0; idx < len(heights)-1; idx++ {
height := heights[idx]
nextHeight := heights[idx+1]

if height >= nextHeight {
continue
}

jump := nextHeight - height

ladderJumps.Insert(Id(idx), float32(jump))

if ladderJumps.Len() > ladders {
minLadderJump, err := ladderJumps.Peel()
if err != nil {
return -1, err
}

if bricks-int(minLadderJump.dist) < 0 {
return idx, nil
}

bricks -= int(minLadderJump.dist)
}
}

return len(heights) - 1, nil
}

/*
*/

0 comments on commit c0a23a4

Please sign in to comment.