Generally, I can intuit the "brute force" solution, but the "right" answer with linear/constant time often requires looking at the answer.
Should I expect that with practice, I'll eventually get better at quickly finding the optimal solution? Or, is it expected that optimal solutions are "memorized" similar to how hard sciences like ME / EE / CE memorize formulas?
Take ThreeSum problem for example. I was able to write the brute force solution without any prior knowledge of the problem. In an interview with time constraints this WOULD have been my solution.
I now understand that's not the right solution. I've looked at the answer, and I have the "right" solution memorized. That kind of feels like cheating though Should I have been able to write that "right" solution straight away, with no prior exposure to the problem?
Just trying to understand what my goal is in this process, and what is considered "success" for my studies.
1. Categorize - Can I identify what type of problem this is? (ie Graph problem, bit problem, sliding window, etc.)?
2. Plan a solution - Can I come up with a strategy to solve it?
3. Based on how I felt about 1 and 2, am I confident enough to dive in and solve it?
3A. If yes, as a final check, go to the discussions and see if you can get some confirmation that you had the right approach just by reading the titles. For example, if you were thinking that this was a backtracking problem because that was the only way you thought it could be solved and you see that the most upvoted posts are talking about priority queus, you probably had the wrong approach. Otherwise if it looks like you were on the right track, only then should you go ahead and start coding it.
3B. If you had the wrong approach or if you weren't confident that you could solve it, then go ahead and learn from the solution.
You will learn a lot from the solutions that others posts but you will hardly learn anything by wasting hours on a flawed solution.
To be clear, to get good requires going well beyond the fundamentals, you need to learn from the best of the best in order to stand a chance.
Now we have programmers being tested in hour long interviews... how could they come up with this stuff on the spot?
There is a reason why people have to study for tech company interviews. Knowing the solution to a problem that is similar to the one that is being asked will go a long way in the interview.
for example, consider the maximum subarray problem (LC 53). this is a dynamic programming problem. but if you don't know DP, you could spend a long time staring at this problem without progress. even looking at the solutions won't be that informative if the concepts of DP are not familiar. MIT's 6.006 course takes four lectures to cover DP (not including the recitations!), which should tell you something.
Learning via attempting leetcode problems directly seems suboptimal, like trying to learn calculus by taking timed exams vs working through a basic text first.
my 2 cents. fwiw.
Should you be MEMORIZING solutions, or, expecting yourself to come up with the O(N)/O(1) solutions on your own?