From c8b91b82221f3164baa09d21c00fa7c28498ae3a Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:27:59 -0700 Subject: [PATCH 1/7] Add dynamic programming approach 1 --- dynamic_programming/pascal_triangle_ii.rb | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 dynamic_programming/pascal_triangle_ii.rb diff --git a/dynamic_programming/pascal_triangle_ii.rb b/dynamic_programming/pascal_triangle_ii.rb new file mode 100644 index 0000000..564840b --- /dev/null +++ b/dynamic_programming/pascal_triangle_ii.rb @@ -0,0 +1,53 @@ +# Challenge name: Pascal's triangle ii +# +# Given an integer row_index, return the rowIndexth (0-indexed) row of the Pascal's triangle. +# Example 1: +# +# Input: row_index = 3 +# Output: [1,3,3,1] +# +# Example 2: +# +# Input: row_index = 0 +# Output: [1] +# +# Example 3: +# +# Input: row_index = 1 +# Output: [1,1] + +# +# Approach 1: Dynamic Programming +# + +# Complexity Analysis +# +# Time complexity: O(k^2). +# Space complexity: O(k) + O(k) ~ O(k). + +# @param {Integer} row_index +# @return {Integer[]} +def get_row(row_index) + result = generate(row_index) + result[result.count - 1] +end + +def generate(num_rows) + return [[1]] if num_rows < 1 + result = [[1], [1, 1]] + + (2...num_rows + 1).each do |row| + prev = result[row - 1] + current = [1] + med = prev.count / 2 + + (1...prev.count).each do |i| + current[i] = prev[i - 1] + prev[i] + end + + current.push(1) + result.push(current) + end + + result +end From a0af18e002445a6925995ab6c7b277a13efab5b7 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:28:05 -0700 Subject: [PATCH 2/7] Add dynamic programming approach 2 --- dynamic_programming/pascal_triangle_ii.rb | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/dynamic_programming/pascal_triangle_ii.rb b/dynamic_programming/pascal_triangle_ii.rb index 564840b..837d2f0 100644 --- a/dynamic_programming/pascal_triangle_ii.rb +++ b/dynamic_programming/pascal_triangle_ii.rb @@ -51,3 +51,41 @@ def generate(num_rows) result end + +# +# Approach 2: Memory-efficient Dynamic Programming +# + +# Complexity Analysis +# +# Time complexity: O(k^2). +# Space complexity: O(k). + +# @param {Integer} row_index +# @return {Integer[]} +def get_row(row_index) + pascal = [[1]] + + (1..row_index).each do |i| + pascal[i] = [] + pascal[i][0] = pascal[i][i] = 1 + + (1...i).each do |j| + pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j] + end + end + + pascal[row_index] +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] From e202d15e6b4720e8f1d64893cf764ea29c86b5e9 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 8 Jun 2021 18:28:22 +0000 Subject: [PATCH 3/7] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6babc0a..8f75d7c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -78,6 +78,7 @@ * [Coin Change](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/coin_change.rb) * [Count Sorted Vowel Strings](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/count_sorted_vowel_strings.rb) * [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb) + * [Pascal Triangle Ii](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/pascal_triangle_ii.rb) ## Maths * [Abs](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs.rb) From 130e736aa66944070def5598910ab2c6a1bf3f1d Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:28:22 -0700 Subject: [PATCH 4/7] minor changes --- dynamic_programming/pascal_triangle_ii.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/dynamic_programming/pascal_triangle_ii.rb b/dynamic_programming/pascal_triangle_ii.rb index 837d2f0..761f0c1 100644 --- a/dynamic_programming/pascal_triangle_ii.rb +++ b/dynamic_programming/pascal_triangle_ii.rb @@ -1,5 +1,3 @@ -# Challenge name: Pascal's triangle ii -# # Given an integer row_index, return the rowIndexth (0-indexed) row of the Pascal's triangle. # Example 1: # From c7329e6bf40447088db04953a50cb43be6c2d6fd Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:30:08 -0700 Subject: [PATCH 5/7] add brute force approach --- dynamic_programming/pascal_triangle_ii.rb | 34 +++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/pascal_triangle_ii.rb b/dynamic_programming/pascal_triangle_ii.rb index 761f0c1..17119aa 100644 --- a/dynamic_programming/pascal_triangle_ii.rb +++ b/dynamic_programming/pascal_triangle_ii.rb @@ -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 From b4d5726791d375ad8388b741793f6843b8ea73a2 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:36:07 -0700 Subject: [PATCH 6/7] add enter --- dynamic_programming/pascal_triangle_ii.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dynamic_programming/pascal_triangle_ii.rb b/dynamic_programming/pascal_triangle_ii.rb index 17119aa..568e76d 100644 --- a/dynamic_programming/pascal_triangle_ii.rb +++ b/dynamic_programming/pascal_triangle_ii.rb @@ -25,21 +25,28 @@ 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] From 6defd7eab5775857171589a8ee54d3c6bd7b8957 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:42:17 -0700 Subject: [PATCH 7/7] add missing output --- dynamic_programming/pascal_triangle_ii.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/dynamic_programming/pascal_triangle_ii.rb b/dynamic_programming/pascal_triangle_ii.rb index 568e76d..f9e73f3 100644 --- a/dynamic_programming/pascal_triangle_ii.rb +++ b/dynamic_programming/pascal_triangle_ii.rb @@ -87,6 +87,18 @@ def generate(num_rows) 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 3: Memory-efficient Dynamic Programming #