I think that if you're stuck on a problem for more than 30 minutes, it's better to go read the solution and learn from it than to keep struggling. (Then go back a week later and see if you can solve the problem from memory).
Opinions certainly differ on whether leetcode makes for a good interviewing process. On your specific problem, there are a ton of similar problems where you need to traverse possible solutions in a particular order to be efficient. I regularly see similar problems at work.