Skip to content

Commit

Permalink
70 && 83 && 88
Browse files Browse the repository at this point in the history
  • Loading branch information
a-abdellatif98 committed Jun 10, 2022
1 parent 702716b commit 291b5be
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Climbing_Stairs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# @lc app=leetcode id=70 lang=ruby
#
# [70] Climbing Stairs
#
# https://leetcode.com/problems/climbing-stairs/description/
#
# You are climbing a stair case. It takes n steps to reach to the top.
#
# Each time you can either climb 1 or 2 steps. In how many distinct
# ways can you climb to the top?
#
# Note: Given n will be a positive integer.
#
# Example 1:
#
# Input: 2
# Output: 2
# Explanation: There are two ways to climb to the top.
# 1. 1 step + 1 step
# 2. 2 steps
#
# Example 2:
#
# Input: 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


# @param {Integer} n
# @return {Integer}
def climb_stairs(n)
Hash.new { |h, k| h[k] = h[k-1] + h[k-2] }.merge({1=>1, 2=>2})[n]
end
24 changes: 24 additions & 0 deletions Merge_Sorted_Array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# @param {Integer[]} nums1
# @param {Integer} m
# @param {Integer[]} nums2
# @param {Integer} n
# @return {Void} Do not return anything, modify nums1 in-place instead.
def merge(nums1, m, nums2, n)
i, j, k = n - 1, m - 1, nums1.size - 1

while i >= 0
if j < 0
nums1[k] = nums2[i]
i -= 1
elsif nums1[j] > nums2[i]
nums1[k] = nums1[j]
j -= 1
else
nums1[k] = nums2[i]
i -= 1
end
k -= 1
end

nums1
end
13 changes: 13 additions & 0 deletions Remove_Duplicates_from_Sorted_Linked_List.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def delete_duplicates(head)
cursor = head

while cursor and cursor.next
if cursor.next.val == cursor.val
cursor.next = cursor.next.next
else
cursor = cursor.next
end
end

head
end

0 comments on commit 291b5be

Please sign in to comment.