We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Ema's Supercomputer
Ema's Supercomputer
Sort by
recency
|
325 Discussions
|
Please Login in order to post a comment
Respectfully, this problem is not "medium", it's freaking hard! (ಥ﹏ಥ)
Heres is my c++ solutions, hope is helps someone.
MY SOLUTION IN PHP
If you fail only a few cases, consider smaller size pluses with the same center, as they might not overlapse while the biggest one does. Also, you might be curious about how much pearls are worth when selecting decorative elements for your design.
Since the input size is very small, we can use brute force approach:
TLDR:
for each G point, traverse in all four directions setp by step (ex: at step 2 you visit all the points which are at dist. 2 in all directions) till all the points in the step are valid G points and collect them at each step
sort them based on len in desc order, then check for all the combinations of such non overlapping plus points and find the max aread
CODE
Initial thoughts:
Approach 1:
start at each G point
traverse in all four directions at once (mark G to say -1 to identify as visited, so that there won't be any intersection with others)
now we have lenghts of all such pluses
find the top two and do product. Either sort the lenghts array or use heap and maintain only top two
Flaws with this approach:
Future G point, might need the current one (as it can be in the plus path of future G) since we are marking as -1, this will be ignored and we might loose the longest plus
Approach 2:
Okay, so what if we try to find the pluses that can be formed from each G point? find the top two such set of points without any intersection
find all the G points
from each G point, traverse in all the directions, collect all the points
sort the collection of points based on length, find top two with zero intersection and do a product of it
Flaws:
x = max(side of plus you can reach in all 4 directions), area = 4*x +1 ex: x=2, area = 4*2+1 = 9 say pluses of x=2 & x=3 are next to each other with one overlapping G point, then maxArea = area(3)*1 (as there is no other plus points) = 13 but if we somehow make x=3 to take only two G's in all directions? then maxArea = area(x=2)*area(x=2) = 81
Approach 3:
So now instead of considering only the points in the max plus path of each g point, consider all the plus points for each length.
Ex: for x=3, we will have in total of 4 collections points while going thorugh the directions, i.e start from x = 0 --> x=1 --> x=2 --> x=3 for a given G point
Now for x=2 & x=3 points, we have 0,1,2 & 0,1,2,3 lengths of collection points for each G point. Now sort the collection of plus points, find max among all the given pairs (not just top two non intersection points, as 2x1 & 3x2 can have intersection, then top two aread will become 1x1 * 3x2, but max is 2x1 *2X2)