Created
January 25, 2022 08:28
-
-
Save thanhpp/5bfc13f9695db0f6b759f10fc42f7321 to your computer and use it in GitHub Desktop.
teko_test2.go
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
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