Sorta floundered about and got here:
But what I was missing was an idea for how to sort the map, and really everything on this last crazy line in this example I found:
So sick! The sorting function, map to return first element, and slice I probably would have gotten to eventually, but they were sort of overwhelming me.
In the context of interviewing, it definitely would be a good idea to just break them down into multiple lines, verbosely if necessary, any way I knew how.
But it is also nice to see this succinct way of doing things.
Sweet, reproduced it:
I think the thing I grok the least here is the sort function to be honest. How does b-a
evaluate in a useful way? It’s clearly an iteration over the map of some kind, that evaluates relationships.
Okay so looking here, and there is an optional compare function that can be passed in, which defines how custom sorting happens.
The compare function has the following form:
Okay…that makes sense, but doesn’t really apply to b[1]-a[1]
as far as I can tell, because this statement could return all sorts of integers, positive or negative, besides 1 and -1.
Ah okay, here’s the real definition:
compareFn(a, b) return value | sort order |
---|---|
> 0 | sort a after b , e.g. [b, a] |
< 0 | sort a before b , e.g. [a, b] |
=== 0 | keep original order of a and b |
Okay let me just grok this in human language. |
If we run our compare function with arguments (a,b)
, if return value is more than 0
, sort a after b. So positive leads to b,a
, ordering. If a is 10
, and be is 5
, then a-b
would result in a positive, and this to b,a
sorting order.
Return 1
Time to completion: 3:47
Tried using const numMap: Map<number, number[]>
which is I supposed an approach that could work, but is probably an unnecessary expensive space complexity.
I did find myself wondering if there was a more efficient way to do the type of map manipulation I am doing, where I check if a map has an entry and then immediately change that entry, otherwise setting it to a default value.
First off, I have to ask the question, why use .has
instead of just .get
on maps?
It turns out that one reason is that if the key to get is falsy, this can kind of mess things up. Not really an issue here though.
Claude says we can get ride of a TON of lines:
Wow. Okay so we have this forEach…then for each, we TRY to get a value, and if it’s undefined, we default it to 0, and we add 1 regardless. Beautiful! This is so cool. Let me try to implement this from memory.
Wow, got it!
Time to completion: 3:16
Technically a three liner. I like how claude split up the dots. Also, the 3rd line is definitely not readable, but it is cool that it CAN be one line.