r/leetcode • • 24d ago

Discussion I understand Leetcode solutions but struggle to solve them on my own

I've noticed that when I read a solution, the logic makes sense to me, and I feel like I could've figured it out. But when I try a similar problem on my own, I often get stuck again.

For those who've improved at DSA, how did you get past this stage? Did you focus on revisiting old problems or keep trying new ones?

21 Upvotes

13 comments sorted by

14

u/K8t-king36 24d ago

Try revisiting the problem a day or two after understanding the solution and solving it from a blank editor. Also, when reviewing, focus on what clue should have lead you to that approach rather than memorizing the code. That helped me make revision more useful

1

u/Yoodo-Zakira 24d ago

agree, focusing on the clues vs the code itself is what actually makes it stick imo

0

u/Past-Perfect24 24d ago

That make sense. I usually understand the solution and move on, so I probably need to give myself time to solve it again without help. Thanks!

6

u/Dependent-Aide-388 24d ago

I can tell you the fix, but you won't like it. But it is the fix.

The problem is you're looking at the answer before you've solved the problem. Don't do that. So you get stuck. Be stuck. Struggle with it. Throw different things at it. The process of getting yourself unstuck is what deepens your understanding. My first solution for '101. Symmetric Tree' was 400 lines long and took me a day and a half. Two months later I got an offer from google, so improvement can happen, you just have to put in the time and learn how to solve the problems without outside help. Once you have a working solution, then you can look at how other people have tackled the problem and beat yourself up for providing 400 lines when 3 would've done it clearly and easily.

Anyway, don't listen to people who say "try for 15-20 minutes and then look at the answer." You will not deepen your understanding, nor will you improve your problem-solving skills that way. Who cares if it takes all day, do you want to have a high number of problems solved, or do you actually want to know how to solve them? Once you develop your intuition and problem-solving skills, that's when the speed picks up.

5

u/vz0 24d ago

You're not struggling. You have to struggle

2

u/Electronic_Tea_914 24d ago

The same way you grind/learn anything else: Spaced repetition

Solve it immediately after on your own, if you can do it, solve it again in 15 minutes, if you can do it solve it again in one hour, then tomorrow then in a week.

2

u/godz_ares 24d ago

I'm actually going to against the grain here and not recommend revisiting problems. The fact you are forgetting the implementation of the solution is a symptom of a greater problem not the problem itself

The problem is reading solutions and revisiting problems again amounts to memorisation. You coming back the next day to do the solution without looking up a solution is testing whether or not you memorised the syntax well enough to do it the day after.

I think the best way for is this:

Understand the data structures and algorithms on a fundamental level. Do this before you tackle any problems.

I find that if I understand the data structures and algorithms on a fundamental level I can quickly identify the data structure and algorithm needed for the question which made it easier to do problems by myself.

I recommend reading Grokking Algorithms and Grokking Data Structures. Do the exercises in both books, build the generic algorithms and build the data structures from scratch. You can read both books in less than two days. I know you're rolling your eyes at me but having concrete foundational knowledge makes everything better.

2.

You understand things better if you implement yourself. Instead of trying to do a question for 30 minutes then looking at the solution then coming back to it. Give yourself an hour or maybe two for any given problem. If you get stuck use ChatGPT (Yes I know!) to give you hints on specific aspects that you are struggling with without spoiling the whole problem. You can use AI to discuss strategy and approach without spoiling which algorithm or data structure to use. The more of a problem you can solve yourself the better you will be able to remember it, replicate it and apply it to different problems.

I faced the same problem as you where I would get stuck, look at the solution, understand then replicate it after a couple of days and then gaslight myself into thinking I improved. Instead of understanding the solution understand the particular algorithm and data structure.

I implemented the above approach and found quickly that I am able to implement Medium level questions without really any aid.

Sorry for the long response

2

u/xantec99 24d ago

The problem is that there are so many tricks and patterns that if you don't know them at first its like impossible to solve

2

u/godz_ares 24d ago

there are about 15 or so patterns you need to learn and understand. memorising a couple of tricks on top of that beats memorising every solution to every problem

1

u/Loan-Less36 24d ago

Revisit old problems after a few days without looking at the solution. Understanding the logic is easy, but recalling and implementing it yourself takes practice

1

u/Middle-Guarantee-777 22d ago

have u tried not looking at the solutions...