Skip to content

Commit

Permalink
Add Go: 853-Car-Fleet.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushi Panchariya committed Oct 14, 2022
1 parent fd465a1 commit 74889c2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions go/853-Car-Fleet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
func carFleet(target int, position []int, speed []int) int {
pair := []carInfo{}
stack := []float32{}
for i, _ := range position {
pair = append(pair, carInfo{position[i], speed[i]})
}

sort.Slice(pair, func(i, j int) bool {
return pair[i].pos < pair[j].pos
})

for i := len(pair) - 1; i >= 0; i-- {
stack = append(stack, float32(target-pair[i].pos)/float32(pair[i].spd))
if len(stack) >= 2 && stack[len(stack)-1] <= stack[len(stack)-2] {
stack = stack[:len(stack)-1]
}
}
return len(stack)
}

type carInfo struct {
pos int
spd int
}

0 comments on commit 74889c2

Please sign in to comment.