Skip to content

Commit 09e458c

Browse files
authored
docs: add a description of the solution to lcci problem: No.08.03 (doocs#760)
No.08.03.Magic Index
1 parent e3fa44a commit 09e458c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lcci/08.03.Magic Index/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@
3030

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

33+
**线性查找:**
34+
35+
遍历数组,当 `A[i] = i` 时直接返回即可。
36+
37+
**优化:**
38+
39+
在遍历的基础,进行可能的 "跳跃",结束时执行 `i = max(A[i], i + 1)`,而不再单纯 `i++`
40+
41+
可行性证明:
42+
43+
因为数组是**有序**的,若 `A[i] != i`,那么就可以将 `A[i]` 以下的可能全部排除,直接将 `i` 设定为 `A[i]`
44+
45+
若是考虑最糟的状况(所有元素都为负数),则该优化与遍历无差别。
46+
3347
<!-- tabs:start -->
3448

3549
### **Python3**

0 commit comments

Comments
 (0)