mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Merge pull request #171 from sidaksohi/add-array-solutions-with-descriptions
Add 'array' solutions, with descriptions
This commit is contained in:
commit
615e07dc76
3 changed files with 204 additions and 0 deletions
88
data_structures/arrays/3sum.rb
Normal file
88
data_structures/arrays/3sum.rb
Normal file
|
@ -0,0 +1,88 @@
|
|||
# Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] ..
|
||||
# .. such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
|
||||
# Notice that the solution set must not contain duplicate triplets.
|
||||
|
||||
# Example 1:
|
||||
# Input: nums = [-1,0,1,2,-1,-4]
|
||||
# Output: [[-1,-1,2],[-1,0,1]]
|
||||
|
||||
# Example 2:
|
||||
# Input: nums = []
|
||||
# Output: []
|
||||
|
||||
# Example 3:
|
||||
# Input: nums = [0]
|
||||
# Output: []
|
||||
|
||||
# Constraints:
|
||||
# 0 <= nums.length <= 3000
|
||||
#-105 <= nums[i] <= 105
|
||||
|
||||
# Two Pointer Approach - O(n) Time / O(1) Space
|
||||
# Return edge cases.
|
||||
# Sort nums, and init ans array
|
||||
# For each |val, index| in nums:
|
||||
# if the current value is the same as last, then go to next iteration
|
||||
# init left and right pointers for two pointer search of the two sum in remaining elements of array
|
||||
# while left < right:
|
||||
# find current sum
|
||||
# if sum > 0, right -= 1
|
||||
# if sum < 0, left += 1
|
||||
# if it's 0, then add the values to the answer array, and set the left pointer to the next valid value ..
|
||||
# .. (left += 1 while nums[left] == nums[left - 1] && left < right)
|
||||
# Return ans[]
|
||||
|
||||
# @param {Integer[]} nums
|
||||
# @return {Integer[][]}
|
||||
def three_sum(nums)
|
||||
# return if length too short
|
||||
return [] if nums.length < 3
|
||||
|
||||
# sort nums, init ans array
|
||||
nums = nums.sort
|
||||
ans = []
|
||||
|
||||
# loop through nums
|
||||
nums.each_with_index do |val, ind|
|
||||
# if the previous value is the same as current, then skip this iteration as it would create duplicates
|
||||
next if ind > 0 && nums[ind] == nums[ind - 1]
|
||||
|
||||
# init & run two pointer search
|
||||
left = ind + 1
|
||||
right = nums.length - 1
|
||||
|
||||
while left < right
|
||||
# find current sum
|
||||
sum = val + nums[left] + nums[right]
|
||||
|
||||
# decrease sum if it's too great, increase sum if it's too low
|
||||
if sum > 0
|
||||
right -= 1
|
||||
elsif sum < 0
|
||||
left += 1
|
||||
# if it's zero, then add the answer to array and set left pointer to next valid value
|
||||
else
|
||||
ans << [val, nums[left], nums[right]]
|
||||
|
||||
left += 1
|
||||
|
||||
left += 1 while nums[left] == nums[left - 1] && left < right
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# return answer
|
||||
ans
|
||||
end
|
||||
|
||||
nums = [-1, 0, 1, 2, -1, -4]
|
||||
print three_sum(nums)
|
||||
# Output: [[-1,-1,2],[-1,0,1]]
|
||||
|
||||
nums = []
|
||||
print three_sum(nums)
|
||||
# Output: []
|
||||
|
||||
nums = [0]
|
||||
print three_sum(nums)
|
||||
# Output: []
|
50
data_structures/arrays/maximum_product_subarray.rb
Normal file
50
data_structures/arrays/maximum_product_subarray.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
# Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.
|
||||
# It is guaranteed that the answer will fit in a 32-bit integer.
|
||||
# A subarray is a contiguous subsequence of the array.
|
||||
|
||||
# Example 1:
|
||||
# Input: nums = [2,3,-2,4]
|
||||
# Output: 6
|
||||
# Explanation: [2,3] has the largest product 6.
|
||||
|
||||
# Example 2:
|
||||
# Input: nums = [-2,0,-1]
|
||||
# Output: 0
|
||||
# Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
|
||||
|
||||
# Constraints:
|
||||
# 1 <= nums.length <= 2 * 104
|
||||
#-10 <= nums[i] <= 10
|
||||
# The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
|
||||
|
||||
# Dynamic Programming Approach (Kadane's Algorithm) - O(n) Time / O(1) Space
|
||||
# Track both current minimum and current maximum (Due to possibility of multiple negative numbers)
|
||||
# Answer is the highest value of current maximum
|
||||
|
||||
# @param {Integer[]} nums
|
||||
# @return {Integer}
|
||||
def max_product(nums)
|
||||
return nums[0] if nums.length == 1
|
||||
|
||||
cur_min = 1
|
||||
cur_max = 1
|
||||
max = -11
|
||||
|
||||
nums.each do |val|
|
||||
tmp_cur_max = cur_max
|
||||
cur_max = [val, val * cur_max, val * cur_min].max
|
||||
cur_min = [val, val * tmp_cur_max, val * cur_min].min
|
||||
|
||||
max = [max, cur_max].max
|
||||
end
|
||||
|
||||
max
|
||||
end
|
||||
|
||||
nums = [2, 3, -2, 4]
|
||||
puts max_product(nums)
|
||||
# Output: 6
|
||||
|
||||
nums = [-2, 0, -1]
|
||||
puts max_product(nums)
|
||||
# Output: 0
|
66
data_structures/arrays/maximum_subarray.rb
Normal file
66
data_structures/arrays/maximum_subarray.rb
Normal file
|
@ -0,0 +1,66 @@
|
|||
# Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
|
||||
# A subarray is a contiguous part of an array.
|
||||
|
||||
# Example 1:
|
||||
# Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
|
||||
# Output: 6
|
||||
# Explanation: [4,-1,2,1] has the largest sum = 6.
|
||||
|
||||
# Example 2:
|
||||
# Input: nums = [1]
|
||||
# Output: 1
|
||||
|
||||
# Example 3:
|
||||
# Input: nums = [5,4,-1,7,8]
|
||||
# Output: 23
|
||||
|
||||
# Constraints:
|
||||
# 1 <= nums.length <= 3 * 104
|
||||
# -105 <= nums[i] <= 105
|
||||
|
||||
# Dynamic Programming Approach (Kadane's Algorithm) - O(n) Time / O(1) Space
|
||||
#
|
||||
# Init max_sum as first element
|
||||
# Return first element if the array length is 1
|
||||
# Init current_sum as 0
|
||||
# Iterate through the array:
|
||||
# if current_sum < 0, then reset it to 0 (to eliminate any negative prefixes)
|
||||
# current_sum += num
|
||||
# max_sum = current_sum if current_sum is greater than max_sum
|
||||
# Return max_sum
|
||||
|
||||
# @param {Integer[]} nums
|
||||
# @return {Integer}
|
||||
def max_sub_array(nums)
|
||||
# initialize max sum to first number
|
||||
max_sum = nums[0]
|
||||
|
||||
# return first number if array length is 1
|
||||
return max_sum if nums.length == 1
|
||||
|
||||
# init current sum to 0
|
||||
current_sum = 0
|
||||
|
||||
# iterate through array, reset current_sum to 0 if it ever goes below 0, track max_sum with highest current_sum
|
||||
nums.each do |num|
|
||||
current_sum = 0 if current_sum < 0
|
||||
|
||||
current_sum += num
|
||||
|
||||
max_sum = [max_sum, current_sum].max
|
||||
end
|
||||
|
||||
max_sum
|
||||
end
|
||||
|
||||
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
|
||||
print max_sub_array(nums)
|
||||
# Output: 6
|
||||
|
||||
nums = [1]
|
||||
print max_sub_array(nums)
|
||||
# Output: 1
|
||||
|
||||
nums = [5, 4, -1, 7, 8]
|
||||
print max_sub_array(nums)
|
||||
# Output: 23
|
Loading…
Reference in a new issue