You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: problems/719/jeremymanning.md
+47Lines changed: 47 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -167,3 +167,50 @@ class Solution:
167
167
💩!
168
168
169
169
Ugh. I'm going to need to come back to this 😞...too tired to think!
170
+
171
+
## The next day...
172
+
- I've now gone through this code many times. I think it's *very close* to being right, but I think that I'm missing one or more important edge cases.
173
+
- I think the heap-based approach may actually be over-complicating the problem. Instead, what if we think this through in a different way:
174
+
- Let's start by sorting `nums`. This is cheap ($O(n \log n)$, where $n$ is `len(nums)`) and will almost certainly make the problem easier. The *maximum* distance is now `nums[-1] - nums[0]`. The *minimum* distance (if we have any repeats) is 0. Note: we might need to actually *see* if we have any repeats. We'll come back to this.
175
+
- What if we do a sort of "binary search" approach? Something like:
176
+
- Set `left, right = 0, nums[-1] - nums[0]`. Note: again, I'm not sure if `left` is set correctly. Come back to this...
177
+
- Now find the midpoint. This is just `mid = (left + right) // 2`.
178
+
- Now we have to find the "rank" of `mid` in the distances. To do this:
179
+
- For each index `i` in `range(len(nums))`:
180
+
- Increment a second counter, `j`, until `nums[j] - nums[i]` grows larger than `mid`.
181
+
- Now we can increment a `count` (of the number of distances) by `j - i - 1` -- this tracks how many distances are less than or equal to `mid`.
182
+
- After looping through every number in the outer loop, if `count >= k`, then we know that `mid` is too high. So we can set the `right` bound to `mid`. Alternatively, if `count < k`, we know that `mid` is too *low*, so we can set the `left` bound to `mid + 1`. This halves our search space with each iteration of the outermost loop.
183
+
- Once `left >= right` we should break out of the outer loop and return `left`. This should be exactly equal to the `k`th smallest distance.
184
+
- Caveats:
185
+
- It's possible the "correction by 1" line (incrementing `count` by `j - i - 1`) is off, and we should instead increment `count` by `j - i`. Let's keep track of this as a possible explanation for why things fail.
186
+
- It's possible we should return `right` at the end instead of `left`. Again, let's track this as a possible fail point. We may need to walk through the full thing...
0 commit comments