📌  相关文章
📜  计算粉刷房屋的总墙面积

📅  最后修改于: 2021-05-17 21:17:31             🧑  作者: Mango

给定整数N,LW表示行数以及每行中房屋的长度和宽度,以及代表每个房屋高度的数组Heights [] ,任务是找到所需的外墙总面积为如果相邻的房屋共用一堵墙,则进行绘制。

例子:

方法:可以使用贪婪技术解决问题。请按照以下步骤解决问题:

  • 涂漆的第一栋房屋的墙壁总面积等于2 * w * h 1 + l *(h 1 + max(0,h 2 – h 1 ))。
  • 最后涂漆的房屋墙壁的总面积等于2 * w * h n + l *(h n + max(0,h n – h n-1 ))。
  • 除了第一个和最后一个房屋外,粉刷过的房屋墙壁的总面积等于2 * w * h i + l *(max(0,h i – h i + 1 )+ max(0,h i – h i-1 ))。
  • 因此,将使用以下公式计算所有粉刷房屋的总面积:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the total area
// of walls painted in N row-houses
void areaToPaint(int N, int W, int L,
                 int Heights[])
{
 
    // Stores total area of N row-houses
    // that needs to be painted
    int total = 0;
 
    // Traverse the array of wall heights
    for (int i = 0; i < N; i++) {
 
        // Update total area painted
        total += 2 * Heights[i] * W;
    }
 
    // Update total
    total += L * (Heights[0]
                  + Heights[N - 1]);
 
    // Traverse all the houses and
    // print the shared walls
    for (int i = 1; i < N; i++) {
 
        // Update total
        total += L * abs(Heights[i]
                         - Heights[i - 1]);
    }
 
    // Print total area needs to paint
    cout << total;
}
 
// Driver Code
int main()
{
 
    // Given N, W & L
    int N = 7, W = 1, L = 1;
 
    // Given heights of houses
    int Heights[N]
        = { 4, 3, 1, 2, 3, 4, 2 };
 
    // Function Call
    areaToPaint(N, W, L, Heights);
}


Java
// Java code of above approach
import java.util.*;
class GFG {
 
  // Function to find the total area
  // of walls painted in N row-houses
  static void areaToPaint(int N, int W, int L,
                          int Heights[])
  {
 
    // Stores total area of N row-houses
    // that needs to be painted
    int total = 0;
 
    // Traverse the array of wall heights
    for (int i = 0; i < N; i++) {
 
      // Update total area painted
      total += 2 * Heights[i] * W;
    }
 
    // Update total
    total += L * (Heights[0]
                  + Heights[N - 1]);
 
    // Traverse all the houses and
    // print the shared walls
    for (int i = 1; i < N; i++) {
 
      // Update total
      total += L * Math.abs(Heights[i]
                            - Heights[i - 1]);
    }
 
    // Print total area needs to paint
    System.out.print(total);
  }
 
  // Driver code
  public static void main(String[] args)
  {
    // Given N, W & L
    int N = 7, W = 1, L = 1;
 
    // Given heights of houses
    int Heights[]
      = { 4, 3, 1, 2, 3, 4, 2 };
 
    // Function Call
    areaToPaint(N, W, L, Heights);
  }
}
 
// This code is contributed by offbeat


Python3
# Python 3 program for the above approach
 
# Function to find the total area
# of walls painted in N row-houses
def areaToPaint(N, W, L, Heights):
 
    # Stores total area of N row-houses
    # that needs to be painted
    total = 0
 
    # Traverse the array of wall heights
    for i in range(N):
 
        # Update total area painted
        total += 2 * Heights[i] * W
 
    # Update total
    total += L * (Heights[0]
                  + Heights[N - 1])
 
    # Traverse all the houses and
    # print the shared walls
    for i in range(1, N):
 
        # Update total
        total += L * abs(Heights[i]
                         - Heights[i - 1])
 
    # Print total area needs to paint
    print(total)
 
 
# Driver Code
if __name__ == "__main__":
 
    # Given N, W & L
    N = 7
    W = 1
    L = 1
 
    # Given heights of houses
    Heights = [4, 3, 1, 2, 3, 4, 2]
 
    # Function Call
    areaToPaint(N, W, L, Heights)
 
    # This code is contributed by chitranayal.


输出:
52

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