给定两个分别表示级别数和秒数的整数N和T ,任务是在给定条件下找到T秒后完全填充的容器数:
- N级血管的结构使得每一级的血管数等于级数,即1, 2, 3, … 直到 N 。
- 每个容器最多可储存 1 个单位的水,并且每秒钟就有 1 个单位的水以恒定速率从水龙头中倒出。
- 如果容器装满,则水开始从容器中流出,并从容器边缘倾泻而下,并均匀分布在紧邻其下方的两个相连容器中。
假设:
- 所有对象沿水平轴对称排列。
- 所有级别均等间隔。
- 水对称地流过容器的两个边缘。
例子:
Input: N = 3, T = 2
Output: 1
Explanation:
View of Structure with N = 3 and at a time T = 2 after the tap has been opened
Input: N = 3, T = 4
Output: 3
Explanation:
View of Structure with N = 3 and at a time T = 4 after the tap has been opened
朴素的方法:解决问题的最简单方法是检查是否可以在T秒内完全填充x 个容器。如果发现为真,则检查x+1血管并重复以获取x的最大值。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:上述方法可以使用动态规划进行优化。请按照以下步骤解决问题:
- 将血管结构存储在矩阵中,例如M ,其中M[i][j]表示第i 层中的第j个血管。
- 对于任何容器M[i][j] ,在直接较低级别的连接容器是M[i + 1][j]和M[i + 1][j + 1] 。
- 最初,将所有水放入第一个容器中,即。 M[0][0] = t 。
- 从最上面的容器 i 开始,每单位时间增量重新计算矩阵的状态,即。 M[0][0] = t 。
- 如果水量超过容器的体积,则从容器中流下的水量分成 2 等分
- 部分填充两个相连的容器在立即较低的水平。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
int n, t;
// Function to find the number of
// completely filled vessels
int FindNoOfFullVessels(int n, int t)
{
// Store the vessels
double Matrix[n][n];
// Assuming all water is present
// in the vessel at the first level
Matrix[0][0] = t * 1.0;
// Store the number of vessel
// that are completely full
int ans = 0;
// Traverse all the levels
for(int i = 0; i < n; i++)
{
// Number of vessel at each
// level is j
for(int j = 0; j <= i; j++)
{
// Calculate the exceeded
// amount of water
double exceededwater = Matrix[i][j] - 1.0;
// If current vessel has
// less than 1 unit of
// water then continue
if (exceededwater < 0)
continue;
// One more vessel is full
ans++;
// If left bottom vessel present
if (i + 1 < n)
Matrix[i + 1][j] += exceededwater / 2;
// If right bottom vessel present
if (i + 1 < n && j + 1 < n)
Matrix[i + 1][j + 1] += exceededwater / 2;
}
}
return ans;
}
// Driver Code
int main()
{
// Number of levels
int N = 3;
// Number of seconds
int T = 4;
// Function call
cout << FindNoOfFullVessels(N, T) << endl;
return 0;
}
// This code is contributed by sanjoy_62
Java
// Java Program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG {
static int n, t;
// Function to find the number of
// completely filled vessels
public static int
FindNoOfFullVessels(int n, int t)
{
// Store the vessels
double Matrix[][]
= new double[n][n];
// Assuming all water is present
// in the vessel at the first level
Matrix[0][0] = t * 1.0;
// Store the number of vessel
// that are completely full
int ans = 0;
// Traverse all the levels
for (int i = 0; i < n; i++) {
// Number of vessel at each
// level is j
for (int j = 0; j <= i; j++) {
// Calculate the exceeded
// amount of water
double exceededwater
= Matrix[i][j] - 1.0;
// If current vessel has
// less than 1 unit of
// water then continue
if (exceededwater < 0)
continue;
// One more vessel is full
ans++;
// If left bottom vessel present
if (i + 1 < n)
Matrix[i + 1][j]
+= exceededwater / 2;
// If right bottom vessel present
if (i + 1 < n && j + 1 < n)
Matrix[i + 1][j + 1]
+= exceededwater / 2;
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Number of levels
int N = 3;
// Number of seconds
int T = 4;
// Function call
System.out.println(
FindNoOfFullVessels(N, T));
}
}
Python3
# Python3 program to implement
# the above approach
# Function to find the number of
# completely filled vessels
def FindNoOfFullVessels(n, t) :
# Store the vessels
Matrix = [[0 for i in range(n)] for j in range(n)]
# Assuming all water is present
# in the vessel at the first level
Matrix[0][0] = t * 1.0
# Store the number of vessel
# that are completely full
ans = 0
# Traverse all the levels
for i in range(n) :
# Number of vessel at each
# level is j
for j in range(i + 1) :
# Calculate the exceeded
# amount of water
exceededwater = Matrix[i][j] - 1.0
# If current vessel has
# less than 1 unit of
# water then continue
if (exceededwater < 0) :
continue
# One more vessel is full
ans += 1
# If left bottom vessel present
if (i + 1 < n) :
Matrix[i + 1][j] += exceededwater / 2
# If right bottom vessel present
if (i + 1 < n and j + 1 < n) :
Matrix[i + 1][j + 1] += exceededwater / 2
return ans
# Number of levels
N = 3
# Number of seconds
T = 4
# Function call
print(FindNoOfFullVessels(N, T))
# This code is contributed by divyesh072019
C#
// C# program to implement
// the above approach
using System;
class GFG{
//static int n, t;
// Function to find the number of
// completely filled vessels
public static int FindNoOfFullVessels(int n,
int t)
{
// Store the vessels
double[,] Matrix = new double[n, n];
// Assuming all water is present
// in the vessel at the first level
Matrix[0, 0] = t * 1.0;
// Store the number of vessel
// that are completely full
int ans = 0;
// Traverse all the levels
for(int i = 0; i < n; i++)
{
// Number of vessel at each
// level is j
for(int j = 0; j <= i; j++)
{
// Calculate the exceeded
// amount of water
double exceededwater = Matrix[i, j] - 1.0;
// If current vessel has
// less than 1 unit of
// water then continue
if (exceededwater < 0)
continue;
// One more vessel is full
ans++;
// If left bottom vessel present
if (i + 1 < n)
Matrix[i + 1, j] += exceededwater / 2;
// If right bottom vessel present
if (i + 1 < n && j + 1 < n)
Matrix[i + 1, j + 1] += exceededwater / 2;
}
}
return ans;
}
// Driver Code
public static void Main()
{
// Number of levels
int N = 3;
// Number of seconds
int T = 4;
// Function call
Console.WriteLine(FindNoOfFullVessels(N, T));
}
}
// This code is contributed by sanjoy_62
Javascript
输出:
3
时间复杂度: O(N 2 )
空间复杂度: O(N 2 )