mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 11:15:19 +08:00
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
93 lines
1.9 KiB
Ruby
93 lines
1.9 KiB
Ruby
=begin
|
|
File: space_complexity.rb
|
|
Created Time: 2024-03-30
|
|
Author: Xuan Khoa Tu Nguyen ([email protected])
|
|
=end
|
|
|
|
require_relative '../utils/list_node'
|
|
require_relative '../utils/tree_node'
|
|
require_relative '../utils/print_util'
|
|
|
|
### 関数 ###
|
|
def function
|
|
# 何らかの処理を実行する
|
|
0
|
|
end
|
|
|
|
### 定数オーダー ###
|
|
def constant(n)
|
|
# 定数、変数、オブジェクトは O(1) の領域を使用する
|
|
a = 0
|
|
nums = [0] * 10000
|
|
node = ListNode.new
|
|
|
|
# ループ内の変数は O(1) の領域を使用する
|
|
(0...n).each { c = 0 }
|
|
# ループ内の関数は O(1) の領域を使用する
|
|
(0...n).each { function }
|
|
end
|
|
|
|
### 線形オーダー ###
|
|
def linear(n)
|
|
# 長さ n のリストは O(n) の領域を使用する
|
|
nums = Array.new(n, 0)
|
|
|
|
# 長さ n のハッシュテーブルは O(n) の領域を使用する
|
|
hmap = {}
|
|
for i in 0...n
|
|
hmap[i] = i.to_s
|
|
end
|
|
end
|
|
|
|
### 線形オーダー(再帰による実装)###
|
|
def linear_recur(n)
|
|
puts "再帰 n = #{n}"
|
|
return if n == 1
|
|
linear_recur(n - 1)
|
|
end
|
|
|
|
### 二次オーダー ###
|
|
def quadratic(n)
|
|
# 二次元リストは O(n^2) の領域を使用する
|
|
Array.new(n) { Array.new(n, 0) }
|
|
end
|
|
|
|
### 二次オーダー(再帰による実装)###
|
|
def quadratic_recur(n)
|
|
return 0 unless n > 0
|
|
|
|
# 配列 nums の長さは n, n-1, ..., 2, 1
|
|
nums = Array.new(n, 0)
|
|
quadratic_recur(n - 1)
|
|
end
|
|
|
|
### 指数オーダー(パーフェクト二分木の構築)###
|
|
def build_tree(n)
|
|
return if n == 0
|
|
|
|
TreeNode.new.tap do |root|
|
|
root.left = build_tree(n - 1)
|
|
root.right = build_tree(n - 1)
|
|
end
|
|
end
|
|
|
|
### 実行用コード ###
|
|
if __FILE__ == $0
|
|
n = 5
|
|
|
|
# 定数オーダー
|
|
constant(n)
|
|
|
|
# 線形オーダー
|
|
linear(n)
|
|
linear_recur(n)
|
|
|
|
# 二次オーダー
|
|
quadratic(n)
|
|
quadratic_recur(n)
|
|
|
|
# 指数オーダー
|
|
root = build_tree(n)
|
|
print_tree(root)
|
|
end
|