Last active
January 29, 2022 01:53
-
-
Save miguelraz/b2cef9124f6bd64f831b14bc2a61e4ca to your computer and use it in GitHub Desktop.
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
julia> function sol(arr) | |
# Bail early if too short | |
length(arr) < 2 && return 0 | |
# Idea - normalize to positive ints and use RadixSort | |
# then take the maximum of the diff | |
mini = minimum(arr) | |
# Use ternary operator `?` and broadcasted in-place addition `.+=` | |
mini <= 0 ? arr .+= abs(mini) : nothing | |
# Use in-place RadixSort to modify `arr` | |
sort!(arr, alg = RadixSort) | |
# Take the max of the diff | |
maximum(diff(arr)) | |
end | |
sol (generic function with 1 method) | |
julia> sol([3,6,9,1]) | |
3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment