mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-14 12:40:58 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
=begin
|
||||
File: space_complexity.rb
|
||||
Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
require_relative '../utils/list_node'
|
||||
require_relative '../utils/tree_node'
|
||||
require_relative '../utils/print_util'
|
||||
|
||||
### Function ###
|
||||
def function
|
||||
# Perform some operations
|
||||
0
|
||||
end
|
||||
|
||||
### Constant time ###
|
||||
def constant(n)
|
||||
# Constants, variables, objects occupy O(1) space
|
||||
a = 0
|
||||
nums = [0] * 10000
|
||||
node = ListNode.new
|
||||
|
||||
# Variables in the loop occupy O(1) space
|
||||
(0...n).each { c = 0 }
|
||||
# Functions in the loop occupy O(1) space
|
||||
(0...n).each { function }
|
||||
end
|
||||
|
||||
### Linear time ###
|
||||
def linear(n)
|
||||
# A list of length n occupies O(n) space
|
||||
nums = Array.new(n, 0)
|
||||
|
||||
# A hash table of length n occupies O(n) space
|
||||
hmap = {}
|
||||
for i in 0...n
|
||||
hmap[i] = i.to_s
|
||||
end
|
||||
end
|
||||
|
||||
### Linear space (recursive) ###
|
||||
def linear_recur(n)
|
||||
puts "Recursion n = #{n}"
|
||||
return if n == 1
|
||||
linear_recur(n - 1)
|
||||
end
|
||||
|
||||
### Quadratic time ###
|
||||
def quadratic(n)
|
||||
# 2D list uses O(n^2) space
|
||||
Array.new(n) { Array.new(n, 0) }
|
||||
end
|
||||
|
||||
### Quadratic space (recursive) ###
|
||||
def quadratic_recur(n)
|
||||
return 0 unless n > 0
|
||||
|
||||
# Array nums has length n, n-1, ..., 2, 1
|
||||
nums = Array.new(n, 0)
|
||||
quadratic_recur(n - 1)
|
||||
end
|
||||
|
||||
### Exponential space (build full binary tree) ###
|
||||
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
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 5
|
||||
|
||||
# Constant order
|
||||
constant(n)
|
||||
|
||||
# Linear order
|
||||
linear(n)
|
||||
linear_recur(n)
|
||||
|
||||
# Exponential order
|
||||
quadratic(n)
|
||||
quadratic_recur(n)
|
||||
|
||||
# Exponential order
|
||||
root = build_tree(n)
|
||||
print_tree(root)
|
||||
end
|
||||
Reference in New Issue
Block a user