-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vector and Priority queue assert (#320)
* feat: hnsw * handle error * feat: vector and priority queue assert * check both vectors * fmt * remove vector.go
- Loading branch information
1 parent
0de05c0
commit c0a23a4
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
/* | ||
*/ |