Lazily Tying the Knot for 1 Dimensional Dynamic Programming

前端 未结 4 836
春和景丽
春和景丽 2021-01-01 17:59

Several years ago I took an algorithms course where we were giving the following problem (or one like it):

There is a building of n floor

4条回答
  •  囚心锁ツ
    2021-01-01 18:43

    Others have answered your direct question about dynamic programming. However, for this kind of problem I think the greedy approach works the best. It's implementation is very straightforward.

    f i j :: Int -> Int -> Int
    f i j = snd $ until (\(i,_) -> i == j) 
                        (\(i,x) -> (i + if i < j then 2 else (-3),x+1))
                        (i,0)
    

提交回复
热议问题