*paint House II

Source: Internet
Author: User

There is a row of n houses, each house can is painted with one of the K colors. The cost of painting a certain color is different. You had to paint all the houses such, that no, and adjacent houses have the same color.

The cost of painting a certain color was represented by a cost n x k matrix. For example, are the cost of costs[0][0] painting House 0 with color 0; costs[1][2] are the cost of Painting House 1 with color 2, and S O on ... Find the minimum cost-to-paint all houses.

Note:
All costs is positive integers.

Follow up:
Could you solve it in O(nk) runtime?

Solution One: O (NKK) ...

 Public classSolution { Public intMincostii (int[] costs) {    if(costs==NULL|| Costs.length==0){        return0; }         for(intI=1; i<costs.length; i++)    {         for(intj=0; j<costs[0].length;j++)        {            intMin =Integer.max_value;  for(intk=0; k<costs[0].length;k++)            {                if(K==J)Continue; Min= Math.min (min,costs[i-1][k]); } Costs[i][j]+=min; }    }        intn = costs.length-1; intMin =Integer.max_value;  for(intj=0; j<costs[0].length;j++) {min=math.min (Min,costs[n][j]); }    returnmin; }}

Solution Two: O (NK)

Explanation:dp[i][j] represents the min paint cost from House 0 to house I if House I use color j; The formula is dp[i][j] = math.min (any k!= j| dp[i-1][k]) + costs[i][j].

Take a closer look at the formula, we don't need an array to represent DP[I][J], we only need to know the min cost to the Previous House of any color and if the color J was used on previous house to get Prev min cost, with the second min cost tha T is a using color J on the previous house. So I had three variable to record:prevmin, Prevmincolor, Prevsecondmin. And the above formula would be translated into:dp[currenthouse][currentcolor] = (CurrentColor = = Prevmincolor? prevsecondm In:prevmin) + Costs[currenthouse][currentcolor].

 Public classSolution { Public intMincostii (int[] costs) {    if(Costs = =NULL|| Costs.length = = 0 | | Costs[0].length = = 0)return0; intn = costs.length, k = costs[0].length; if(k = = 1)return(N==1 costs[0][0]: 1); intPrevmin = 0, Prevminind =-1, prevsecmin = 0;//prevsecmin always >= prevmin     for(inti = 0; i<n; i++) {        intmin = integer.max_value, Minind =-1, secmin =Integer.max_value;  for(intj = 0;  j<k; J + +) {            intval = Costs[i][j] + (j = = Prevminind?)prevsecmin:prevmin); if(minind< 0) {min = val; minind = j;}//When min isn ' t initialized            Else if(Val < min) {//When val < min,Secmin =min; Min=Val; Minind=J; } Else if(Val < secmin) {//When min<=val< secminSecmin =Val; }} prevmin=min; Prevminind=Minind; Prevsecmin=secmin; }    returnprevmin;}}

Reference:https://leetcode.com/discuss/60625/fast-dp-java-solution-runtime-o-nk-space-o-1

*paint House II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.