From 2032eef8242b6db31f97f892fa832a73cd767111 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 14 May 2021 16:56:07 -0700 Subject: [PATCH 01/15] Add common characters challenge --- DIRECTORY.md | 1 + .../arrays/strings/common_characters.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 data_structures/arrays/strings/common_characters.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index d0c4469..fcd790b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -22,6 +22,7 @@ * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) * Strings * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb) + * [Common Characters](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/common_characters.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb) diff --git a/data_structures/arrays/strings/common_characters.rb b/data_structures/arrays/strings/common_characters.rb new file mode 100644 index 0000000..42205aa --- /dev/null +++ b/data_structures/arrays/strings/common_characters.rb @@ -0,0 +1,19 @@ +# Challenge name: Find Common Characters +# +# Given an array A of strings made only from lowercase letters, return a list +# of all characters that show up in all strings within the list +# (including duplicates). For example, if a character occurs 3 times in all +# strings but not 4 times, you need to include that character three times in +# the final answer. +# +# You may return the answer in any order. +# +# Example 1: +# +# Input: ["bella","label","roller"] +# Output: ["e","l","l"] +# +# Example 2: +# +# Input: ["cool","lock","cook"] +# Output: ["c","o"] \ No newline at end of file From 60d4a4bcbce7b73a1bf48437ea92a973bb3caa7b Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Tue, 1 Jun 2021 13:11:08 -0700 Subject: [PATCH 02/15] Add hash solution --- .../arrays/strings/common_characters.rb | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/strings/common_characters.rb b/data_structures/arrays/strings/common_characters.rb index 42205aa..6d126c5 100644 --- a/data_structures/arrays/strings/common_characters.rb +++ b/data_structures/arrays/strings/common_characters.rb @@ -9,11 +9,43 @@ # You may return the answer in any order. # # Example 1: -# # Input: ["bella","label","roller"] # Output: ["e","l","l"] # # Example 2: -# # Input: ["cool","lock","cook"] -# Output: ["c","o"] \ No newline at end of file +# Output: ["c","o"] + +# +# Approach 1: Hash +# +# Time Complexity: O(n) +# +def common_characters(arr) + target_count = arr.count + + hash = Hash.new(0) + (0...target_count).each do |i| + arr[i].split('').each do |letter| + hash[letter] += 1 + end + end + + result = [] + hash.each do |k, v| + while v >= target_count + if v >= target_count + result << k + v -= target_count + end + end + end + + result +end + +puts common_characters(["bella","label","roller"]) +# => ["e","l","l"] + +puts common_characters(["cool","lock","cook"]) +# => ["c","o"] \ No newline at end of file From 3a104e9ff0c018475c75bb75d945119041560240 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 3 Jun 2021 10:11:21 -0700 Subject: [PATCH 03/15] Move solution to hash table section --- DIRECTORY.md | 2 +- .../{arrays/strings => hash_table}/common_characters.rb | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename data_structures/{arrays/strings => hash_table}/common_characters.rb (100%) diff --git a/DIRECTORY.md b/DIRECTORY.md index fcd790b..43f1f82 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -22,7 +22,6 @@ * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) * Strings * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb) - * [Common Characters](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/common_characters.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb) @@ -38,6 +37,7 @@ * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/two_sum.rb) * Linked Lists * [Circular Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/circular_linked_list.rb) + * [Common Characters](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/common_characters.rb) * [Doubly Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/doubly_linked_list.rb) * [Singly Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/singly_linked_list.rb) * Queues diff --git a/data_structures/arrays/strings/common_characters.rb b/data_structures/hash_table/common_characters.rb similarity index 100% rename from data_structures/arrays/strings/common_characters.rb rename to data_structures/hash_table/common_characters.rb From c01e4b5b0c423ab6840ce6975f74abdb5abac85a Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 3 Jun 2021 10:12:02 -0700 Subject: [PATCH 04/15] Update directory with solution in hash table section --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 43f1f82..c94148f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -32,12 +32,12 @@ * [Postorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/postorder_traversal.rb) * [Preorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/preorder_traversal.rb) * Hash Table + * [Common Characters](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/common_characters.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/find_all_duplicates_in_an_array.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/two_sum.rb) * Linked Lists * [Circular Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/circular_linked_list.rb) - * [Common Characters](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/common_characters.rb) * [Doubly Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/doubly_linked_list.rb) * [Singly Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/singly_linked_list.rb) * Queues From aece3afeee5232df496df8d382cfae1c29c83f79 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 8 Jun 2021 02:39:00 +0000 Subject: [PATCH 05/15] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index c9c954d..6babc0a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -51,7 +51,7 @@ * Hash Table * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/anagram_checker.rb) * [Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/arrays_intersection.rb) - * [Common Characters](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/common_characters.rb) + * [Common Characters](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/common_characters.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/find_all_duplicates_in_an_array.rb) * [Good Pairs](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/good_pairs.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb) From 5f6036b6c69810d7a12c3765531b72a472c45fd1 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:25:04 -0700 Subject: [PATCH 06/15] Pascal triangle ii: math approach --- maths/pascal_triangle_ii.rb | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 maths/pascal_triangle_ii.rb diff --git a/maths/pascal_triangle_ii.rb b/maths/pascal_triangle_ii.rb new file mode 100644 index 0000000..81fcb11 --- /dev/null +++ b/maths/pascal_triangle_ii.rb @@ -0,0 +1,48 @@ +# 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] + +# Complexity Analysis +# Time complexity: O(k). +# Each term is calculated once, in constant time. + +# Space complexity: O(k). +# No extra space required other than that required to hold the output. + +def get_row(row_index) + (0..row_index).map {|num| combination(row_index, num) } +end + +def combination(num1, num2) + factorial(num1) / (factorial(num2) * factorial(num1 - num2)) +end + +def factorial(num) + (1..num).inject(1) {|res, i| res * i } +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 2763a871fafcb71297f1645ef8411c3534975231 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 8 Jun 2021 18:25:23 +0000 Subject: [PATCH 07/15] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6babc0a..53c8b26 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -103,6 +103,7 @@ * [Find Min](https://github.com/TheAlgorithms/Ruby/blob/master/maths/find_min.rb) * [Lucas Series](https://github.com/TheAlgorithms/Ruby/blob/master/maths/lucas_series.rb) * [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb) + * [Pascal Triangle Ii](https://github.com/TheAlgorithms/Ruby/blob/master/maths/pascal_triangle_ii.rb) * [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/maths/power_of_two.rb) * [Prime Number](https://github.com/TheAlgorithms/Ruby/blob/master/maths/prime_number.rb) * [Roman To Integer](https://github.com/TheAlgorithms/Ruby/blob/master/maths/roman_to_integer.rb) From c8b91b82221f3164baa09d21c00fa7c28498ae3a Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:27:59 -0700 Subject: [PATCH 08/15] 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 09/15] 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 10/15] 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 11/15] 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 12/15] 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 08b2e6b5476470321414c1a1eaff1991ebf1b634 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:31:15 -0700 Subject: [PATCH 13/15] minor changes --- maths/pascal_triangle_ii.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/maths/pascal_triangle_ii.rb b/maths/pascal_triangle_ii.rb index 81fcb11..de60abd 100644 --- a/maths/pascal_triangle_ii.rb +++ b/maths/pascal_triangle_ii.rb @@ -1,6 +1,5 @@ -# Challenge name: Pascal's triangle ii -# -# Given an integer row_index, return the rowIndexth (0-indexed) row of the Pascal's triangle. +# Given an integer row_index, return the rowIndexth (0-indexed) row of the Pascal's triangle. + # Example 1: # # Input: row_index = 3 @@ -17,11 +16,9 @@ # Output: [1,1] # Complexity Analysis +# # Time complexity: O(k). -# Each term is calculated once, in constant time. - # Space complexity: O(k). -# No extra space required other than that required to hold the output. def get_row(row_index) (0..row_index).map {|num| combination(row_index, num) } @@ -32,7 +29,7 @@ def combination(num1, num2) end def factorial(num) - (1..num).inject(1) {|res, i| res * i } + (1..num).inject(1) { |res, i| res * i } end row_index = 3 From b4d5726791d375ad8388b741793f6843b8ea73a2 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Tue, 8 Jun 2021 11:36:07 -0700 Subject: [PATCH 14/15] 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 15/15] 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 #