给定输入的路段数量,每个路段在道路两侧都有 2 个地块。找到所有可能的方法在地块中建造建筑物,以便在任何 2 座建筑物之间留有空间。
例子 :
N = 1
Output = 4
Place a building on one side.
Place a building on other side
Do not place any building.
Place a building on both sides.
N = 3
Output = 25
3 sections, which means possible ways for one side are
BSS, BSB, SSS, SBS, SSB where B represents a building
and S represents an empty space
Total possible ways are 25, because a way to place on
one side can correspond to any of 5 ways on other side.
N = 4
Output = 64
https://practice.geeksforgeeks.org/problems/count-possible-ways-to-construct-buildings5007/1
我们强烈建议您将浏览器最小化并先尝试一下
我们可以将问题简化为首先只计算一侧。如果我们知道一侧的结果,我们总是可以对结果进行平方并得到两侧的结果。
新建筑可以放置在该部分之前,如果该部分有空间。一个空间可以放置在任何地方(无论上一节是否有建筑物)。
Let countB(i) be count of possible ways with i sections
ending with a building.
countS(i) be count of possible ways with i sections
ending with a space.
// A space can be added after a building or after a space.
countS(N) = countB(N-1) + countS(N-1)
// A building can only be added after a space.
countB[N] = countS(N-1)
// Result for one side is sum of the above two counts.
result1(N) = countS(N) + countB(N)
// Result for two sides is square of result1(N)
result2(N) = result1(N) * result1(N)
下面是上述想法的实现。
C++
// C++ program to count all possible way to construct buildings
#include
using namespace std;
// Returns count of possible ways for N sections
int countWays(int N)
{
// Base case
if (N == 1)
return 4; // 2 for one side and 4 for two sides
// countB is count of ways with a building at the end
// countS is count of ways with a space at the end
// prev_countB and prev_countS are previous values of
// countB and countS respectively.
// Initialize countB and countS for one side
int countB=1, countS=1, prev_countB, prev_countS;
// Use the above recursive formula for calculating
// countB and countS using previous values
for (int i=2; i<=N; i++)
{
prev_countB = countB;
prev_countS = countS;
countS = prev_countB + prev_countS;
countB = prev_countS;
}
// Result for one side is sum of ways ending with building
// and ending with space
int result = countS + countB;
// Result for 2 sides is square of result for one side
return (result*result);
}
// Driver program
int main()
{
int N = 3;
cout << "Count of ways for " << N
<< " sections is " << countWays(N);
return 0;
}
Java
class Building
{
// Returns count of possible ways for N sections
static int countWays(int N)
{
// Base case
if (N == 1)
return 4; // 2 for one side and 4 for two sides
// countB is count of ways with a building at the end
// countS is count of ways with a space at the end
// prev_countB and prev_countS are previous values of
// countB and countS respectively.
// Initialize countB and countS for one side
int countB=1, countS=1, prev_countB, prev_countS;
// Use the above recursive formula for calculating
// countB and countS using previous values
for (int i=2; i<=N; i++)
{
prev_countB = countB;
prev_countS = countS;
countS = prev_countB + prev_countS;
countB = prev_countS;
}
// Result for one side is sum of ways ending with building
// and ending with space
int result = countS + countB;
// Result for 2 sides is square of result for one side
return (result*result);
}
public static void main(String args[])
{
int N = 3;
System.out.println("Count of ways for "+ N+" sections is "
+countWays(N));
}
}/* This code is contributed by Rajat Mishra */
Python3
# Python 3 program to count all possible
# way to construct buildings
# Returns count of possible ways
# for N sections
def countWays(N) :
# Base case
if (N == 1) :
# 2 for one side and 4
# for two sides
return 4
# countB is count of ways with a
# building at the end
# countS is count of ways with a
# space at the end
# prev_countB and prev_countS are
# previous values of
# countB and countS respectively.
# Initialize countB and countS
# for one side
countB=1
countS=1
# Use the above recursive formula
# for calculating
# countB and countS using previous values
for i in range(2,N+1) :
prev_countB = countB
prev_countS = countS
countS = prev_countB + prev_countS
countB = prev_countS
# Result for one side is sum of ways
# ending with building
# and ending with space
result = countS + countB
# Result for 2 sides is square of
# result for one side
return (result*result)
# Driver program
if __name__ == "__main__":
N = 3
print ("Count of ways for ", N
," sections is " ,countWays(N))
# This code is contributed by
# ChitraNayal
C#
// C# program to count all
// possible way to construct
// buildings
using System;
class GFG
{
// Returns count of possible
// ways for N sections
static int countWays(int N)
{
// Base case
if (N == 1)
// 2 for one side and
// 4 for two sides
return 4;
// countB is count of ways
// with a building at the
// end, countS is count of
// ways with a space at the
// end, prev_countB and
// prev_countS are previous
// values of countB and countS
// respectively.
// Initialize countB and
// countS for one side
int countB = 1, countS = 1,
prev_countB, prev_countS;
// Use the above recursive
// formula for calculating
// countB and countS using
// previous values
for (int i = 2; i <= N; i++)
{
prev_countB = countB;
prev_countS = countS;
countS = prev_countB +
prev_countS;
countB = prev_countS;
}
// Result for one side is sum
// of ways ending with building
// and ending with space
int result = countS + countB;
// Result for 2 sides is
// square of result for
// one side
return (result * result);
}
// Driver Code
public static void Main()
{
int N = 3;
Console.Write(countWays(N));
}
}
// This code is contributed by nitin mittal.
PHP
Javascript
输出
Count of ways for 3 sections is 25
输出 :
Count of ways for 3 sections is 25
时间复杂度:O(N)
辅助空间:O(1)
算法范式:动态规划
优化方案:
注意上述方案可以进一步优化。如果我们仔细查看结果,对于不同的值,我们可以注意到两侧的结果是斐波那契数的平方。
N = 1,结果 = 4 [一侧的结果 = 2]
N = 2,结果 = 9 [一侧的结果 = 3]
N = 3,结果 = 25 [一侧的结果 = 5]
N = 4,结果 = 64 [一侧的结果 = 8]
N = 5,结果 = 169 [一侧的结果 = 13]
…………………………
…………………………
一般来说,我们可以说
result(N) = fib(N+2)2
fib(N) is a function that returns N'th
Fibonacci Number.
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。