Merge branch 'TheAlgorithms:master' into master

This commit is contained in:
Sahil Afrid Farookhi 2021-08-29 23:58:07 +05:30 committed by GitHub
commit 5ca172acab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 152 additions and 0 deletions

View file

@ -78,6 +78,7 @@
* [Lcm](https://github.com/TheAlgorithms/Ruby/blob/master/discrete_mathematics/lcm.rb) * [Lcm](https://github.com/TheAlgorithms/Ruby/blob/master/discrete_mathematics/lcm.rb)
## Dynamic Programming ## Dynamic Programming
* [Climbing Stairs](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/climbing_stairs.rb)
* [Coin Change](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/coin_change.rb) * [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) * [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) * [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb)
@ -146,6 +147,7 @@
* [Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/double_linear_search.rb) * [Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/double_linear_search.rb)
* [Jump Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/jump_search.rb) * [Jump Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/jump_search.rb)
* [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb) * [Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/linear_search.rb)
* [Number Of Islands](https://github.com/TheAlgorithms/Ruby/blob/master/searches/number_of_islands.rb)
* [Recursive Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_double_linear_search.rb) * [Recursive Double Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_double_linear_search.rb)
* [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_linear_search.rb) * [Recursive Linear Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/recursive_linear_search.rb)
* [Ternary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/ternary_search.rb) * [Ternary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/ternary_search.rb)

View file

@ -0,0 +1,71 @@
#You are climbing a staircase. It takes n steps to reach the top.
#Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
#Example 1:
#Input: n = 2
#Output: 2
#Explanation: There are two ways to climb to the top.
#1. 1 step + 1 step
#2. 2 steps
#Example 2:
#Input: n = 3
#Output: 3
#Explanation: There are three ways to climb to the top.
#1. 1 step + 1 step + 1 step
#2. 1 step + 2 steps
#3. 2 steps + 1 step
#Constraints:
#1 <= n <= 45
#Dynamic Programming, Recursive Bottom Up Approach - O(n) Time / O(n) Space
#Init memoization hash (only 1 parameter)
#Set base cases which are memo[0] = 1 and memo[1] = 1, since there are only 1 way to get to each stair
#Iterate from 2..n and call recurse(n, memo) for each value n.
#Return memo[n].
#recurse(n, memo) - Recurrence Relation is n = (n - 1) + (n - 2)
#return memo[n] if memo[n] exists.
#otherwise, memo[n] = recurse(n - 1, memo) + recurse(n - 2, memo)
# @param {Integer} n
# @return {Integer}
def climb_stairs(n)
memo = Hash.new
memo[0] = 1
memo[1] = 1
return memo[n] if n == 0 || n == 1
(2..n).each do |n|
recurse(n, memo)
end
memo[n]
end
def recurse(n, memo)
return memo[n] if memo[n]
memo[n] = recurse(n - 1, memo) + recurse(n - 2, memo)
end
puts climb_stairs(2)
# => 2
puts climb_stairs(4)
# => 5
puts climb_stairs(10)
# => 89
puts climb_stairs(45)
# => 1836311903

View file

@ -0,0 +1,79 @@
#Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
#An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
#Example 1:
#Input: grid = [
# ["1","1","1","1","0"],
# ["1","1","0","1","0"],
# ["1","1","0","0","0"],
# ["0","0","0","0","0"]
#]
#Output: 1
#Example 2:
#Input: grid = [
# ["1","1","0","0","0"],
# ["1","1","0","0","0"],
# ["0","0","1","0","0"],
# ["0","0","0","1","1"]
#]
#Output: 3
#Constraints:
#m == grid.length
#n == grid[i].length
#1 <= m, n <= 300
#grid[i][j] is '0' or '1'.
#DFS, Recursive Bottom Up Approach - O(n*m) Time / O(1) Space
#Init num_of_islands = 0, return if the grid is empty
#Start a double loop with index to iterate through each plot (each value is a plot of either water or land in this case)
#if the plot is land, dfs(grid, x, y)
#num_of_islands += 1
#Return num_of_islands
#dfs(grid, x, y)
#Return if x or y are out of bounds, or if the plot is water
#Make the current plot water
#Call dfs again for up, down, left, and right
# @param {Character[][]} grid
# @return {Integer}
def num_islands(grid)
return 0 if grid.empty?
#init num of islands
islands = 0
#loop through each element (plot) in the 2d array
grid.each_with_index do |row, x|
row.each_with_index do |plot, y|
#if the plot is water, start a dfs
if plot == "1"
dfs(grid, x, y)
#add 1 to islands once all connected land plots are searched
islands += 1
end
end
end
#return ans
islands
end
def dfs(grid, x, y)
#don't search if out of bounds, or if it's already water
return if x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] == "0"
#set the plot to water
grid[x][y] = "0"
#search each adjacent plot
dfs(grid, x - 1, y) #up
dfs(grid, x + 1, y) #down
dfs(grid, x, y - 1) #left
dfs(grid, x, y + 1) #right
end