LeetCode 670. Maximum Swap in F#
URL
https://leetcode.com/problems/maximum-swap/description/?envType=daily-question&envId=2024-10-17
Code
let maximumSwap (num: int) : int =
let rec maximumSwap' i (s: (int * char)[]) =
if i >= s.Length then
num
else
let digit = snd s.[i]
let _, pos =
Array.skip i s
|> Array.fold
(fun (maxChar, pos) (j, c) ->
if maxChar < c || (pos <> s.Length && maxChar <= c) then
c, j
else
maxChar, pos)
(digit, s.Length)
if pos <> s.Length then
let cs = s |> Array.map snd
let tmp = cs.[i]
cs.[i] <- cs.[pos]
cs.[pos] <- tmp
cs |> Array.fold (fun acc c -> 10 * acc + int c - int '0') 0
else
maximumSwap' (i + 1) s
let s = string num |> fun s -> s.ToCharArray() |> Array.indexed
maximumSwap' 0 s