Skip to content

Instantly share code, notes, and snippets.

@thanhpp
Created January 25, 2022 08:28
Show Gist options
  • Save thanhpp/5bfc13f9695db0f6b759f10fc42f7321 to your computer and use it in GitHub Desktop.
Save thanhpp/5bfc13f9695db0f6b759f10fc42f7321 to your computer and use it in GitHub Desktop.
teko_test2.go
package main
import (
"fmt"
)
func main() {
var (
input = []int{1, 2, 3, 4, 5, 6, 7}
k int = 4
)
var (
maxVal, maxIdx int
)
for i := 0; i < k; i++ {
maxVal, maxIdx = max(input)
if maxIdx == -1 {
panic("invalid k")
}
input = append(input[:maxIdx], input[maxIdx+1:]...)
}
fmt.Println(maxVal)
}
func max(input []int) (val int, index int) {
var (
max int = -1
maxIdx int = -1
)
for i := range input {
if input[i] > max {
max = input[i]
maxIdx = i
}
}
return max, maxIdx
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment