ClosetNarcissist's blog

By ClosetNarcissist, history, 2 months ago, In English

Recently i did Leetcode Longest Common Subsequence problem. I used the basic dp approach and solved it in 18 ms using 12 mb space. After that i decided to check the fastest solutions for the problem and got this code

Fastest LCS code

I submitted it and it ran in 4 ms using 7.5 mb space. I tried to deconstruct the algorithm and understand how it was working. I cleaned the code a bit and got it to run in 3 ms, but I still could not understand it.

My optimized code

I could not find any relevant blogs explaining it. Can anyone help me understand how this works or link any blogs. Thanks.

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it

By ClosetNarcissist, 3 months ago, In English

In the recent CF round 931 for problem B I realized that I could not solve it normally using DP since n was too high. I knew that some coin systems can be solved greedily but it was not the case for this one. During the contest, I solved it like this: https://codeforces.com/contest/1934/submission/249165174

But later I saw that some people just pre-calculated the answer upto 30 or something and then used some mod tricks to get the answer. I thought they picked 30 since it was the LCM and so I tried to test my hypothesis by solving the CSES Minimizing Coin problem and it actually worked. I thought this was some optimization on the problem that I did not know about before. This was my solution: https://cses.fi/paste/dbb9ee26f083862b8352b1/

However, I soon realized that my method does not work for coin systems like {3, 6, 10, 15}, as my code would give impossible for the value 32, which is obviously wrong as it should really be 4. After that, I thought there had to be some conditions that had to be met for me to use this "lcm" trick (if this is even real or just something that coincidentally worked). But I could not think of any and I could not find any relevant articles, so I want to ask you guys to make me understand.

Thanks in advance. Btw before writing this blog, I thought I'd solve the original CF problem using the "lcm" trick but it did not give the correct answers lol.

Full text and comments »

  • Vote: I like it
  • +2
  • Vote: I do not like it