Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 630. Course Schedule III in F#

Shohei Yoshida's photo
Shohei Yoshida
·Jun 23, 2022·

1 min read

URL

leetcode.com/problems/course-schedule-iii

Code

github.com/syohex/dotnet-study/blob/master/..

#r "nuget:FSharpx.Collections"

open FSharpx.Collections

let scheduleCourse (courses: (int * int) list) : int =
    let rec scheduleCourse' time courses q qLen =
        match courses with
        | [] -> qLen
        | (duration, lastDay) :: rest ->
            if time + duration <= lastDay then
                let q' = PriorityQueue.insert duration q
                scheduleCourse' (time + duration) rest q' (qLen + 1)
            else
                match PriorityQueue.tryPop q with
                | None -> scheduleCourse' time rest q qLen
                | Some (minDuration, q') ->
                    if minDuration > duration then
                        let time' = time - minDuration + duration
                        let q'' = PriorityQueue.insert duration q'
                        scheduleCourse' time' rest q'' qLen
                    else
                        scheduleCourse' time rest q qLen

    let courses' =
        courses
        |> List.sortWith (fun (_, lastDay1) (_, lastDay2) -> compare lastDay1 lastDay2)

    let q = PriorityQueue.empty true
    scheduleCourse' 0 courses' q 0
 
Share this