The Zen of grinding LeetCode problems: Day 8 — A balanced challenge

Danut Matei
CodeX
Published in
3 min readJun 13, 2022

Hello again. This is the LeetCode daily practice series. Today’s summary: 2 problems in 30 minutes, 1 SQL, 1 Python. Perfectly balanced, as all things should be.

A little bit of math

Insights:

  • The idea for the solution was to check how many apparitions each number has in the list, and then compute the result based on that
  • Since each pair of such apparitions matters, we need to use the mathematical formula for the number of unordered pairs, which is n * (n-1) / 2. This is guaranteed to be divisible by 2, but we use the // operator to get an integer result
  • I also thought about first sorting the array, then going through it and aggregating the count for each number, but I’ve figured out that using a dictionary would be more efficient than sorting (O(n) versus O(nlogn))

A worthy SQL opponent

Insights:

  • This SQL problem was the most difficult I have solved in this series, but it helped me get accustomed to the workflow: first you write a small query, test it, then add some extra code, test it again and so on…
  • The nvl() function is very useful when you expect to get some null values in your query, whether because of a left join with no correspondence or just from the data itself. It replaces the null values in an expression with the value you provide as a second argument
  • One key mistake I made which cost me a lot of time was assuming I can just group by name when I add up the distances. As I later found out, you can have multiple users with the same name, so I had to rely on the id instead, which is unique

Closing thoughts:

  • I feel like I’m getting better at both SQL and Python, even though I’ve been doing this for just a week. I really needed a refresher for SQL, but it’s surprising to see that even basic Python problems can sometimes challenge you and help you learn things.
  • On a more personal note, in the last couple of days I’ve been rather stressed because of external factors. Because of this, I was considering if I have to give up on some commitments I’ve made in order to have time for everything.
  • But I’ve realized that this is not the way. As longs as you can work on the things you set out to do, without any compromise, then you will be in control, even if the external factors get worse.
  • If, on the contrary, you decide that you don’t have the motivation or the mental energy for everything and you decide to free up some time for yourself, this can backfire. Any free moment your mind has to wonder around, it will hone in on the worst possible thing that can happen, and that will drain you.
  • So remember: move forward, everyday, no matter what happens in the world, and you will be free to develop to the full extent of your potential

--

--

Danut Matei
CodeX
Writer for

Machine Learning Engineer with a passion for Data Science and Statistics. Sharing insights on both latest trends and obscure topics in AI and Data