mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 17:09:46 +08:00
deploy
This commit is contained in:
@@ -60,7 +60,18 @@
|
||||
|
||||
|
||||
|
||||
</head>
|
||||
<link href="../../assets/stylesheets/glightbox.min.css" rel="stylesheet"/><style>
|
||||
html.glightbox-open { overflow: initial; height: 100%; }
|
||||
.gslide-title { margin-top: 0px; user-select: text; }
|
||||
.gslide-desc { color: #666; user-select: text; }
|
||||
.gslide-image img { background: white; }
|
||||
|
||||
.gscrollbar-fixer { padding-right: 15px; }
|
||||
.gdesc-inner { font-size: 0.75rem; }
|
||||
body[data-md-color-scheme="slate"] .gdesc-inner { background: var(--md-default-bg-color);}
|
||||
body[data-md-color-scheme="slate"] .gslide-title { color: var(--md-default-fg-color);}
|
||||
body[data-md-color-scheme="slate"] .gslide-desc { color: var(--md-default-fg-color);}
|
||||
</style> <script src="../../assets/javascripts/glightbox.min.js"></script></head>
|
||||
|
||||
|
||||
|
||||
@@ -2027,14 +2038,6 @@
|
||||
10.2 二分查找插入点
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2055,14 +2058,6 @@
|
||||
10.3 二分查找边界
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2482,14 +2477,6 @@
|
||||
第 12 章 分治
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -2521,14 +2508,6 @@
|
||||
12.1 分治算法
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2549,14 +2528,6 @@
|
||||
12.2 分治搜索策略
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2577,14 +2548,6 @@
|
||||
12.3 构建树问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2605,14 +2568,6 @@
|
||||
12.4 汉诺塔问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2633,14 +2588,6 @@
|
||||
12.5 小结
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2872,14 +2819,6 @@
|
||||
第 14 章 动态规划
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -2911,14 +2850,6 @@
|
||||
14.1 初探动态规划
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2939,14 +2870,6 @@
|
||||
14.2 DP 问题特性
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2967,14 +2890,6 @@
|
||||
14.3 DP 解题思路
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -2995,14 +2910,6 @@
|
||||
14.4 0-1 背包问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3023,14 +2930,6 @@
|
||||
14.5 完全背包问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3051,14 +2950,6 @@
|
||||
14.6 编辑距离问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3079,14 +2970,6 @@
|
||||
14.7 小结
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3145,14 +3028,6 @@
|
||||
第 15 章 贪心
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
|
||||
@@ -3184,14 +3059,6 @@
|
||||
15.1 贪心算法
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3212,14 +3079,6 @@
|
||||
15.2 分数背包问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3240,14 +3099,6 @@
|
||||
15.3 最大容量问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3268,14 +3119,6 @@
|
||||
15.4 最大切分乘积问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -3296,14 +3139,6 @@
|
||||
15.5 小结
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
@@ -4050,7 +3885,7 @@
|
||||
<li>算法 <code>B</code> 中的打印操作需要循环 <span class="arithmatex">\(n\)</span> 次,算法运行时间随着 <span class="arithmatex">\(n\)</span> 增大呈线性增长。此算法的时间复杂度被称为“线性阶”。</li>
|
||||
<li>算法 <code>C</code> 中的打印操作需要循环 <span class="arithmatex">\(1000000\)</span> 次,虽然运行时间很长,但它与输入数据大小 <span class="arithmatex">\(n\)</span> 无关。因此 <code>C</code> 的时间复杂度和 <code>A</code> 相同,仍为“常数阶”。</li>
|
||||
</ul>
|
||||
<p><img alt="算法 A、B 和 C 的时间增长趋势" src="../time_complexity.assets/time_complexity_simple_example.png" /></p>
|
||||
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_simple_example.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="算法 A、B 和 C 的时间增长趋势" src="../time_complexity.assets/time_complexity_simple_example.png" /></a></p>
|
||||
<p align="center"> 图 2-7 算法 A、B 和 C 的时间增长趋势 </p>
|
||||
|
||||
<p>相较于直接统计算法运行时间,时间复杂度分析有哪些特点呢?</p>
|
||||
@@ -4212,7 +4047,7 @@ T(n) = 3 + 2n
|
||||
<p>若存在正实数 <span class="arithmatex">\(c\)</span> 和实数 <span class="arithmatex">\(n_0\)</span> ,使得对于所有的 <span class="arithmatex">\(n > n_0\)</span> ,均有 <span class="arithmatex">\(T(n) \leq c \cdot f(n)\)</span> ,则可认为 <span class="arithmatex">\(f(n)\)</span> 给出了 <span class="arithmatex">\(T(n)\)</span> 的一个渐近上界,记为 <span class="arithmatex">\(T(n) = O(f(n))\)</span> 。</p>
|
||||
</div>
|
||||
<p>如图 2-8 所示,计算渐近上界就是寻找一个函数 <span class="arithmatex">\(f(n)\)</span> ,使得当 <span class="arithmatex">\(n\)</span> 趋向于无穷大时,<span class="arithmatex">\(T(n)\)</span> 和 <span class="arithmatex">\(f(n)\)</span> 处于相同的增长级别,仅相差一个常数项 <span class="arithmatex">\(c\)</span> 的倍数。</p>
|
||||
<p><img alt="函数的渐近上界" src="../time_complexity.assets/asymptotic_upper_bound.png" /></p>
|
||||
<p><a class="glightbox" href="../time_complexity.assets/asymptotic_upper_bound.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="函数的渐近上界" src="../time_complexity.assets/asymptotic_upper_bound.png" /></a></p>
|
||||
<p align="center"> 图 2-8 函数的渐近上界 </p>
|
||||
|
||||
<h2 id="233">2.3.3 推算方法<a class="headerlink" href="#233" title="Permanent link">¶</a></h2>
|
||||
@@ -4472,7 +4307,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
||||
\text{常数阶} < \text{对数阶} < \text{线性阶} < \text{线性对数阶} < \text{平方阶} < \text{指数阶} < \text{阶乘阶}
|
||||
\end{aligned}
|
||||
\]</div>
|
||||
<p><img alt="常见的时间复杂度类型" src="../time_complexity.assets/time_complexity_common_types.png" /></p>
|
||||
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_common_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="常见的时间复杂度类型" src="../time_complexity.assets/time_complexity_common_types.png" /></a></p>
|
||||
<p align="center"> 图 2-9 常见的时间复杂度类型 </p>
|
||||
|
||||
<h3 id="1-o1">1. 常数阶 <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#1-o1" title="Permanent link">¶</a></h3>
|
||||
@@ -5073,7 +4908,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
||||
</div>
|
||||
</div>
|
||||
<p>图 2-10 对比了常数阶、线性阶和平方阶三种时间复杂度。</p>
|
||||
<p><img alt="常数阶、线性阶和平方阶的时间复杂度" src="../time_complexity.assets/time_complexity_constant_linear_quadratic.png" /></p>
|
||||
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_constant_linear_quadratic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="常数阶、线性阶和平方阶的时间复杂度" src="../time_complexity.assets/time_complexity_constant_linear_quadratic.png" /></a></p>
|
||||
<p align="center"> 图 2-10 常数阶、线性阶和平方阶的时间复杂度 </p>
|
||||
|
||||
<p>以冒泡排序为例,外层循环执行 <span class="arithmatex">\(n - 1\)</span> 次,内层循环执行 <span class="arithmatex">\(n-1\)</span>、<span class="arithmatex">\(n-2\)</span>、<span class="arithmatex">\(\dots\)</span>、<span class="arithmatex">\(2\)</span>、<span class="arithmatex">\(1\)</span> 次,平均为 <span class="arithmatex">\(n / 2\)</span> 次,因此时间复杂度为 <span class="arithmatex">\(O((n - 1) n / 2) = O(n^2)\)</span> 。</p>
|
||||
@@ -5534,7 +5369,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p><img alt="指数阶的时间复杂度" src="../time_complexity.assets/time_complexity_exponential.png" /></p>
|
||||
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_exponential.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="指数阶的时间复杂度" src="../time_complexity.assets/time_complexity_exponential.png" /></a></p>
|
||||
<p align="center"> 图 2-11 指数阶的时间复杂度 </p>
|
||||
|
||||
<p>在实际算法中,指数阶常出现于递归函数中。例如在以下代码中,其递归地一分为二,经过 <span class="arithmatex">\(n\)</span> 次分裂后停止:</p>
|
||||
@@ -5800,7 +5635,7 @@ O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p><img alt="对数阶的时间复杂度" src="../time_complexity.assets/time_complexity_logarithmic.png" /></p>
|
||||
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_logarithmic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="对数阶的时间复杂度" src="../time_complexity.assets/time_complexity_logarithmic.png" /></a></p>
|
||||
<p align="center"> 图 2-12 对数阶的时间复杂度 </p>
|
||||
|
||||
<p>与指数阶类似,对数阶也常出现于递归函数中。以下代码形成了一个高度为 <span class="arithmatex">\(\log_2 n\)</span> 的递归树:</p>
|
||||
@@ -6087,7 +5922,7 @@ O(\log_m n) = O(\log_k n / \log_k m) = O(\log_k n)
|
||||
</div>
|
||||
</div>
|
||||
<p>图 2-13 展示了线性对数阶的生成方式。二叉树的每一层的操作总数都为 <span class="arithmatex">\(n\)</span> ,树共有 <span class="arithmatex">\(\log_2 n + 1\)</span> 层,因此时间复杂度为 <span class="arithmatex">\(O(n \log n)\)</span> 。</p>
|
||||
<p><img alt="线性对数阶的时间复杂度" src="../time_complexity.assets/time_complexity_logarithmic_linear.png" /></p>
|
||||
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_logarithmic_linear.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="线性对数阶的时间复杂度" src="../time_complexity.assets/time_complexity_logarithmic_linear.png" /></a></p>
|
||||
<p align="center"> 图 2-13 线性对数阶的时间复杂度 </p>
|
||||
|
||||
<p>主流排序算法的时间复杂度通常为 <span class="arithmatex">\(O(n \log n)\)</span> ,例如快速排序、归并排序、堆排序等。</p>
|
||||
@@ -6265,7 +6100,7 @@ n! = n \times (n - 1) \times (n - 2) \times \dots \times 2 \times 1
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p><img alt="阶乘阶的时间复杂度" src="../time_complexity.assets/time_complexity_factorial.png" /></p>
|
||||
<p><a class="glightbox" href="../time_complexity.assets/time_complexity_factorial.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="阶乘阶的时间复杂度" src="../time_complexity.assets/time_complexity_factorial.png" /></a></p>
|
||||
<p align="center"> 图 2-14 阶乘阶的时间复杂度 </p>
|
||||
|
||||
<p>请注意,因为当 <span class="arithmatex">\(n \geq 4\)</span> 时恒有 <span class="arithmatex">\(n! > 2^n\)</span> ,所以阶乘阶比指数阶增长得更快,在 <span class="arithmatex">\(n\)</span> 较大时也是不可接受的。</p>
|
||||
@@ -6777,10 +6612,15 @@ aria-label="页脚"
|
||||
<div class="md-copyright">
|
||||
|
||||
<div class="md-copyright__highlight">
|
||||
Copyright © 2023 Krahets
|
||||
Copyright © 2022 - 2023 Krahets
|
||||
</div>
|
||||
|
||||
|
||||
Made with
|
||||
<a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
|
||||
Material for MkDocs
|
||||
</a>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Social links -->
|
||||
@@ -6849,5 +6689,5 @@ aria-label="页脚"
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
<script>document$.subscribe(() => {const lightbox = GLightbox({"touchNavigation": true, "loop": false, "zoomable": true, "draggable": false, "openEffect": "zoom", "closeEffect": "zoom", "slideEffect": "none"});})</script></body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user