Its not submitting , btw on ide its running correctly of the given case ...and i have taken help from gfg also
Hey @Muskan-Gupta-598128740703036
Your solution works in N^2. It should work in N time. N is number of cells in grid.
Save all pond sizes beforehand. Then at each position where there is a zero check all its neighbours. For each neighbour add its size. Note : Same neighbour may be on two sides so make sure to add it only once.
Hope this helps.
it is showing tle on 2 test cases…can you figure out why?
is this kind of approach is right???
@Muskan-Gupta-598128740703036
Lets say your input is
0 1 1
1 0 1
1 0 0
After DFS your array should be like
0 2 2
3 0 2
3 0 0
All values with 2 belong to same pond
All with 3 belong to same pond
Now for each unique value ( 2,3 here ) store the size of their pond
Then iterate the 2D array, when you see a 0, see its neighbours. If a neighbour is non zero then push its value it to a vector. Remove duplicates from this array. Then do 1 + sum of sizes of ponds of values in array.
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.