2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-12-13 23:24:12 +08:00
|
|
|
File: space_complexity.py
|
2022-11-25 02:04:38 +08:00
|
|
|
Created Time: 2022-11-25
|
|
|
|
Author: Krahets (krahets@163.com)
|
2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-11-25 02:04:38 +08:00
|
|
|
|
|
|
|
import sys, os.path as osp
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2022-11-25 02:04:38 +08:00
|
|
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
2023-03-03 03:07:22 +08:00
|
|
|
from modules import *
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def function() -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""函数"""
|
2022-11-25 20:12:20 +08:00
|
|
|
# do something
|
|
|
|
return 0
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-07-24 22:34:05 +08:00
|
|
|
def constant(n: int):
|
2023-04-09 05:05:35 +08:00
|
|
|
"""常数阶"""
|
2022-11-25 20:12:20 +08:00
|
|
|
# 常量、变量、对象占用 O(1) 空间
|
2023-05-22 22:03:57 +08:00
|
|
|
a = 0
|
|
|
|
nums = [0] * 10000
|
2022-11-25 20:12:20 +08:00
|
|
|
node = ListNode(0)
|
|
|
|
# 循环中的变量占用 O(1) 空间
|
|
|
|
for _ in range(n):
|
2023-05-22 22:03:57 +08:00
|
|
|
c = 0
|
2022-11-25 20:12:20 +08:00
|
|
|
# 循环中的函数占用 O(1) 空间
|
|
|
|
for _ in range(n):
|
|
|
|
function()
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-07-24 22:34:05 +08:00
|
|
|
def linear(n: int):
|
2023-04-09 05:05:35 +08:00
|
|
|
"""线性阶"""
|
2022-11-25 20:12:20 +08:00
|
|
|
# 长度为 n 的列表占用 O(n) 空间
|
2023-05-22 22:03:57 +08:00
|
|
|
nums = [0] * n
|
2022-11-25 20:12:20 +08:00
|
|
|
# 长度为 n 的哈希表占用 O(n) 空间
|
2023-08-08 23:16:33 +08:00
|
|
|
hmap = dict[int, str]()
|
2022-11-25 20:12:20 +08:00
|
|
|
for i in range(n):
|
2023-08-08 23:16:33 +08:00
|
|
|
hmap[i] = str(i)
|
2022-11-25 20:12:20 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-07-24 22:34:05 +08:00
|
|
|
def linear_recur(n: int):
|
2023-04-09 05:05:35 +08:00
|
|
|
"""线性阶(递归实现)"""
|
2022-11-27 04:20:30 +08:00
|
|
|
print("递归 n =", n)
|
2023-04-09 05:05:35 +08:00
|
|
|
if n == 1:
|
|
|
|
return
|
2023-02-03 20:03:30 +08:00
|
|
|
linear_recur(n - 1)
|
2022-11-25 20:12:20 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-07-24 22:34:05 +08:00
|
|
|
def quadratic(n: int):
|
2023-04-09 05:05:35 +08:00
|
|
|
"""平方阶"""
|
2022-11-25 20:12:20 +08:00
|
|
|
# 二维列表占用 O(n^2) 空间
|
2023-05-22 22:03:57 +08:00
|
|
|
num_matrix = [[0] * n for _ in range(n)]
|
2022-11-25 20:12:20 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def quadratic_recur(n: int) -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""平方阶(递归实现)"""
|
|
|
|
if n <= 0:
|
|
|
|
return 0
|
2023-02-06 04:11:22 +08:00
|
|
|
# 数组 nums 长度为 n, n-1, ..., 2, 1
|
2023-05-22 22:03:57 +08:00
|
|
|
nums = [0] * n
|
2022-11-25 20:12:20 +08:00
|
|
|
return quadratic_recur(n - 1)
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
def build_tree(n: int) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""指数阶(建立满二叉树)"""
|
|
|
|
if n == 0:
|
|
|
|
return None
|
2022-11-25 20:12:20 +08:00
|
|
|
root = TreeNode(0)
|
|
|
|
root.left = build_tree(n - 1)
|
|
|
|
root.right = build_tree(n - 1)
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
"""Driver Code"""
|
2022-11-25 20:12:20 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
n = 5
|
|
|
|
# 常数阶
|
|
|
|
constant(n)
|
|
|
|
# 线性阶
|
|
|
|
linear(n)
|
2023-02-03 20:03:30 +08:00
|
|
|
linear_recur(n)
|
2022-11-25 20:12:20 +08:00
|
|
|
# 平方阶
|
|
|
|
quadratic(n)
|
|
|
|
quadratic_recur(n)
|
|
|
|
# 指数阶
|
2023-03-23 18:51:56 +08:00
|
|
|
root = build_tree(n)
|
2022-11-25 20:12:20 +08:00
|
|
|
print_tree(root)
|