refactor(leetcode): standardize midpoint formula on problem #14 binary search approach
This commit is contained in:
1 parent
4d43ab07d2
commit
78c6f39510
2 files changed
+3
-6
No files matched your search
@@ -262,7 +262,7 @@ class Solution:
|
|||||||
return "".join(prefix)
|
return "".join(prefix)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Binary Search
|
### [Binary Search](../../knowledge/dsa/binary-search.md)
|
||||||
This is a classic Divide and Conquer approach. Rather than searching over the strings themselves, we search over the **possible prefix lengths**. The search space ranges from `0` to the length of the shortest string. For a candidate length `k`, we ask whether the first `k` characters are shared by every string.
|
This is a classic Divide and Conquer approach. Rather than searching over the strings themselves, we search over the **possible prefix lengths**. The search space ranges from `0` to the length of the shortest string. For a candidate length `k`, we ask whether the first `k` characters are shared by every string.
|
||||||
|
|
||||||
**Algorithm**:
|
**Algorithm**:
|
||||||
@@ -326,7 +326,7 @@ class Solution:
|
|||||||
answer = ""
|
answer = ""
|
||||||
|
|
||||||
while left <= right:
|
while left <= right:
|
||||||
mid = (left + right) // 2
|
mid = left + (right - left) // 2
|
||||||
if is_common_prefix(strs, mid):
|
if is_common_prefix(strs, mid):
|
||||||
answer = strs[0][:mid]
|
answer = strs[0][:mid]
|
||||||
left = mid + 1
|
left = mid + 1
|
||||||
@@ -368,12 +368,9 @@ Using `S` isn't wrong if you define it carefully, but I think `n` and `m` make i
|
|||||||
- **Why it works:** `zip(*strs)` creates an iterator of tuples representing "columns." `set()` provides an $O(1)$ way to verify if all items in a column are identical.
|
- **Why it works:** `zip(*strs)` creates an iterator of tuples representing "columns." `set()` provides an $O(1)$ way to verify if all items in a column are identical.
|
||||||
|
|
||||||
- **Trade-off:** `zip` will stop at the length of the shortest string automatically, which handles the bounds check elegantly. However, `list(zip(...))` creates an intermediate data structure in memory.
|
- **Trade-off:** `zip` will stop at the length of the shortest string automatically, which handles the bounds check elegantly. However, `list(zip(...))` creates an intermediate data structure in memory.
|
||||||
|
|
||||||
|
|
||||||
### 4. Binary Search
|
### 4. Binary Search
|
||||||
|
|
||||||
- **Why it works:** We treat the prefix length as the search space $[0, M]$.
|
- **Why it works:** We treat the prefix length as the search space $[0, M]$.
|
||||||
|
|
||||||
- **Trade-off:** Primarily valuable as an application of binary search over the solution space rather than for practical performance on typical inputs. This is overkill for most interview cases. Use it only when strings are exceptionally long, as it minimizes the number of character comparisons.
|
- **Trade-off:** Primarily valuable as an application of binary search over the solution space rather than for practical performance on typical inputs. This is overkill for most interview cases. Use it only when strings are exceptionally long, as it minimizes the number of character comparisons.
|
||||||
## Takeaways
|
## Takeaways
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class Solution:
|
|||||||
right = min(len(string) for string in strs)
|
right = min(len(string) for string in strs)
|
||||||
answer = ""
|
answer = ""
|
||||||
while left <= right:
|
while left <= right:
|
||||||
mid = (left + right) // 2
|
mid = left + (right - left) // 2
|
||||||
if is_common_prefix(strs, mid):
|
if is_common_prefix(strs, mid):
|
||||||
answer = strs[0][:mid]
|
answer = strs[0][:mid]
|
||||||
left = mid + 1
|
left = mid + 1
|
||||||
|
|||||||
Reference in new issue
Block a user