LeetCode Drawing - Day 1

Python is slow. So, even a constant factor improvement sometimes matters a lot.

Choosing the language

I decided to use Python 3 for my coding challenges 2022. I used TypeScript for the same purpose last year. I used to love, and still love, TypeScript for its flexibility suitable for timed coding sessions. However, my current work has nothing to do with it and my proficiency in it has become so blunt. On the other hand, I have written many Python scripts here and there in my current work to the extent that it has overtaken the seat of my top scripting language.

1: Two Sum

Take away

You can fill auxiliary data structures (idxDict, in this question's case) as you check the termination condition in a loop. My original strategy was to populate idxDict by iterating through the entire input string, and check if we can make a pair by iterating the string again for the second time looking up the possible counterpart in the idxDict. After looking at the discussion page of this question, I rewrote the code in a way that we only have to go through the input string once. Both approaches are O(n), so there is no difference in theory. However, Python is usually very slow. So, it's good to know techniques to reduce the computation time even though the gain is just a constant factor in real world engineering, I think.

2: Add Two Numbers

Take aways

  • Group items to make each iteration explicit in the picture
  • You can draw pointers as a character p

3: Longest Substring Without Repeating Characters

Take away

Write indices over a string, and write the start and end indices on top of them. It is important to keep all the key information always visible because it offloads the burden of imaging what is going on. This way, we can have focus on the discussion about our algorithms .