mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 13:12:44 +00:00
docs(content): set meta display to page and improve interval documentation formatting
This commit is contained in:
1 parent
2ac54e9930
commit
a7737502e1
3 files changed
+39
-37
No files matched your search
@@ -1,3 +1,8 @@
|
||||
import { defineMeta } from "blume";
|
||||
|
||||
export default defineMeta({ title: "Binary Search", order: 5, icon: "search", display: "group" });
|
||||
export default defineMeta({
|
||||
title: "Binary Search",
|
||||
order: 5,
|
||||
icon: "search",
|
||||
display: "page",
|
||||
});
|
||||
@@ -1,3 +1,8 @@
|
||||
import { defineMeta } from "blume";
|
||||
|
||||
export default defineMeta({ title: "Hash Table", order: 1, icon: "hash", display: "group" });
|
||||
export default defineMeta({
|
||||
title: "Hash Table",
|
||||
order: 1,
|
||||
icon: "hash",
|
||||
display: "page",
|
||||
});
|
||||
@@ -52,8 +52,13 @@ Sorting by start is the standard first line of almost every interval solution, a
|
||||
That gives the template every interval problem in this section builds on: sort by start, then sweep once, comparing each interval against the last one you kept.
|
||||
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Example">
|
||||
:::note[Input]
|
||||
`[[1,3], [8,10], [2,6], [4,5], [10,12]]`
|
||||
:::
|
||||
|
||||
:::note[After sort]
|
||||
`[[1,3], [2,6], [4,5], [8,10], [10,12]]`
|
||||
:::
|
||||
|
||||
```python
|
||||
# Time: O(n log n)
|
||||
@@ -73,16 +78,7 @@ def interval_pattern(intervals):
|
||||
return result
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab title="Trace">
|
||||
|
||||
:::note[Input]
|
||||
`[[1,3], [8,10], [2,6], [4,5], [10,12]]`
|
||||
:::
|
||||
|
||||
:::note[After sort]
|
||||
`[[1,3], [2,6], [4,5], [8,10], [10,12]]`
|
||||
:::
|
||||
<Expandable title="Show trace table">
|
||||
|
||||
| i | `[start, end]` | `result[-1]` | `start <= result[-1][1]`? | Action | `result` after |
|
||||
|---|---|---|---|---|---|
|
||||
@@ -99,10 +95,10 @@ def interval_pattern(intervals):
|
||||
- **Row 4:** `[8,10]` and `[10,12]` only touch at 10. The `<=` check merges them. With `<`, you would get two intervals.
|
||||
:::
|
||||
|
||||
</Tab>
|
||||
</Tabs>
|
||||
</Expandable>
|
||||
|
||||
## Variant: Insert into a sorted list
|
||||
|
||||
## Insert into a sorted list
|
||||
|
||||
Sometimes the list is already sorted and has no overlaps, and you must add one new interval. You could append it, sort, and run the template. That works, but it pays `O(n log n)` for a sort you do not need.
|
||||
|
||||
@@ -114,17 +110,18 @@ A sorted list with no overlaps splits into three parts around the new interval,
|
||||
|
||||
Walk the list once and handle each part in turn. No sort, so the cost drops to `O(n)`.
|
||||
|
||||
:::warning
|
||||
If the input is already sorted, do not sort again. Interviewers add "sorted" to the problem on purpose. Name the `O(n log n)` cost of re-sorting before they do, then offer the `O(n)` three-part walk.
|
||||
:::note[Intervals]
|
||||
`[[1,2], [3,5], [6,7], [8,10], [12,16]]`
|
||||
:::
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Example">
|
||||
:::note[New interval]
|
||||
`[4,8]`
|
||||
:::
|
||||
|
||||
```python
|
||||
# Time: O(n)
|
||||
# Space: O(n)
|
||||
def insert_interval(intervals, new_interval):
|
||||
```python
|
||||
# Time: O(n)
|
||||
# Space: O(n)
|
||||
def insert_interval(intervals, new_interval):
|
||||
result = []
|
||||
i, n = 0, len(intervals)
|
||||
start, end = new_interval
|
||||
@@ -144,18 +141,10 @@ If the input is already sorted, do not sort again. Interviewers add "sorted" to
|
||||
# 3. Starts after new ends: keep
|
||||
result.extend(intervals[i:])
|
||||
return result
|
||||
```
|
||||
```
|
||||
|
||||
</Tab>
|
||||
<Tab title="Trace">
|
||||
<Expandable title="Show trace table">
|
||||
|
||||
:::note[Intervals]
|
||||
`[[1,2], [3,5], [6,7], [8,10], [12,16]]`
|
||||
:::
|
||||
|
||||
:::note[New interval]
|
||||
`[4,8]`
|
||||
:::
|
||||
|
||||
| i | interval | Part | Check | Action | `[start, end]` | `result` after |
|
||||
|---|---|---|---|---|---|---|
|
||||
@@ -174,8 +163,11 @@ If the input is already sorted, do not sort again. Interviewers add "sorted" to
|
||||
- **Part 1 uses `<`, part 2 uses `<=`:** An interval that ends exactly at `start` touches the new interval. It must fail part 1 so part 2 can merge it.
|
||||
:::
|
||||
|
||||
</Tab>
|
||||
</Tabs>
|
||||
</Expandable>
|
||||
|
||||
:::warning
|
||||
If the input is already sorted, do not sort again. Interviewers add "sorted" to the problem on purpose. Name the `O(n log n)` cost of re-sorting before they do, then offer the `O(n)` three-part walk.
|
||||
:::
|
||||
|
||||
### Footnote
|
||||
|
||||
|
||||
Reference in new issue
Block a user