In Go 1.21, the standard library includes built-in max/min function, which can greatly simplify the code.
Looks good. One place I think the existing code is wrong.
Thanks. Modified.