This problem involves finding the minimum generation K where a target point appears in a 3D space, where each generation is formed by taking all pairs of distinct points from previous generations and computing new points as floor((x1+x2)/2), floor((y1+y2)/2), and floor((z1+z2)/2). The solution uses a set to store unique points for each generation, with time complexity O(n²) per generation, and terminates when no new points are generated or after 343 generations (since coordinates are ≤6, giving 7×7×7=343 possible distinct points).
Deep Dive
Voraussetzung
- Keine Daten verfügbar.
Nächste Schritte
- Keine Daten verfügbar.
Deep Dive
Leetcode 3923 | Minimum Generations to Target Point | Leetcode biweekly contest 182Hinzugefügt:
Hello. In this video, let's discuss third question of today's contest. You are given a 2D integer array points where points of I is xi, yi, zi represents a point in a 3D space and an integer target representing a target point. Define the generation zero as the initial list of points. For each integer K more than equal to one, form the generation K points as follows.
Consider every pair of two distinct points x1, y1, z1 and x2, y2, z2 taken from all the points produced in the generation zero through K minus one.
For each such pair, compute a new point which is floor of x1 + x2 / 2 and so on.
And collect every such C into a new generation K.
All the points in the generation K are produced simultaneously from the points in zero to K minus one.
After generation K is formed, the points in the generation K are considered available for the later generations.
Return the smallest integer K such that the given target appears in one of the generation zero through If the target is in the initial points, then it is zero.
If it is impossible, then return minus one.
And the floor denotes the down to the nearest integer.
So, floor of one plus one plus four by two it is equal to two because five by two floor is equal to two.
Two distinct points means that two chosen points must have different x, y, z coordinates.
And the constraints are the points are up to only 20. The values are six. The target length is only three. It means it is a valid and then target of I is less than or equal to six.
So, the question is over complicated, but it is very simple, right?
So, the question is very simply saying that initially we have let's say initially we have some uh five points.
Let's say we have five x, y, z in the generation zero.
Now, given these five points, what we have to find?
This is the zeroth generation. So, how can we find the values in the first generation.
We have to take every two distinct coordinates from the before set. For example, let's say this is the zeroth generation. So, in the first row, we have to consider these two. And then, we have to produce some kind of sun with respect to these two. So, this will contain the floor of these two by two, floor of y by two, and floor of z by two. And then, we should consider these two points. And then, we should consider these two, and these two, and these two, these two, these two, these two, these two, these two.
So, if you have n points in the before generation, then how many Let's assume that these are n distinct, right? Let's assume if these are n distinct. So, how can we find the number of points in the generation plus one of the before one?
We have to take every two, and then we have to find a new coordinate, new tuple, indirectly. Tuple means simply 3D point 3D point.
x, z, y. So, this is called tuple. So, two is called a pair. Three is called tuple. So, for every tuple, we have to find a new tuple for every pair in the before generation tuple.
And then, what is the time complexity for doing this? It will be n squared, right? Because if you have n points, then we can select n into n minus one by two number of distinct pairs to get a new point.
So, the time complexity for this will be n squared. And then, and then, is there a way like is there a use of storing same coordinate twice?
Let's say we have 111. And let's say we have Sorry, let's take 000 and 222.
Now, let's assume these are in the zeroth generation. So, in the first generation, what are the points that are available? They are 000, 222.
All the before points also will exist.
But, new points will be generated where the new points are from before pairs.
So, the only pair that we can form in the generation one because of these two are 111.
Because these two are the only pairs, right? So, in the generation one, these three are the only coordinates we have.
So, and now let's come to second generation.
So, in the second generation, these three points will be existing. And then coming to the new points, what are new points will be there? Let's first start with these two.
So, given these two, the new point formed is 111. So, we already have 111.
So, is there a way to maintain this also 111 in the new vector?
No, because there is no use at all. For example, if we have 111, and let's say we are storing 111.
And then let's assume the other pair.
So, what is the use of storing this?
Because if the new coordinate, if we are trying to find, then we can find between these two. No need to find between these two, because these both will give Let's say this is giving tuple one.
So, these both also will give tuple one.
So, indirectly, no need to store the same tuple again.
And if we are trying to store the same, then by these two, only the same will be formed again.
That's why if no need to maintain two sim two same things, because two same things will not give a new one, or two same things will not give a new one with the other one.
So, if this is if these both are giving tuple one, these both also will give give same. So, very simply, we have to store only the unique tuples XYZ in the generation. So, now what can we do?
The constraints are very small, right?
So, the points are only up to six. Let's assume seven. So, the target It means the points are only less than or equal to seven. So, what is the net volume?
Let's say what are uh So, what are the total points that we can have? It is 7 into 7 into 7.
So, these are the only points that we have. These many distinct points that we have. And that is simply 343.
So, now what we should do? Let's go with the same brute force, because the time complexity for forming the new generation is n squared.
Let's assume if we are finding the Right? Because if we have n points, and for next, it will be n into n plus 1 by 2. n into n plus one by two.
And then we will get all these points and then we will get a new generation using all these n all these distinct pairs and so on.
Now one question thing the one thing that we have to address is till how many generations we have to keep on traversing until we get that point.
We can't do infinite times, right?
Because even though it's time complexity is this, if we are doing ultimate like infinite times, it will not be possible. So how to find what is the maximum number of generations where we can try to get this as answer.
So one thing, the point should be like given a target, there are two possibilities. The first thing is the answer can be there.
It can be there in some kth generation or it cannot be there at all. So if there is in kth generation, then we can try to loop till some 10 power six or highest value and then we can break if we find this value. But what if we don't have any guarantee that this point will not be there, then we'll keep on traversing.
Right? But what if the number found is in only ninth 10 power ninth generation?
So we can't loop through ultimately. So how to find if there is answer or not?
So that is the that is one thing left.
Because if we know if the target element exists or not and then we want to know what is the maximum generation. These two are the points to be addressed. So how to do this?
So instead of complicating, let's say we got we discussed that, right? We only have 343 points.
If we even if we do 10 power nine generations, the points the total number of distinct points are only up to 343.
So can we keep an upper bound or can we keep a maximum number of generations?
Can we find that? Yes.
So let's say we are at the generation one zero and let's say we are generation k and then we are trying to form k plus one generation.
So if the values in in k is equal to values in k plus 1. For example, if there are three points in this and after doing all the computation, even then we got only three distinct points in this. So, what does that mean then?
It means there is no use of continuing this because the points are only distinct.
So, these distinct points will come in valid iterations, maybe some finite iterations or something.
It will be very less.
So, there is no need of keeping the loop ultimately.
If the points in the generation K is equal to the number of points in generation K + 1, it means all the points are generated and the no new points can be generated. And that's why we can break the loop here itself.
Because if these are the only three points, then even if you go to next generation, only these three can be found.
That's why if the current set if the current elements in K and then if we are trying to find the K + 1 if all the values are same, it means we are unable to add any new element.
Let's take the same example. Let's take 0 0 0, 2 2 2, and 1 1 1.
Now, is there any way of adding the new point into this?
Let's initially the values are 0 0 0, 2 2 2. And now we came to next generation, so we got a new point 1 1 1. Now, now what are the points in the next generation? These three will come again, 0 0 0, 1 1 1, 2 2 2. Now, are there any new points that can be found? Let's check. Let's take these two. So, if we take these two, then we have to take floor. So, 0 0 0 will be formed again, so no new point. And coming to these two, even then 1 1 1 will come, but it is already there, so no use. And then let's come to this. So, even if we are taking these two, then 1 1 1 will come.
That's why we got that these three are the only points that are there and these three points cannot be increased. So, why to waste? So, simply break the loop.
And if the point is existing, let it For example, in the Kth generation, we have X points and in the next generation, if we are able to get more than X points, more than X tuples, then we can keep on continuing because we are sure that the points are only up to 343.
Right, the points are only up to 343.
So, we can loop till at most 343 generations. Even though this is not possible, but this can be like a at most at most upper bound.
So, we can we can loop through 343 generations and then we can keep on continuing the finding the elements in the next generation from the previous. And then if these both are same, then break the loop.
And we can check if the target is in generation or not.
So, let's discuss for the last time. So, initially we have generation zero. Now, we'll check if the tuple is in this list. If yes, we can return here itself.
Else, we will start the loop. And then in the loop, we will loop through every two distinct values and then we'll get all the new points. Now, in the new points, we'll check if the present if the target is there or not. If it is there, we'll return. Else, we'll continue to the next generation. And so on till 350 generations.
So, if we did not find element, then we'll return minus one. Else, we would have returned our answer in the middle itself.
So, how to maintain how to write the code? We can simply maintain a set because we only want the distinct tuples. So, maintain set of tuple. So, for a tuple, we can have vector of three elements or we can maintain two pairs.
Pair of pair of in comment comment. And then we have a tuple also, which is very useful here. So, we can simply maintain array of int {comma} three. So, this is like a three vector size element.
So, this is what the All right, so we are storing in the set.
So, initially maintain the set and then loop through all the initial values and then push into current. So, this is the zeroth generation. Now, if the current contains this target, Sorry, if Yeah, if count contains the target values, then simply return zero because it is in the zeroth generation itself. Else, you can add any element here because this will be broken sometime in the middle itself. So, loop till any number of generation and then maintain the next set of points in the next generation. How to find it? It is very easy. So, loop through all the before points.
Loop through every distinct J and I and then find the net point new point. So, add the new point into next. And with the new points, we can maintain the previous points as well. So, these are the previous points. So, in the new vector, add the before generation all the points and then for all the before points, loop through each and every value and then add new pair formed for every double.
New double formed for every two distinct doubles.
And then add to the vector. And then if we simplify this, then return the generation.
And if this is equal to before value, it means no points can be increased. So, simply break the loop. Else, current is equal to next.
And if you're unable to find, then return minus one.
If you have any doubts, comment below.
See you in the next video.
Ähnliche Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











