LeetCode 1422. Maximum Score After Splitting a String in F#
URL
https://leetcode.com/problems/maximum-score-after-splitting-a-string/description/
Code
https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/problems/1422/main.fsx
open System
let maxScore (s: string) : int =
let rec maxScore' cs left right acc =
match cs with
| [] -> failwith "never reach here"
| _ :: [] -> acc
| h :: t ->
let left', right' = if h = '1' then left, right - 1 else left + 1, right
maxScore' t left' right' (Math.Max(acc, left' + right'))
let cs = Seq.toList s
let left = if List.head cs = '0' then 1 else 0
let right = List.tail cs |> List.filter ((=) '1') |> List.length
maxScore' (List.tail cs) left right (left + right)