Please help me optimize my code.
my code :
Getting TLE in some test cases of Expand the pond
Just do Floodfill and mark the connected area with unique numbers.
For ex -:
1 0 0 0 1
1 0 1 1 1
0 1 0 0 0
Then after flood fill it will look like this -:
2 0 0 0 3
2 0 3 3 3
0 4 0 0 0
So now all you have to do is to check for every zero to extend your area.
Check in all four direction ( L R U D ) , and add that to set to avoid duplicates.
Then traverse the set and check which one makes the biggest area expansion.
You can refer this code for better understanding -:
But how can I optimize my approach.
Tell me your approach.
I added comments in my code that i have already posted.Please refer to that code .
Check your code complexity , at line number 82 , you are traversing every pond and then all its neighbour’s which are zero ,So in a grid of 1000x1000 , number of zeroes can be large and same for pond. You have to only iterate over the zeroes and check its all four direrction to expand some pond area.
Please refer above explanation and code.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.