📌  相关文章
📜  最小化N座房屋的油漆成本,以使相邻房屋具有不同的颜色

📅  最后修改于: 2021-05-17 23:56:34             🧑  作者: Mango

给定整数N和2D数组cost [] [3] ,其中cost [i] [0]cost [i] [1]cost [i] [2]是用颜色绘制i栋房屋的成本分别是红色蓝色绿色,任务是找到粉刷所有房屋的最低成本,以确保没有两个相邻的房屋具有相同的颜色。

例子:

天真的方法:解决给定问题的最简单方法是生成所有可能的方法,以红色蓝色绿色为所有房屋着色,并在所有可能的组合中找到最低成本,以使没有两个相邻的房屋具有相同的颜色颜色。
时间复杂度: (3 N )
辅助空间: O(1)

高效的方法:可以通过使用动态编程来优化上述方法,因为可以存储重叠的子问题以最大程度地减少递归调用的次数。想法是在以前着色的房屋的其他两种颜色的最低成本的基础上,找到用任何颜色喷涂当前房屋的最低成本。请按照以下步骤解决给定的问题:

请按照以下步骤解决问题:

  • 创建一个辅助2d dp [] [3]数组,以存储先前着色房屋的最低成本。
  • dp [0] [0]dp [0] [1]dp [0] [2]初始化为cost [i] [0]cost [i] [1]cost [i]的成本[2]分别。
  • 遍历给定阵列成本[] [3]在所述范围[1,N],并更新绘画当前房子颜色的成本红色蓝色,和绿色的具有最小以dp成本其它两种颜色[I][ 0]dp [i] [1]dp [i] [2]
  • 完成上述步骤后,打印dp [N – 1] [0]dp [N – 1] [1]dp [N – 1] [2]的最小值作为用油漆所有房屋的最低成本不同的相邻颜色。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum cost of
// coloring the houses such that no two
// adjacent houses has the same color
int minCost(vector >& costs,
            int N)
{
    // Corner Case
    if (N == 0)
        return 0;
 
    // Auxiliary 2D dp array
    vector > dp(
        N, vector(3, 0));
 
    // Base Case
    dp[0][0] = costs[0][0];
    dp[0][1] = costs[0][1];
    dp[0][2] = costs[0][2];
 
    for (int i = 1; i < N; i++) {
 
        // If current house is colored
        // with red the take min cost of
        // previous houses colored with
        // (blue and green)
        dp[i][0] = min(dp[i - 1][1],
                       dp[i - 1][2])
                   + costs[i][0];
 
        // If current house is colored
        // with blue the take min cost of
        // previous houses colored with
        // (red and green)
        dp[i][1] = min(dp[i - 1][0],
                       dp[i - 1][2])
                   + costs[i][1];
 
        // If current house is colored
        // with green the take min cost of
        // previous houses colored with
        // (red and blue)
        dp[i][2] = min(dp[i - 1][0],
                       dp[i - 1][1])
                   + costs[i][2];
    }
 
    // Print the min cost of the
    // last painted house
    cout << min(dp[N - 1][0],
                min(dp[N - 1][1],
                    dp[N - 1][2]));
}
 
// Driver Code
int main()
{
    vector > costs{ { 14, 2, 11 },
                                { 11, 14, 5 },
                                { 14, 3, 10 } };
    int N = costs.size();
 
    // Function Call
    minCost(costs, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the minimum cost of
  // coloring the houses such that no two
  // adjacent houses has the same color
  static void minCost(int costs[][], int N)
  {
 
    // Corner Case
    if (N == 0)
      return;
 
    // Auxiliary 2D dp array
    int dp[][] = new int[N][3];
 
    // Base Case
    dp[0][0] = costs[0][0];
    dp[0][1] = costs[0][1];
    dp[0][2] = costs[0][2];
 
    for (int i = 1; i < N; i++) {
 
      // If current house is colored
      // with red the take min cost of
      // previous houses colored with
      // (blue and green)
      dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2])
        + costs[i][0];
 
      // If current house is colored
      // with blue the take min cost of
      // previous houses colored with
      // (red and green)
      dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2])
        + costs[i][1];
 
      // If current house is colored
      // with green the take min cost of
      // previous houses colored with
      // (red and blue)
      dp[i][2] = Math.min(dp[i - 1][0], dp[i - 1][1])
        + costs[i][2];
    }
 
    // Print the min cost of the
    // last painted house
    System.out.println(
      Math.min(dp[N - 1][0],
               Math.min(dp[N - 1][1], dp[N - 1][2])));
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    int costs[][] = { { 14, 2, 11 },
                     { 11, 14, 5 },
                     { 14, 3, 10 } };
 
    int N = costs.length;
 
    // Function Call
    minCost(costs, N);
  }
}
 
// This code is contribued by Kingash.


Python3
# Python 3 program for the above approach
 
# Function to find the minimum cost of
# coloring the houses such that no two
# adjacent houses has the same color
def minCost(costs, N):
   
    # Corner Case
    if (N == 0):
        return 0
 
    # Auxiliary 2D dp array
    dp = [[0 for i in range(3)] for j in range(3)]
 
    # Base Case
    dp[0][0] = costs[0][0]
    dp[0][1] = costs[0][1]
    dp[0][2] = costs[0][2]
 
    for i in range(1, N, 1):
       
        # If current house is colored
        # with red the take min cost of
        # previous houses colored with
        # (blue and green)
        dp[i][0] = min(dp[i - 1][1], dp[i - 1][2]) + costs[i][0]
 
        # If current house is colored
        # with blue the take min cost of
        # previous houses colored with
        # (red and green)
        dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + costs[i][1]
 
        # If current house is colored
        # with green the take min cost of
        # previous houses colored with
        # (red and blue)
        dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + costs[i][2]
 
    # Print the min cost of the
    # last painted house
    print(min(dp[N - 1][0], min(dp[N - 1][1],dp[N - 1][2])))
 
# Driver Code
if __name__ == '__main__':
    costs = [[14, 2, 11],
             [11, 14, 5],
             [14, 3, 10]]
    N = len(costs)
     
    # Function Call
    minCost(costs, N)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
 
  // Function to find the minimum cost of
  // coloring the houses such that no two
  // adjacent houses has the same color
  static int minCost(List>costs,
                     int N)
  {
    // Corner Case
    if (N == 0)
      return 0;
 
    // Auxiliary 2D dp array
    List temp = new List();
    for(int i=0;i<3;i++)
      temp.Add(0);
    List> dp = new List>();
    for(int i=0;i>costs = new List>();
    costs.Add(new List(){14, 2, 11});
    costs.Add(new List(){11, 14, 5 });
    costs.Add(new List(){14, 3, 10 });
    int N = 3;
 
    // Function Call
    Console.WriteLine((int)(minCost(costs, N)));
  }
}
 
// This code is contributed by bgangwar59.


输出:
10

时间复杂度: O(N)
辅助空间: O(N)