LEARNING JOURNAL

I created this learning journal to practice writting and to help me learn by writting everything down. Source code here

1/20/2024

Number of Unique Categories #2 attempt

This is my second attempt at Number of Unique Categories problem. Some lessons learned today:

  1. The roots property has its name for a reason. It is not an array of parents but an array of roots. This is why on findRoot, on the way back from the root, we need to mark each element's root to be the real root. If we use parents instead of roots, each time we find the root, we need to perform multiple recursive calls to traverse to the root. When using roots, we only call findRoot at most twice. If x is the root, we call it once. If x is not the root, we call it twice because the second call will reach the root.
  2. When the job of finding or getting something expensive or complicated, the best way is to use some variable to store the result when it just appears. It makes our lives much easier.