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

📅  最后修改于: 2021-09-17 07:14:01             🧑  作者: Mango

给定一个整数N和一个二维数组cost[][3] ,其中cost[i][0]cost[i][1]cost[i][2]是用颜色绘制i房子的成本分别为redbluegreen ,任务是找到油漆所有房屋的最小成本,使得没有两个相邻的房屋具有相同的颜色。

例子:

朴素方法:解决给定问题的最简单方法是生成所有可能的方法,用红色蓝色绿色为所有房屋着色,并在所有可能的组合中找到最小成本,使得没有两个相邻的房屋具有相同的颜色颜色。
时间复杂度: (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]分别。
  • [1, N]范围内遍历给定的数组cost[][3]并使用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 contributed 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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程