AndrijaMlad's blog

By AndrijaMlad, history, 16 months ago, In English

Hello, I have a question for all the REDS/yellows on codeforces, How do you learn algorithm fast?

  • Vote: I like it
  • -18
  • Vote: I do not like it

»
16 months ago, # |
  Vote: I like it +24 Vote: I do not like it

I'm neither red nor yellow, but my advice would be to make sure you fully understand the algorithms you're implementing. You should be able to explain how (and ideally why, although this gets more difficult with advanced algorithms) the algorithm works. From there, it comes down to translating thoughts into code, and that gets easier with practice.

IMO, you should never have to memorize the exact code for an algorithm. At most, you should remember a few implementation tricks specific to the algorithm.

»
16 months ago, # |
  Vote: I like it +11 Vote: I do not like it

Why cope? If I don't understand, it's fun to understand it a bit more each time. If I understand it, it's just a few minutes of coding, so not a big deal.

In general, if you need to code something a 100 times before you're confident, you should probably go back to studying it and understanding it better. Or just learn something else and give it some time — sometimes it takes time for it to settle in your head and to mature as a coder to understand something fully (I wouldn't say I fully understood DFS for the first 4-5 years of my career, even though I could probably code it blindfolded).

  • »
    »
    16 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    What do you mean by you didn't fully understand DFS? And what parts of BFS does OP not understand before they coded 100 times?

    • »
      »
      »
      16 months ago, # ^ |
        Vote: I like it +9 Vote: I do not like it

      Well, I pretended I knew DFS, but I would just memorize algorithms like AP/Bridge finding or SCC, which are just based on DFS properties. One day it just clicked, and I could code them like I was coding any other DFS problem.

      The in-out orderings, the all-edges-are-backedges property are very simple when you read about them, but using them creatively in solutions took me that long.

  • »
    »
    16 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Vidim da ste iz Srbije, pa cu da vam pisem na srpskom :) Ja neznam zasto ali sam mislio da zada bi naucio algoritam trebam da ga kucam dok ga ne memorizujem, ali hvala na savetu. Potrudi cu se da ga primenujem u buducnosti. Imajte lep dan

»
16 months ago, # |
  Vote: I like it +11 Vote: I do not like it

I think you should put in more time to actually understand it rather than write it 100 times. What I usually did was to carefully study the code for the new algorithm I was trying to learn, then solve a few related easy/standard problems, while trying to write the code on my own, without looking at an example implementation of that new algo. This way, you kind of test if you really understood what you learnt.

»
16 months ago, # |
  Vote: I like it +42 Vote: I do not like it

you don't. doing something 100 times is stupid.

  • »
    »
    16 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    When I said 100 times I didn't mean like writhing DP 100 times for the Coin Change problem for example, but like you have to learn knapsack, Range DP, Com. DP, Digit DP... And at some point you learn all the things, and you now need to just solve solve Solve. I used BFS as an example, but My question was when you are bad at for example DP you should do more DP problems, but nobody likes to do what they are bad at. So how do you disciplin your self?

    • »
      »
      »
      16 months ago, # ^ |
        Vote: I like it +11 Vote: I do not like it

      Well, if you don't want to do smth, and you can't force yourself to do it, then there is no solution for it.

    • »
      »
      »
      16 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I really like Greedy, so like every time I code I tend to solve Greedy/math problems instead of DP problems

»
16 months ago, # |
  Vote: I like it +1 Vote: I do not like it

if you understand something, you don't have to write it even a single time to know how to use it :3 though practice still helps

»
16 months ago, # |
  Vote: I like it +1 Vote: I do not like it

I am cleary not qualified for this but I think that for learning any algorithm you don't need to write it a 100 times to learn it, don't try to memorize the algorithm, understand it and I think it will mostly take you 20-30 problems to get it.

»
16 months ago, # |
  Vote: I like it +1 Vote: I do not like it

I don't think i am enough qualified to say something . But when i learn some algorithm for example BFS DFS i try to understand the algorithm step by step . After understanding i try to implement it by myself without seeing the code on the basis of the steps if i fail then i see the code and try to understand how the code if working and logic behind it. After that I try to write that code for 2/3 time. After that I try to solve few easy questions of that algorithm . I learned BFS DFS like this 5/6 month ago and still didn't forget that.

»
16 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by AndrijaMlad (previous revision, new revision, compare).