zoukankan      html  css  js  c++  java
  • 一排房子,连续填色,成本最低的问题

    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.

  • 相关阅读:
    php
    php数据排序---array_multisort
    IOS 线程描述
    IOS 进程描述
    IOS 强指针(strong)和弱指针(weak)
    IOS autosizing(设置控件的固定位置大小)
    IOS UIActivityIndicatorView动画
    IOS UIImageView的帧动画
    IOS Block动画
    IOS UIView动画(封装动画)
  • 原文地址:https://www.cnblogs.com/yayagamer/p/2412650.html
Copyright © 2011-2022 走看看