
LeetCode Weekly Contest 496 - How I solved them
Overall a 6/10 difficulty contest I'd say.
EDIT: I wrote the above cause a lot of people ask for how hard the contests are, I'm just giving my opinion for that is all
Q1: Kind of an implementation battle. I recommend just making functions like mirror(char) to make it easy. Also a good trick to avoid duplicates is just to hash them like always put the smaller letter first, and store tuples of ones we have considered, in a set. so (a, z) goes into a set, and when we get to (z, a) it gets hashed to (a, z) and de-duped.
Q2: Since N is large we cannot enumerate it. Instead, observe the maximum of a single factor can be n^1/3. Now just enumerate pairs of these options which gives us n^2/3 complexity. Track how many numbers get at least two occurrences.
Q3: If N is odd it is easy, we make the indices 1, 3, 5, ... peaks.
If it is even we run into some tricky cases...
# 1 | (2) 3 (4) 5 (6) 7 | 8
# 1 | 2 (3) 4 (5) 6 (7) | 8
# 1 | (2) 3 (4) 5 6 (7) | 8
# 1 | (2) 3 4 (5) 6 (7) | 8
Numbers inside the () are peaks. We can basically put a single gap of 2 in multiple places. But note we cannot put it at i=1 or i=n-2
To find the best answer, I used a prefix + suffix to track the scores on the left and right regions, and enumerated the possible locations for the gap. Lots of edge cases.
Q4: dp(index, peaksLeft, didTakeFirst) is n x k x 2 complexity. A knapsack DP.
Note that this is too much in python so you can either attempt a cursed bottom-up solution that only uses K memory, but it's annoying since we have 2 dp arrays and need to preserve the last 2 layers for the recurrence. Or you can just use C++.
We could also observe that k is at most n/2 but even then it's sus in python.
