256.Paint House

Source: Internet
Author: User

    

/** 256.Paint House * 2016-6-21 by Mingyang * Typical DP, I thought from the beginning that if you use another value of a two-dimensional dp,dp to represent the color * The most advanced part of this place is! I was updating the cost of the matrix, and did not specifically come up with a DP array very clever! */ Public intMincost (int[] costs) { //Please remember the simplest case. if(Costs! =NULL&& Costs.length = = 0) return0; for(inti = 1; i < costs.length; i++){ //In the first color, the Last house cannot be painted in the first color, so we have to find the smallest of the second and third colors in the Last house plusCosts[i][0] = costs[i][0] + math.min (costs[i-1][1], costs[i-1][2]); //Apply the second or third color similarlyCOSTS[I][1] = costs[i][1] + math.min (costs[i-1][0], costs[i-1][2]); costs[i][2] = costs[i][2] + math.min (costs[i-1][0], costs[i-1][1]); } //returns the least expensive of the three colors. returnMath.min (Costs[costs.length-1][0], math.min (costs[costs.length-1][1], costs[costs.length-1][2])); }

256.Paint House

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.