Skip to content

docs: add a description of the solution to lcci problem: No.08.03 #760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lcci/08.03.Magic Index/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@

<!-- 这里可写通用的实现逻辑 -->

**线性查找:**

遍历数组,当 `A[i] = i` 时直接返回即可。

**优化:**

在遍历的基础,进行可能的 "跳跃",结束时执行 `i = max(A[i], i + 1)`,而不再单纯 `i++`。

可行性证明:

因为数组是**有序**的,若 `A[i] != i`,那么就可以将 `A[i]` 以下的可能全部排除,直接将 `i` 设定为 `A[i]`。

若是考虑最糟的状况(所有元素都为负数),则该优化与遍历无差别。

<!-- tabs:start -->

### **Python3**
Expand Down