feat: progress

This commit is contained in:
2026-05-23 16:12:24 -04:00
parent 2ca8bb4427
commit dc373fecb5
9 changed files with 660 additions and 11 deletions

View File

@@ -6,6 +6,13 @@ import (
func MaxSlidingWindow(nums []int, k int) []int {
h := heap.NewBy(heap.More, nums[:k]...)
result := []int{h.Data[0]}
result := []int{h.Top()}
for i := range nums[k:] {
h.Drop(nums[i])
h.Put(nums[i+k])
result = append(result, h.Top())
}
return result
}

View File

@@ -0,0 +1,65 @@
package maximum_sliding_window
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMaxSlidingWindow(t *testing.T) {
tests := []struct {
name string
nums []int
k int
want []int
}{
{
name: "example 1",
nums: []int{1, 3, -1, -3, 5, 3, 6, 7},
k: 3,
want: []int{3, 3, 5, 5, 6, 7},
},
{
name: "example 2",
nums: []int{1},
k: 1,
want: []int{1},
},
{
name: "k equals length",
nums: []int{4, 2, 7, 1},
k: 4,
want: []int{7},
},
{
name: "all same",
nums: []int{3, 3, 3, 3},
k: 2,
want: []int{3, 3, 3},
},
{
name: "descending",
nums: []int{5, 4, 3, 2, 1},
k: 3,
want: []int{5, 4, 3},
},
{
name: "ascending",
nums: []int{1, 2, 3, 4, 5},
k: 3,
want: []int{3, 4, 5},
},
{
name: "negatives",
nums: []int{-5, -3, -1, -2, -4},
k: 2,
want: []int{-3, -1, -1, -2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, MaxSlidingWindow(tt.nums, tt.k))
})
}
}