Playing Around on LeetCode
I have recently started practicing problems on LeetCode again to keep my mind fresh on solving problems. Right now I plan to do 1–2 a week when I get free time and range from easy to medium (I know what a pussy I am).
Anyways I decided to play around with one problem after solving it and just wanted to show off some code golf and how much it doesn’t matter to LeetCode runtime and memory usage percentiles.
Before starting here’s the reference to the problem: https://leetcode.com/problems/count-items-matching-a-rule/
Side Note: If you’re getting back into being a problem solver extraordinaire this is a good problem to start on.
Now the problem itself is fairly simple in nature if you utilize a dictionary/map to map indexes of the nested list to the ruleKey
variable.
Here’s my initial solution:
class Solution(object):
def countMatches(self, items, ruleKey, ruleValue):
"""
:type items: List[List[str]]
:type ruleKey: str
:type ruleValue: str
:rtype: int
"""
match_rule_count = 0
rule_indexes = {
'type': 0,
'color': 1,
'name': 2
}
rule_index = rule_indexes[ruleKey]
for item in items:
if item[rule_index] == ruleValue:
match_rule_count += 1
return match_rule_count
Hope you appreciate the variable names as much as I do.
However, I submitted the solution you get this as a result:
I never really actually tried to manipulate this before so I decided to play around for the hell of it.
First attempt I tried to improve by code golfing the solution and removed as many characters as possible:
class Solution(object):
def countMatches(self, i, k, v):
c, s = 0, {'type': 0,'color': 1,'name': 2}
a = s[k]
for j in i:
if j[a] == v:
c += 1
return c
This abomination resulted in this:
So the runtime got worse, who knows maybe the servers had a slow day on my free tier account.
However, I persisted and attempted to do one more attempt to simplify the solution down to this:
class Solution(object):
def countMatches(self, i, k, v):
c, s = 0, {'t': 0,'c': 1,'n': 2}
a = s[k[0]]
for j in i:
if j[a] == v:
c += 1
return c
However, the result remained the same. Essentially what I learned is that no matter how much you code golf a solution the result overall doesn’t improve performance that much (who knows maybe a different language works better).
Finally after realizing I only need to compare the first letter of the keyValue
and do a full comparison of the actual value the need for a map was essentially not necessary (don’t do this in day to day coding). This resulted in my final masterpiece of code:
class Solution(object):
def countMatches(self, i, k, v):
c = 0
for j in i:
if (k[0] == 't' and j[0] == v) or (k[0] == 'c' and j[1] == v) or (k[0] == 'n' and j[2] == v):
c += 1
return c
Which gave me the result:
Overall it didn’t really change anything except 4 ms faster in runtime. I don’t really know how the LeetCode environment works as well, but I thought it was fun to experiment around with code to see what is the bare minimum necessary and see what the result were.
I’d be interested to hear feedback of how I could make this code use less memory and run faster if anyone has any great ideas.
Conclusion
Overall I like this setup, I’d be interested to see if I could optimize any other solutions I end up making down the line, but this was a fun way to get back into the coding challenge mindset. Also, I’d recommend breaking down problems in as many ways as possible for personal learning as it can open up some interesting solutions you thought of (outside of just trying to code golf it). One thing I’d like to improve on with this solution even is trying to using the current list to keep track of a running total using list comprehension instead of a for loop.