mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-02-06 08:46:04 +01:00
add brute force approach
This commit is contained in:
parent
130e736aa6
commit
c7329e6bf4
1 changed files with 32 additions and 2 deletions
|
@ -15,7 +15,37 @@
|
|||
# Output: [1,1]
|
||||
|
||||
#
|
||||
# Approach 1: Dynamic Programming
|
||||
# Approach 1: Brute Force
|
||||
#
|
||||
|
||||
# Complexity Analysis
|
||||
#
|
||||
# Time complexity: O(k^2).
|
||||
# Space complexity: O(k) + O(k) ~ O(k)
|
||||
|
||||
def get_num(row, col)
|
||||
return 1 if row == 0 || col == 0 || row == col
|
||||
get_num(row - 1, col - 1) + get_num(row - 1, col)
|
||||
end
|
||||
def get_row(row_index)
|
||||
result = []
|
||||
(row_index + 1).times do |i|
|
||||
result.push(get_num(row_index, i))
|
||||
end
|
||||
result
|
||||
end
|
||||
row_index = 3
|
||||
print(get_row(row_index))
|
||||
# => [1,3,3,1]
|
||||
row_index = 0
|
||||
print(get_row(row_index))
|
||||
# => [1]
|
||||
row_index = 1
|
||||
print(get_row(row_index))
|
||||
# => [1,1]
|
||||
|
||||
#
|
||||
# Approach 2: Dynamic Programming
|
||||
#
|
||||
|
||||
# Complexity Analysis
|
||||
|
@ -51,7 +81,7 @@ def generate(num_rows)
|
|||
end
|
||||
|
||||
#
|
||||
# Approach 2: Memory-efficient Dynamic Programming
|
||||
# Approach 3: Memory-efficient Dynamic Programming
|
||||
#
|
||||
|
||||
# Complexity Analysis
|
||||
|
|
Loading…
Add table
Reference in a new issue