给定两个整数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
输出:
3
时间复杂度: O(N 2 )
空间复杂度: O(N 2 )
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。