我有一个使用动态编程解决加权间隔调度问题的程序(信不信由你,它不适合家庭作业)。我已经对其进行了概要分析,而且我似乎大部分时间都在用 p(...) 填充 M。以下是功能:
let rec get_highest_nonconflicting prev count start =
match prev with
head :: tail ->
if head < start then
count
else
get_highest_nonconflicting tail (count - 1) start
| [] -> 0;;
let m_array = Array.make (num_genes + 1) 0;;
let rec fill_m_array ?(count=1) ?(prev=[]) gene_spans =
match gene_spans with
head :: tail -> m_array.(count) <-
get_highest_nonconflicting prev (count - 1) (get_start head);
fill_m_array tail ~prev:(get_stop (head) :: prev) ~count:(count+1);
| [] -> ();;
我真的想不出任何优化这个的方法,根据我对这个算法的了解,这似乎是最可能占用时间的地方。但这也只是我的第二个 OCaml 程序。那么有没有办法优化呢?