• 一排房子,连续填色,成本最低的问题


    Q:

    There are a row of houses, each house can be painted with three colors red, blue and green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. You have to paint the houses with minimum cost. How would you do it?

    Note: Painting house-1 with red costs different from painting house-2 with red. The costs are different for each house and each color.

    A:

    If T(i) is the min up to the ith place, and T(i,j) is the min up to ith place with ith spot having the color j, then:

    T(i)=min(T(i-1,B)+min(Gi,Ri), T(i-1,R)+min(Gi,Bi), T(i-1,G)+min(Bi,Ri))

    which naively would result in exponential growth, but if you cache the results in an array, I believe it should be O(n).

    u hv to maintain 3 minimums
    cost(i,b)=min(cost(i-1,g),cost(i-1,r))+cost of painting i as b;
    cost(i,g)=min(cost(i-1,b),cost(i-1,r))+cost of painting i as g;
    cost(i,r)=min(cost(i-1,g),cost(i-1,b))+cost of painting i as r;

    finally min(cost(N,b),cost(N,g),cost(N,r)) is the answer.

  • 相关阅读:
    动画 + 设置contentoffset,然后就 蛋疼了,
    xmpp这一段蛋疼的 坑,
    项目,
    一段测试代码,哦哦哦,
    libresolv,
    mutating method sent to immutable object'
    解析json,是还是不是,
    济南学习 Day 4 T1 am
    济南学习 Day 3 T3 pm
    济南学习 Day 3 T2 pm
  • 原文地址:https://www.cnblogs.com/yayagamer/p/2412650.html
Copyright © 2020-2023  润新知