[LeetCode#265] Paint House II - airforce - 博客园



[LeetCode#265] Paint House II - airforce - 博客园

Problem:

There are a row of n houses, each house can be painted with one of the k colors. 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.

The cost of painting each house with a certain color is represented by a n x k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

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

Analysis:

复制代码
This problem is very elegant if you take the time comlexity constraint into consideration.   It actually share the same dynamic programming idea as Paint House |.    If we continue follow the old coding structure, we definitely would end up with the time complexity: O(nk^2).  level 1: n is the total number of houses we have to paint.   level 2: the first k represent for each house we need to try k colors.   level 3: the second k was caused by the process to search the minimum cost (if not use certain color).    Apparently, if we want reach the time complexity O(nk), we have to optimize our operation at level 3.   If we choose the color[i][j], how could we reduce the comparision between (color[i-1][0] to color[i-1][k], except color[i-1][j])  And we know there are acutally extra comparisions, since fore each color, we have to find the smallest amongst other colors.     There must be way to solve it, Right?  Yup!!! There is a magic skill for it!!!  Let us assume, we have "min_1" and "min_2".   min_1 : the lowest cost at previous stage.  min_2 : the 2nd lowest cost at previous stage.     And we have the minimum costs for all colors at previous stage.  color[i-1][k]    Then, iff we decide to paint house "i" with color "j", we can compute the minimum cost of other colors at "i-1" stage through following way.  case 1: iff "color[i-1][j] == min_1", it means the min_1 actually records the minimum value of color[i-1][j] (previous color is j), we have to use min_2;  case 2: iff "color[i-1][j] != min_1", it means min_1 is not the value of color[i-1][j] (previous color is not j), we can use the min_1's color.  Note: iff "pre_min_1 == pre_min_2", it means there are two minimum costs, anyway, no matter which color is pre_min_1, we can use pre_min_2.  ----------------------------------------------------------  if (dp[j] != pre_min_1 || pre_min_1 == pre_min_2) {      dp[j] = pre_min_1 + costs[i][j];  } else{      dp[j] = pre_min_2 + costs[i][j];  }  ----------------------------------------------------------  The way to maintain "min_1" and "min_2".  for (int i = 0; i < len; i++) {      ...      min_1 = Integer.MAX_VALUE;      min_2 = Integer.MAX_VALUE;      ...      if (dp[j] <= min_1) {          min_2 = min_1;          min_1 = dp[j];      } else if (dp[j] < min_2){          min_2 = dp[j];      }  }    Note:  To reduce the burden of handling case, we absolutely could start from i=0, when we could assume all previous cost is 0 since we have no house.

Read full article from [LeetCode#265] Paint House II - airforce - 博客园


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts