mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-10 17:00:45 +08:00
deploy
This commit is contained in:
@ -26,7 +26,7 @@
|
||||
|
||||
|
||||
|
||||
<title>2.4 Space Complexity - Hello Algo</title>
|
||||
<title>2.4 Space complexity - Hello Algo</title>
|
||||
|
||||
|
||||
|
||||
@ -153,7 +153,7 @@
|
||||
<div class="md-header__topic" data-md-component="header-topic">
|
||||
<span class="md-ellipsis">
|
||||
|
||||
2.4 Space Complexity
|
||||
2.4 Space complexity
|
||||
|
||||
</span>
|
||||
</div>
|
||||
@ -201,7 +201,13 @@
|
||||
|
||||
<li class="md-select__item">
|
||||
<a href="/" hreflang="zh" class="md-select__link">
|
||||
中文
|
||||
简体中文
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="md-select__item">
|
||||
<a href="/zh-hant/" hreflang="zh-Hant" class="md-select__link">
|
||||
繁體中文
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@ -393,7 +399,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
0.1 About This Book
|
||||
0.1 About this book
|
||||
</span>
|
||||
|
||||
|
||||
@ -414,7 +420,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
0.2 How to Read
|
||||
0.2 How to read
|
||||
</span>
|
||||
|
||||
|
||||
@ -491,7 +497,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2m0 16H5V5h14v14M6.2 7.7h5v1.5h-5V7.7m6.8 8.1h5v1.5h-5v-1.5m0-2.6h5v1.5h-5v-1.5M8 18h1.5v-2h2v-1.5h-2v-2H8v2H6V16h2v2m6.1-7.1 1.4-1.4 1.4 1.4 1.1-1-1.4-1.4L18 7.1 16.9 6l-1.4 1.4L14.1 6 13 7.1l1.4 1.4L13 9.9l1.1 1Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 1. Introduction to Algorithms
|
||||
Chapter 1. Introduction to algorithms
|
||||
</span>
|
||||
|
||||
|
||||
@ -507,7 +513,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_2">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 1. Introduction to Algorithms
|
||||
Chapter 1. Introduction to algorithms
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -524,7 +530,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.1 Algorithms are Everywhere
|
||||
1.1 Algorithms are everywhere
|
||||
</span>
|
||||
|
||||
|
||||
@ -545,7 +551,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.2 What is an Algorithm
|
||||
1.2 What is an algorithm
|
||||
</span>
|
||||
|
||||
|
||||
@ -628,7 +634,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M6 2h12v6l-4 4 4 4v6H6v-6l4-4-4-4V2m10 14.5-4-4-4 4V20h8v-3.5m-4-5 4-4V4H8v3.5l4 4M10 6h4v.75l-2 2-2-2V6Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 2. Complexity Analysis
|
||||
Chapter 2. Complexity analysis
|
||||
</span>
|
||||
|
||||
|
||||
@ -644,7 +650,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_3_label" aria-expanded="true">
|
||||
<label class="md-nav__title" for="__nav_3">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 2. Complexity Analysis
|
||||
Chapter 2. Complexity analysis
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -661,7 +667,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.1 Algorithm Efficiency Assessment
|
||||
2.1 Algorithm efficiency assessment
|
||||
</span>
|
||||
|
||||
|
||||
@ -682,7 +688,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.2 Iteration and Recursion
|
||||
2.2 Iteration and recursion
|
||||
</span>
|
||||
|
||||
|
||||
@ -703,7 +709,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.3 Time Complexity
|
||||
2.3 Time complexity
|
||||
</span>
|
||||
|
||||
|
||||
@ -733,7 +739,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.4 Space Complexity
|
||||
2.4 Space complexity
|
||||
</span>
|
||||
|
||||
|
||||
@ -744,7 +750,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.4 Space Complexity
|
||||
2.4 Space complexity
|
||||
</span>
|
||||
|
||||
|
||||
@ -768,7 +774,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#241-space-related-to-algorithms" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2.4.1 Space Related to Algorithms
|
||||
2.4.1 Space related to algorithms
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -777,7 +783,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#242-calculation-method" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2.4.2 Calculation Method
|
||||
2.4.2 Calculation method
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -786,17 +792,17 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#243-common-types" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2.4.3 Common Types
|
||||
2.4.3 Common types
|
||||
</span>
|
||||
</a>
|
||||
|
||||
<nav class="md-nav" aria-label="2.4.3 Common Types">
|
||||
<nav class="md-nav" aria-label="2.4.3 Common types">
|
||||
<ul class="md-nav__list">
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-constant-order-o1" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Constant Order
|
||||
1. Constant order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -805,7 +811,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#2-linear-order-on" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2. Linear Order
|
||||
2. Linear order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -814,7 +820,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#3-quadratic-order-on2" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3. Quadratic Order
|
||||
3. Quadratic order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -823,7 +829,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#4-exponential-order-o2n" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
4. Exponential Order
|
||||
4. Exponential order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -832,7 +838,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#5-logarithmic-order-olog-n" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
5. Logarithmic Order
|
||||
5. Logarithmic order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -846,7 +852,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#244-balancing-time-and-space" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2.4.4 Balancing Time and Space
|
||||
2.4.4 Balancing time and space
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -932,7 +938,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M11 13.5v8H3v-8h8m-2 2H5v4h4v-4M12 2l5.5 9h-11L12 2m0 3.86L10.08 9h3.84L12 5.86M17.5 13c2.5 0 4.5 2 4.5 4.5S20 22 17.5 22 13 20 13 17.5s2-4.5 4.5-4.5m0 2a2.5 2.5 0 0 0-2.5 2.5 2.5 2.5 0 0 0 2.5 2.5 2.5 2.5 0 0 0 2.5-2.5 2.5 2.5 0 0 0-2.5-2.5Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 3. Data Structures
|
||||
Chapter 3. Data structures
|
||||
</span>
|
||||
|
||||
|
||||
@ -948,7 +954,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_4_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_4">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 3. Data Structures
|
||||
Chapter 3. Data structures
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -965,7 +971,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.1 Classification of Data Structures
|
||||
3.1 Classification of data structures
|
||||
</span>
|
||||
|
||||
|
||||
@ -986,7 +992,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.2 Fundamental Data Types
|
||||
3.2 Fundamental data types
|
||||
</span>
|
||||
|
||||
|
||||
@ -1007,7 +1013,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.3 Number Encoding *
|
||||
3.3 Number encoding *
|
||||
</span>
|
||||
|
||||
|
||||
@ -1028,7 +1034,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.4 Character Encoding *
|
||||
3.4 Character encoding *
|
||||
</span>
|
||||
|
||||
|
||||
@ -1109,7 +1115,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 5v14h17V5H3m4 2v2H5V7h2m-2 6v-2h2v2H5m0 2h2v2H5v-2m13 2H9v-2h9v2m0-4H9v-2h9v2m0-4H9V7h9v2Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Array and linked list
|
||||
</span>
|
||||
|
||||
|
||||
@ -1125,7 +1131,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_5">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Array and linked list
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -1163,7 +1169,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
4.2 Linked List
|
||||
4.2 Linked list
|
||||
</span>
|
||||
|
||||
|
||||
@ -1205,7 +1211,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
4.4 Memory and Cache
|
||||
4.4 Memory and cache
|
||||
</span>
|
||||
|
||||
|
||||
@ -1284,7 +1290,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17.36 20.2v-5.38h1.79V22H3v-7.18h1.8v5.38h12.56M6.77 14.32l.37-1.76 8.79 1.85-.37 1.76-8.79-1.85m1.16-4.21.76-1.61 8.14 3.78-.76 1.62-8.14-3.79m2.26-3.99 1.15-1.38 6.9 5.76-1.15 1.37-6.9-5.75m4.45-4.25L20 9.08l-1.44 1.07-5.36-7.21 1.44-1.07M6.59 18.41v-1.8h8.98v1.8H6.59Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stack and queue
|
||||
</span>
|
||||
|
||||
|
||||
@ -1300,7 +1306,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_6_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_6">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stack and queue
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -1359,7 +1365,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
5.3 Double-ended Queue
|
||||
5.3 Double-ended queue
|
||||
</span>
|
||||
|
||||
|
||||
@ -1438,7 +1444,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19.3 17.89c1.32-2.1.7-4.89-1.41-6.21a4.52 4.52 0 0 0-6.21 1.41C10.36 15.2 11 18 13.09 19.3c1.47.92 3.33.92 4.8 0L21 22.39 22.39 21l-3.09-3.11m-2-.62c-.98.98-2.56.97-3.54 0-.97-.98-.97-2.56.01-3.54.97-.97 2.55-.97 3.53 0 .96.99.95 2.57-.03 3.54h.03M19 4H5a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h5.81a6.3 6.3 0 0 1-1.31-2H5v-4h4.18c.16-.71.43-1.39.82-2H5V8h6v2.81a6.3 6.3 0 0 1 2-1.31V8h6v2a6.499 6.499 0 0 1 2 2V6a2 2 0 0 0-2-2Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 6. Hash Table
|
||||
Chapter 6. Hash table
|
||||
</span>
|
||||
|
||||
|
||||
@ -1454,7 +1460,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_7_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_7">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 6. Hash Table
|
||||
Chapter 6. Hash table
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -1471,7 +1477,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.1 Hash Table
|
||||
6.1 Hash table
|
||||
</span>
|
||||
|
||||
|
||||
@ -1492,7 +1498,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.2 Hash Collision
|
||||
6.2 Hash collision
|
||||
</span>
|
||||
|
||||
|
||||
@ -1513,7 +1519,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.3 Hash Algorithm
|
||||
6.3 Hash algorithm
|
||||
</span>
|
||||
|
||||
|
||||
@ -2088,7 +2094,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#241-space-related-to-algorithms" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2.4.1 Space Related to Algorithms
|
||||
2.4.1 Space related to algorithms
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2097,7 +2103,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#242-calculation-method" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2.4.2 Calculation Method
|
||||
2.4.2 Calculation method
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2106,17 +2112,17 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#243-common-types" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2.4.3 Common Types
|
||||
2.4.3 Common types
|
||||
</span>
|
||||
</a>
|
||||
|
||||
<nav class="md-nav" aria-label="2.4.3 Common Types">
|
||||
<nav class="md-nav" aria-label="2.4.3 Common types">
|
||||
<ul class="md-nav__list">
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="#1-constant-order-o1" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
1. Constant Order
|
||||
1. Constant order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2125,7 +2131,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#2-linear-order-on" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2. Linear Order
|
||||
2. Linear order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2134,7 +2140,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#3-quadratic-order-on2" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3. Quadratic Order
|
||||
3. Quadratic order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2143,7 +2149,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#4-exponential-order-o2n" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
4. Exponential Order
|
||||
4. Exponential order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2152,7 +2158,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#5-logarithmic-order-olog-n" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
5. Logarithmic Order
|
||||
5. Logarithmic order
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2166,7 +2172,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#244-balancing-time-and-space" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
2.4.4 Balancing Time and Space
|
||||
2.4.4 Balancing time and space
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2208,38 +2214,38 @@
|
||||
|
||||
|
||||
<!-- Page content -->
|
||||
<h1 id="24-space-complexity">2.4 Space Complexity<a class="headerlink" href="#24-space-complexity" title="Permanent link">¶</a></h1>
|
||||
<h1 id="24-space-complexity">2.4 Space complexity<a class="headerlink" href="#24-space-complexity" title="Permanent link">¶</a></h1>
|
||||
<p>"Space complexity" is used to measure the growth trend of the memory space occupied by an algorithm as the amount of data increases. This concept is very similar to time complexity, except that "running time" is replaced with "occupied memory space".</p>
|
||||
<h2 id="241-space-related-to-algorithms">2.4.1 Space Related to Algorithms<a class="headerlink" href="#241-space-related-to-algorithms" title="Permanent link">¶</a></h2>
|
||||
<h2 id="241-space-related-to-algorithms">2.4.1 Space related to algorithms<a class="headerlink" href="#241-space-related-to-algorithms" title="Permanent link">¶</a></h2>
|
||||
<p>The memory space used by an algorithm during its execution mainly includes the following types.</p>
|
||||
<ul>
|
||||
<li><strong>Input Space</strong>: Used to store the input data of the algorithm.</li>
|
||||
<li><strong>Temporary Space</strong>: Used to store variables, objects, function contexts, and other data during the algorithm's execution.</li>
|
||||
<li><strong>Output Space</strong>: Used to store the output data of the algorithm.</li>
|
||||
<li><strong>Input space</strong>: Used to store the input data of the algorithm.</li>
|
||||
<li><strong>Temporary space</strong>: Used to store variables, objects, function contexts, and other data during the algorithm's execution.</li>
|
||||
<li><strong>Output space</strong>: Used to store the output data of the algorithm.</li>
|
||||
</ul>
|
||||
<p>Generally, the scope of space complexity statistics includes both "Temporary Space" and "Output Space".</p>
|
||||
<p>Temporary space can be further divided into three parts.</p>
|
||||
<ul>
|
||||
<li><strong>Temporary Data</strong>: Used to save various constants, variables, objects, etc., during the algorithm's execution.</li>
|
||||
<li><strong>Stack Frame Space</strong>: Used to save the context data of the called function. The system creates a stack frame at the top of the stack each time a function is called, and the stack frame space is released after the function returns.</li>
|
||||
<li><strong>Instruction Space</strong>: Used to store compiled program instructions, which are usually negligible in actual statistics.</li>
|
||||
<li><strong>Temporary data</strong>: Used to save various constants, variables, objects, etc., during the algorithm's execution.</li>
|
||||
<li><strong>Stack frame space</strong>: Used to save the context data of the called function. The system creates a stack frame at the top of the stack each time a function is called, and the stack frame space is released after the function returns.</li>
|
||||
<li><strong>Instruction space</strong>: Used to store compiled program instructions, which are usually negligible in actual statistics.</li>
|
||||
</ul>
|
||||
<p>When analyzing the space complexity of a program, <strong>we typically count the Temporary Data, Stack Frame Space, and Output Data</strong>, as shown in the Figure 2-15 .</p>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Space Types Used in Algorithms" class="animation-figure" src="../space_complexity.assets/space_types.png" /></a></p>
|
||||
<p align="center"> Figure 2-15 Space Types Used in Algorithms </p>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Space types used in algorithms" class="animation-figure" src="../space_complexity.assets/space_types.png" /></a></p>
|
||||
<p align="center"> Figure 2-15 Space types used in algorithms </p>
|
||||
|
||||
<p>The relevant code is as follows:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:13"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><input id="__tabbed_1_12" name="__tabbed_1" type="radio" /><input id="__tabbed_1_13" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="k">class</span> <span class="nc">Node</span><span class="p">:</span>
|
||||
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">"""Classes"""</span><span class="s2">"</span>
|
||||
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="w"> </span><span class="sd">"""Classes"""</span>
|
||||
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a> <span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">x</span> <span class="c1"># node value</span>
|
||||
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a> <span class="bp">self</span><span class="o">.</span><span class="n">next</span><span class="p">:</span> <span class="n">Node</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span> <span class="c1"># reference to the next node</span>
|
||||
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a>
|
||||
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="k">def</span> <span class="nf">function</span><span class="p">()</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
|
||||
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="sd">""""Functions"""</span><span class="s2">""</span>
|
||||
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="sd">"""Functions"""</span>
|
||||
<a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a> <span class="c1"># Perform certain operations...</span>
|
||||
<a id="__codelineno-0-10" name="__codelineno-0-10" href="#__codelineno-0-10"></a> <span class="k">return</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-0-11" name="__codelineno-0-11" href="#__codelineno-0-11"></a>
|
||||
@ -2458,7 +2464,7 @@
|
||||
<a id="__codelineno-9-7" name="__codelineno-9-7" href="#__codelineno-9-7"></a><span class="w"> </span><span class="n">next</span>: <span class="nb">Option</span><span class="o"><</span><span class="n">Rc</span><span class="o"><</span><span class="n">RefCell</span><span class="o"><</span><span class="n">Node</span><span class="o">>>></span><span class="p">,</span>
|
||||
<a id="__codelineno-9-8" name="__codelineno-9-8" href="#__codelineno-9-8"></a><span class="p">}</span>
|
||||
<a id="__codelineno-9-9" name="__codelineno-9-9" href="#__codelineno-9-9"></a>
|
||||
<a id="__codelineno-9-10" name="__codelineno-9-10" href="#__codelineno-9-10"></a><span class="cm">/* Creating a Node structure */</span>
|
||||
<a id="__codelineno-9-10" name="__codelineno-9-10" href="#__codelineno-9-10"></a><span class="cm">/* Constructor */</span>
|
||||
<a id="__codelineno-9-11" name="__codelineno-9-11" href="#__codelineno-9-11"></a><span class="k">impl</span><span class="w"> </span><span class="n">Node</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-9-12" name="__codelineno-9-12" href="#__codelineno-9-12"></a><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">(</span><span class="n">val</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-> <span class="nc">Self</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-9-13" name="__codelineno-9-13" href="#__codelineno-9-13"></a><span class="w"> </span><span class="bp">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">val</span>: <span class="nc">val</span><span class="p">,</span><span class="w"> </span><span class="n">next</span>: <span class="nb">None</span> <span class="p">}</span>
|
||||
@ -2505,7 +2511,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h2 id="242-calculation-method">2.4.2 Calculation Method<a class="headerlink" href="#242-calculation-method" title="Permanent link">¶</a></h2>
|
||||
<h2 id="242-calculation-method">2.4.2 Calculation method<a class="headerlink" href="#242-calculation-method" title="Permanent link">¶</a></h2>
|
||||
<p>The method for calculating space complexity is roughly similar to that of time complexity, with the only change being the shift of the statistical object from "number of operations" to "size of used space".</p>
|
||||
<p>However, unlike time complexity, <strong>we usually only focus on the worst-case space complexity</strong>. This is because memory space is a hard requirement, and we must ensure that there is enough memory space reserved under all input data.</p>
|
||||
<p>Consider the following code, the term "worst-case" in worst-case space complexity has two meanings.</p>
|
||||
@ -2605,10 +2611,10 @@
|
||||
</div>
|
||||
<div class="tabbed-block">
|
||||
<div class="highlight"><pre><span></span><code><a id="__codelineno-22-1" name="__codelineno-22-1" href="#__codelineno-22-1"></a><span class="k">fn</span> <span class="nf">algorithm</span><span class="p">(</span><span class="n">n</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// O(1)</span>
|
||||
<a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">10000</span><span class="p">];</span><span class="w"> </span><span class="c1">// O(1)</span>
|
||||
<a id="__codelineno-22-2" name="__codelineno-22-2" href="#__codelineno-22-2"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// O(1)</span>
|
||||
<a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">10000</span><span class="p">];</span><span class="w"> </span><span class="c1">// O(1)</span>
|
||||
<a id="__codelineno-22-4" name="__codelineno-22-4" href="#__codelineno-22-4"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="p">{</span>
|
||||
<a id="__codelineno-22-5" name="__codelineno-22-5" href="#__codelineno-22-5"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">];</span><span class="w"> </span><span class="c1">// O(n)</span>
|
||||
<a id="__codelineno-22-5" name="__codelineno-22-5" href="#__codelineno-22-5"></a><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">];</span><span class="w"> </span><span class="c1">// O(n)</span>
|
||||
<a id="__codelineno-22-6" name="__codelineno-22-6" href="#__codelineno-22-6"></a><span class="w"> </span><span class="p">}</span>
|
||||
<a id="__codelineno-22-7" name="__codelineno-22-7" href="#__codelineno-22-7"></a><span class="p">}</span>
|
||||
</code></pre></div>
|
||||
@ -2641,12 +2647,12 @@
|
||||
<a id="__codelineno-26-3" name="__codelineno-26-3" href="#__codelineno-26-3"></a> <span class="k">return</span> <span class="mi">0</span>
|
||||
<a id="__codelineno-26-4" name="__codelineno-26-4" href="#__codelineno-26-4"></a>
|
||||
<a id="__codelineno-26-5" name="__codelineno-26-5" href="#__codelineno-26-5"></a><span class="k">def</span> <span class="nf">loop</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="sd">"""Loop O(1)"""</span><span class="s2">""</span>
|
||||
<a id="__codelineno-26-6" name="__codelineno-26-6" href="#__codelineno-26-6"></a><span class="w"> </span><span class="sd">"""Loop O(1)"""</span>
|
||||
<a id="__codelineno-26-7" name="__codelineno-26-7" href="#__codelineno-26-7"></a> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
||||
<a id="__codelineno-26-8" name="__codelineno-26-8" href="#__codelineno-26-8"></a> <span class="n">function</span><span class="p">()</span>
|
||||
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a>
|
||||
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a><span class="k">def</span> <span class="nf">recur</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
|
||||
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="w"> </span><span class="sd">"""Recursion O(n)"""</span><span class="s2">""</span>
|
||||
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="w"> </span><span class="sd">"""Recursion O(n)"""</span>
|
||||
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
|
||||
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a> <span class="k">return</span>
|
||||
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a> <span class="k">return</span> <span class="n">recur</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
|
||||
@ -2858,7 +2864,7 @@
|
||||
<li>The <code>loop()</code> function calls <code>function()</code> <span class="arithmatex">\(n\)</span> times in a loop, where each iteration's <code>function()</code> returns and releases its stack frame space, so the space complexity remains <span class="arithmatex">\(O(1)\)</span>.</li>
|
||||
<li>The recursive function <code>recur()</code> will have <span class="arithmatex">\(n\)</span> instances of unreturned <code>recur()</code> existing simultaneously during its execution, thus occupying <span class="arithmatex">\(O(n)\)</span> stack frame space.</li>
|
||||
</ul>
|
||||
<h2 id="243-common-types">2.4.3 Common Types<a class="headerlink" href="#243-common-types" title="Permanent link">¶</a></h2>
|
||||
<h2 id="243-common-types">2.4.3 Common types<a class="headerlink" href="#243-common-types" title="Permanent link">¶</a></h2>
|
||||
<p>Let the size of the input data be <span class="arithmatex">\(n\)</span>, the following chart displays common types of space complexities (arranged from low to high).</p>
|
||||
<div class="arithmatex">\[
|
||||
\begin{aligned}
|
||||
@ -2866,10 +2872,10 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
||||
\text{Constant Order} < \text{Logarithmic Order} < \text{Linear Order} < \text{Quadratic Order} < \text{Exponential Order}
|
||||
\end{aligned}
|
||||
\]</div>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_common_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Common Types of Space Complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_common_types.png" /></a></p>
|
||||
<p align="center"> Figure 2-16 Common Types of Space Complexity </p>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_common_types.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Common types of space complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_common_types.png" /></a></p>
|
||||
<p align="center"> Figure 2-16 Common types of space complexity </p>
|
||||
|
||||
<h3 id="1-constant-order-o1">1. Constant Order <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#1-constant-order-o1" title="Permanent link">¶</a></h3>
|
||||
<h3 id="1-constant-order-o1">1. Constant order <span class="arithmatex">\(O(1)\)</span><a class="headerlink" href="#1-constant-order-o1" title="Permanent link">¶</a></h3>
|
||||
<p>Constant order is common in constants, variables, objects that are independent of the size of input data <span class="arithmatex">\(n\)</span>.</p>
|
||||
<p>Note that memory occupied by initializing variables or calling functions in a loop, which is released upon entering the next cycle, does not accumulate over space, thus the space complexity remains <span class="arithmatex">\(O(1)\)</span>:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="4:14"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" name="__tabbed_4" type="radio" /><input id="__tabbed_4_10" name="__tabbed_4" type="radio" /><input id="__tabbed_4_11" name="__tabbed_4" type="radio" /><input id="__tabbed_4_12" name="__tabbed_4" type="radio" /><input id="__tabbed_4_13" name="__tabbed_4" type="radio" /><input id="__tabbed_4_14" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">Python</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Java</label><label for="__tabbed_4_4">C#</label><label for="__tabbed_4_5">Go</label><label for="__tabbed_4_6">Swift</label><label for="__tabbed_4_7">JS</label><label for="__tabbed_4_8">TS</label><label for="__tabbed_4_9">Dart</label><label for="__tabbed_4_10">Rust</label><label for="__tabbed_4_11">C</label><label for="__tabbed_4_12">Kotlin</label><label for="__tabbed_4_13">Ruby</label><label for="__tabbed_4_14">Zig</label></div>
|
||||
@ -3237,7 +3243,7 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
||||
<p><div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20function%28%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%87%BD%E6%95%B0%22%22%22%0A%20%20%20%20%23%20%E6%89%A7%E8%A1%8C%E6%9F%90%E4%BA%9B%E6%93%8D%E4%BD%9C%0A%20%20%20%20return%200%0A%0Adef%20constant%28n%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E5%B8%B8%E6%95%B0%E9%98%B6%22%22%22%0A%20%20%20%20%23%20%E5%B8%B8%E9%87%8F%E3%80%81%E5%8F%98%E9%87%8F%E3%80%81%E5%AF%B9%E8%B1%A1%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20a%20%3D%200%0A%20%20%20%20nums%20%3D%20%5B0%5D%20*%2010%0A%20%20%20%20node%20%3D%20ListNode%280%29%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E5%8F%98%E9%87%8F%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20c%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E5%87%BD%E6%95%B0%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20function%28%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E5%B8%B8%E6%95%B0%E9%98%B6%0A%20%20%20%20constant%28n%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=6&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20function%28%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%87%BD%E6%95%B0%22%22%22%0A%20%20%20%20%23%20%E6%89%A7%E8%A1%8C%E6%9F%90%E4%BA%9B%E6%93%8D%E4%BD%9C%0A%20%20%20%20return%200%0A%0Adef%20constant%28n%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E5%B8%B8%E6%95%B0%E9%98%B6%22%22%22%0A%20%20%20%20%23%20%E5%B8%B8%E9%87%8F%E3%80%81%E5%8F%98%E9%87%8F%E3%80%81%E5%AF%B9%E8%B1%A1%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20a%20%3D%200%0A%20%20%20%20nums%20%3D%20%5B0%5D%20*%2010%0A%20%20%20%20node%20%3D%20ListNode%280%29%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E5%8F%98%E9%87%8F%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20c%20%3D%200%0A%20%20%20%20%23%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E5%87%BD%E6%95%B0%E5%8D%A0%E7%94%A8%20O%281%29%20%E7%A9%BA%E9%97%B4%0A%20%20%20%20for%20_%20in%20range%28n%29%3A%0A%20%20%20%20%20%20%20%20function%28%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E5%B8%B8%E6%95%B0%E9%98%B6%0A%20%20%20%20constant%28n%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=6&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<h3 id="2-linear-order-on">2. Linear Order <span class="arithmatex">\(O(n)\)</span><a class="headerlink" href="#2-linear-order-on" title="Permanent link">¶</a></h3>
|
||||
<h3 id="2-linear-order-on">2. Linear order <span class="arithmatex">\(O(n)\)</span><a class="headerlink" href="#2-linear-order-on" title="Permanent link">¶</a></h3>
|
||||
<p>Linear order is common in arrays, linked lists, stacks, queues, etc., where the number of elements is proportional to <span class="arithmatex">\(n\)</span>:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="5:14"><input checked="checked" id="__tabbed_5_1" name="__tabbed_5" type="radio" /><input id="__tabbed_5_2" name="__tabbed_5" type="radio" /><input id="__tabbed_5_3" name="__tabbed_5" type="radio" /><input id="__tabbed_5_4" name="__tabbed_5" type="radio" /><input id="__tabbed_5_5" name="__tabbed_5" type="radio" /><input id="__tabbed_5_6" name="__tabbed_5" type="radio" /><input id="__tabbed_5_7" name="__tabbed_5" type="radio" /><input id="__tabbed_5_8" name="__tabbed_5" type="radio" /><input id="__tabbed_5_9" name="__tabbed_5" type="radio" /><input id="__tabbed_5_10" name="__tabbed_5" type="radio" /><input id="__tabbed_5_11" name="__tabbed_5" type="radio" /><input id="__tabbed_5_12" name="__tabbed_5" type="radio" /><input id="__tabbed_5_13" name="__tabbed_5" type="radio" /><input id="__tabbed_5_14" name="__tabbed_5" type="radio" /><div class="tabbed-labels"><label for="__tabbed_5_1">Python</label><label for="__tabbed_5_2">C++</label><label for="__tabbed_5_3">Java</label><label for="__tabbed_5_4">C#</label><label for="__tabbed_5_5">Go</label><label for="__tabbed_5_6">Swift</label><label for="__tabbed_5_7">JS</label><label for="__tabbed_5_8">TS</label><label for="__tabbed_5_9">Dart</label><label for="__tabbed_5_10">Rust</label><label for="__tabbed_5_11">C</label><label for="__tabbed_5_12">Kotlin</label><label for="__tabbed_5_13">Ruby</label><label for="__tabbed_5_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@ -3662,10 +3668,10 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
||||
<p><div style="height: 441px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20linear_recur%28n%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E7%BA%BF%E6%80%A7%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20print%28%22%E9%80%92%E5%BD%92%20n%20%3D%22,%20n%29%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20linear_recur%28n%20-%201%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E7%BA%BF%E6%80%A7%E9%98%B6%0A%20%20%20%20linear_recur%28n%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=25&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20linear_recur%28n%3A%20int%29%3A%0A%20%20%20%20%22%22%22%E7%BA%BF%E6%80%A7%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20print%28%22%E9%80%92%E5%BD%92%20n%20%3D%22,%20n%29%0A%20%20%20%20if%20n%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20linear_recur%28n%20-%201%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E7%BA%BF%E6%80%A7%E9%98%B6%0A%20%20%20%20linear_recur%28n%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=25&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_recursive_linear.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive Function Generating Linear Order Space Complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_recursive_linear.png" /></a></p>
|
||||
<p align="center"> Figure 2-17 Recursive Function Generating Linear Order Space Complexity </p>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_recursive_linear.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive function generating linear order space complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_recursive_linear.png" /></a></p>
|
||||
<p align="center"> Figure 2-17 Recursive function generating linear order space complexity </p>
|
||||
|
||||
<h3 id="3-quadratic-order-on2">3. Quadratic Order <span class="arithmatex">\(O(n^2)\)</span><a class="headerlink" href="#3-quadratic-order-on2" title="Permanent link">¶</a></h3>
|
||||
<h3 id="3-quadratic-order-on2">3. Quadratic order <span class="arithmatex">\(O(n^2)\)</span><a class="headerlink" href="#3-quadratic-order-on2" title="Permanent link">¶</a></h3>
|
||||
<p>Quadratic order is common in matrices and graphs, where the number of elements is quadratic to <span class="arithmatex">\(n\)</span>:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="7:14"><input checked="checked" id="__tabbed_7_1" name="__tabbed_7" type="radio" /><input id="__tabbed_7_2" name="__tabbed_7" type="radio" /><input id="__tabbed_7_3" name="__tabbed_7" type="radio" /><input id="__tabbed_7_4" name="__tabbed_7" type="radio" /><input id="__tabbed_7_5" name="__tabbed_7" type="radio" /><input id="__tabbed_7_6" name="__tabbed_7" type="radio" /><input id="__tabbed_7_7" name="__tabbed_7" type="radio" /><input id="__tabbed_7_8" name="__tabbed_7" type="radio" /><input id="__tabbed_7_9" name="__tabbed_7" type="radio" /><input id="__tabbed_7_10" name="__tabbed_7" type="radio" /><input id="__tabbed_7_11" name="__tabbed_7" type="radio" /><input id="__tabbed_7_12" name="__tabbed_7" type="radio" /><input id="__tabbed_7_13" name="__tabbed_7" type="radio" /><input id="__tabbed_7_14" name="__tabbed_7" type="radio" /><div class="tabbed-labels"><label for="__tabbed_7_1">Python</label><label for="__tabbed_7_2">C++</label><label for="__tabbed_7_3">Java</label><label for="__tabbed_7_4">C#</label><label for="__tabbed_7_5">Go</label><label for="__tabbed_7_6">Swift</label><label for="__tabbed_7_7">JS</label><label for="__tabbed_7_8">TS</label><label for="__tabbed_7_9">Dart</label><label for="__tabbed_7_10">Rust</label><label for="__tabbed_7_11">C</label><label for="__tabbed_7_12">Kotlin</label><label for="__tabbed_7_13">Ruby</label><label for="__tabbed_7_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@ -4057,10 +4063,10 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
||||
<p><div style="height: 459px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=def%20quadratic_recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3C%3D%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20%23%20%E6%95%B0%E7%BB%84%20nums%20%E9%95%BF%E5%BA%A6%E4%B8%BA%20n,%20n-1,%20...,%202,%201%0A%20%20%20%20nums%20%3D%20%5B0%5D%20*%20n%0A%20%20%20%20return%20quadratic_recur%28n%20-%201%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E5%B9%B3%E6%96%B9%E9%98%B6%0A%20%20%20%20quadratic_recur%28n%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=28&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20quadratic_recur%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%B9%B3%E6%96%B9%E9%98%B6%EF%BC%88%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3C%3D%200%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20%23%20%E6%95%B0%E7%BB%84%20nums%20%E9%95%BF%E5%BA%A6%E4%B8%BA%20n,%20n-1,%20...,%202,%201%0A%20%20%20%20nums%20%3D%20%5B0%5D%20*%20n%0A%20%20%20%20return%20quadratic_recur%28n%20-%201%29%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E5%B9%B3%E6%96%B9%E9%98%B6%0A%20%20%20%20quadratic_recur%28n%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=28&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_recursive_quadratic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive Function Generating Quadratic Order Space Complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_recursive_quadratic.png" /></a></p>
|
||||
<p align="center"> Figure 2-18 Recursive Function Generating Quadratic Order Space Complexity </p>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_recursive_quadratic.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Recursive function generating quadratic order space complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_recursive_quadratic.png" /></a></p>
|
||||
<p align="center"> Figure 2-18 Recursive function generating quadratic order space complexity </p>
|
||||
|
||||
<h3 id="4-exponential-order-o2n">4. Exponential Order <span class="arithmatex">\(O(2^n)\)</span><a class="headerlink" href="#4-exponential-order-o2n" title="Permanent link">¶</a></h3>
|
||||
<h3 id="4-exponential-order-o2n">4. Exponential order <span class="arithmatex">\(O(2^n)\)</span><a class="headerlink" href="#4-exponential-order-o2n" title="Permanent link">¶</a></h3>
|
||||
<p>Exponential order is common in binary trees. Observe the below image, a "full binary tree" with <span class="arithmatex">\(n\)</span> levels has <span class="arithmatex">\(2^n - 1\)</span> nodes, occupying <span class="arithmatex">\(O(2^n)\)</span> space:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="9:14"><input checked="checked" id="__tabbed_9_1" name="__tabbed_9" type="radio" /><input id="__tabbed_9_2" name="__tabbed_9" type="radio" /><input id="__tabbed_9_3" name="__tabbed_9" type="radio" /><input id="__tabbed_9_4" name="__tabbed_9" type="radio" /><input id="__tabbed_9_5" name="__tabbed_9" type="radio" /><input id="__tabbed_9_6" name="__tabbed_9" type="radio" /><input id="__tabbed_9_7" name="__tabbed_9" type="radio" /><input id="__tabbed_9_8" name="__tabbed_9" type="radio" /><input id="__tabbed_9_9" name="__tabbed_9" type="radio" /><input id="__tabbed_9_10" name="__tabbed_9" type="radio" /><input id="__tabbed_9_11" name="__tabbed_9" type="radio" /><input id="__tabbed_9_12" name="__tabbed_9" type="radio" /><input id="__tabbed_9_13" name="__tabbed_9" type="radio" /><input id="__tabbed_9_14" name="__tabbed_9" type="radio" /><div class="tabbed-labels"><label for="__tabbed_9_1">Python</label><label for="__tabbed_9_2">C++</label><label for="__tabbed_9_3">Java</label><label for="__tabbed_9_4">C#</label><label for="__tabbed_9_5">Go</label><label for="__tabbed_9_6">Swift</label><label for="__tabbed_9_7">JS</label><label for="__tabbed_9_8">TS</label><label for="__tabbed_9_9">Dart</label><label for="__tabbed_9_10">Rust</label><label for="__tabbed_9_11">C</label><label for="__tabbed_9_12">Kotlin</label><label for="__tabbed_9_13">Ruby</label><label for="__tabbed_9_14">Zig</label></div>
|
||||
<div class="tabbed-content">
|
||||
@ -4238,13 +4244,13 @@ O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
||||
<p><div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20build_tree%28n%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E5%BB%BA%E7%AB%8B%E6%BB%A1%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20root%20%3D%20TreeNode%280%29%0A%20%20%20%20root.left%20%3D%20build_tree%28n%20-%201%29%0A%20%20%20%20root.right%20%3D%20build_tree%28n%20-%201%29%0A%20%20%20%20return%20root%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E6%8C%87%E6%95%B0%E9%98%B6%0A%20%20%20%20root%20%3D%20build_tree%28n%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=507&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%20%3D%200%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20build_tree%28n%3A%20int%29%20-%3E%20TreeNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E6%8C%87%E6%95%B0%E9%98%B6%EF%BC%88%E5%BB%BA%E7%AB%8B%E6%BB%A1%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20root%20%3D%20TreeNode%280%29%0A%20%20%20%20root.left%20%3D%20build_tree%28n%20-%201%29%0A%20%20%20%20root.right%20%3D%20build_tree%28n%20-%201%29%0A%20%20%20%20return%20root%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%205%0A%20%20%20%20print%28%22%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%A4%A7%E5%B0%8F%20n%20%3D%22,%20n%29%0A%0A%20%20%20%20%23%20%E6%8C%87%E6%95%B0%E9%98%B6%0A%20%20%20%20root%20%3D%20build_tree%28n%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=507&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">Full Screen ></a></div></p>
|
||||
</details>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_exponential.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Full Binary Tree Generating Exponential Order Space Complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_exponential.png" /></a></p>
|
||||
<p align="center"> Figure 2-19 Full Binary Tree Generating Exponential Order Space Complexity </p>
|
||||
<p><a class="glightbox" href="../space_complexity.assets/space_complexity_exponential.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Full binary tree generating exponential order space complexity" class="animation-figure" src="../space_complexity.assets/space_complexity_exponential.png" /></a></p>
|
||||
<p align="center"> Figure 2-19 Full binary tree generating exponential order space complexity </p>
|
||||
|
||||
<h3 id="5-logarithmic-order-olog-n">5. Logarithmic Order <span class="arithmatex">\(O(\log n)\)</span><a class="headerlink" href="#5-logarithmic-order-olog-n" title="Permanent link">¶</a></h3>
|
||||
<h3 id="5-logarithmic-order-olog-n">5. Logarithmic order <span class="arithmatex">\(O(\log n)\)</span><a class="headerlink" href="#5-logarithmic-order-olog-n" title="Permanent link">¶</a></h3>
|
||||
<p>Logarithmic order is common in divide-and-conquer algorithms. For example, in merge sort, an array of length <span class="arithmatex">\(n\)</span> is recursively divided in half each round, forming a recursion tree of height <span class="arithmatex">\(\log n\)</span>, using <span class="arithmatex">\(O(\log n)\)</span> stack frame space.</p>
|
||||
<p>Another example is converting a number to a string. Given a positive integer <span class="arithmatex">\(n\)</span>, its number of digits is <span class="arithmatex">\(\log_{10} n + 1\)</span>, corresponding to the length of the string, thus the space complexity is <span class="arithmatex">\(O(\log_{10} n + 1) = O(\log n)\)</span>.</p>
|
||||
<h2 id="244-balancing-time-and-space">2.4.4 Balancing Time and Space<a class="headerlink" href="#244-balancing-time-and-space" title="Permanent link">¶</a></h2>
|
||||
<h2 id="244-balancing-time-and-space">2.4.4 Balancing time and space<a class="headerlink" href="#244-balancing-time-and-space" title="Permanent link">¶</a></h2>
|
||||
<p>Ideally, we aim for both time complexity and space complexity to be optimal. However, in practice, optimizing both simultaneously is often difficult.</p>
|
||||
<p><strong>Lowering time complexity usually comes at the cost of increased space complexity, and vice versa</strong>. The approach of sacrificing memory space to improve algorithm speed is known as "space-time tradeoff"; the reverse is known as "time-space tradeoff".</p>
|
||||
<p>The choice depends on which aspect we value more. In most cases, time is more precious than space, so "space-time tradeoff" is often the more common strategy. Of course, controlling space complexity is also very important when dealing with large volumes of data.</p>
|
||||
@ -4270,7 +4276,7 @@ aria-label="Footer"
|
||||
<a
|
||||
href="../time_complexity/"
|
||||
class="md-footer__link md-footer__link--prev"
|
||||
aria-label="Previous: 2.3 Time Complexity"
|
||||
aria-label="Previous: 2.3 Time complexity"
|
||||
rel="prev"
|
||||
>
|
||||
<div class="md-footer__button md-icon">
|
||||
@ -4282,7 +4288,7 @@ aria-label="Footer"
|
||||
Previous
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
2.3 Time Complexity
|
||||
2.3 Time complexity
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
@ -4395,7 +4401,7 @@ aria-label="Footer"
|
||||
<nav class="md-footer__inner md-grid" aria-label="Footer" >
|
||||
|
||||
|
||||
<a href="../time_complexity/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.3 Time Complexity">
|
||||
<a href="../time_complexity/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 2.3 Time complexity">
|
||||
<div class="md-footer__button md-icon">
|
||||
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
|
||||
@ -4405,7 +4411,7 @@ aria-label="Footer"
|
||||
Previous
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
2.3 Time Complexity
|
||||
2.3 Time complexity
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
|
Reference in New Issue
Block a user