Skip to content

Commit e676e28

Browse files
committed
Add array solutions with descriptions
1 parent 02db2b9 commit e676e28

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

data_structures/arrays/3sum.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] ..
2+
#.. such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
3+
#Notice that the solution set must not contain duplicate triplets.
4+
5+
#Example 1:
6+
#Input: nums = [-1,0,1,2,-1,-4]
7+
#Output: [[-1,-1,2],[-1,0,1]]
8+
9+
#Example 2:
10+
#Input: nums = []
11+
#Output: []
12+
13+
#Example 3:
14+
#Input: nums = [0]
15+
#Output: []
16+
17+
#Constraints:
18+
#0 <= nums.length <= 3000
19+
#-105 <= nums[i] <= 105
20+
21+
22+
23+
24+
#Two Pointer Approach - O(n) Time / O(1) Space
25+
#Return edge cases.
26+
#Sort nums, and init ans array
27+
#For each |val, index| in nums:
28+
#if the current value is the same as last, then go to next iteration
29+
#init left and right pointers for two pointer search of the two sum in remaining elements of array
30+
#while left < right:
31+
#find current sum
32+
#if sum > 0, right -= 1
33+
#if sum < 0, left += 1
34+
#if it's 0, then add the values to the answer array, and set the left pointer to the next valid value ..
35+
#.. (left += 1 while nums[left] == nums[left - 1] && left < right)
36+
#Return ans[]
37+
38+
39+
# @param {Integer[]} nums
40+
# @return {Integer[][]}
41+
def three_sum(nums)
42+
#return if length too short
43+
return [] if nums.length < 3
44+
45+
#sort nums, init ans array
46+
nums, ans = nums.sort, []
47+
48+
#loop through nums
49+
nums.each_with_index do |val, ind|
50+
#if the previous value is the same as current, then skip this iteration as it would create duplicates
51+
next if ind > 0 && nums[ind] == nums[ind - 1]
52+
53+
#init & run two pointer search
54+
left, right = ind + 1, nums.length - 1
55+
56+
while left < right
57+
#find current sum
58+
sum = val + nums[left] + nums[right]
59+
60+
#decrease sum if it's too great, increase sum if it's too low
61+
if sum > 0
62+
right -= 1
63+
elsif sum < 0
64+
left += 1
65+
#if it's zero, then add the answer to array and set left pointer to next valid value
66+
else
67+
ans << [val, nums[left], nums[right]]
68+
69+
left += 1
70+
71+
while nums[left] == nums[left - 1] && left < right
72+
left += 1
73+
end
74+
end
75+
end
76+
end
77+
78+
#return answer
79+
ans
80+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.
2+
#It is guaranteed that the answer will fit in a 32-bit integer.
3+
#A subarray is a contiguous subsequence of the array.
4+
5+
#Example 1:
6+
#Input: nums = [2,3,-2,4]
7+
#Output: 6
8+
#Explanation: [2,3] has the largest product 6.
9+
10+
#Example 2:
11+
#Input: nums = [-2,0,-1]
12+
#Output: 0
13+
#Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
14+
15+
#Constraints:
16+
#1 <= nums.length <= 2 * 104
17+
#-10 <= nums[i] <= 10
18+
#The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
19+
20+
21+
22+
23+
#Dynamic Programming Approach (Kadane's Algorithm) - O(n) Time / O(1) Space
24+
#Track both current minimum and current maximum (Due to possibility of multiple negative numbers)
25+
#Answer is the highest value of current maximum
26+
27+
28+
# @param {Integer[]} nums
29+
# @return {Integer}
30+
def max_product(nums)
31+
return nums[0] if nums.length == 1
32+
33+
cur_min, cur_max, max = 1, 1, -11
34+
35+
nums.each do |val|
36+
tmp_cur_max = cur_max
37+
cur_max = [val, val*cur_max, val*cur_min].max
38+
cur_min = [val, val*tmp_cur_max, val*cur_min].min
39+
40+
max = [max, cur_max].max
41+
end
42+
43+
max
44+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
2+
#A subarray is a contiguous part of an array.
3+
4+
#Example 1:
5+
#Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
6+
#Output: 6
7+
#Explanation: [4,-1,2,1] has the largest sum = 6.
8+
9+
#Example 2:
10+
#Input: nums = [1]
11+
#Output: 1
12+
13+
#Example 3:
14+
#Input: nums = [5,4,-1,7,8]
15+
#Output: 23
16+
17+
#Constraints:
18+
#1 <= nums.length <= 3 * 104
19+
#-105 <= nums[i] <= 105
20+
21+
22+
23+
24+
#Sliding Window Approach - O(n) Time / O(1) Space
25+
#Init max_sum as first element
26+
#Return first element if the array length is 1
27+
#Init current_sum as 0
28+
#Iterate through the array:
29+
#if current_sum < 0, then reset it to 0 (to eliminate any negative prefixes)
30+
#current_sum += num
31+
#max_sum = current_sum if current_sum is greater than max_sum
32+
#Return max_sum
33+
34+
35+
# @param {Integer[]} nums
36+
# @return {Integer}
37+
def max_sub_array(nums)
38+
#initialize max sum to first number
39+
max_sum = nums[0]
40+
41+
#return first number if array length is 1
42+
return max_sum if nums.length == 1
43+
44+
#init current sum to 0
45+
current_sum = 0
46+
47+
#iterate through array, reset current_sum to 0 if it ever goes below 0, track max_sum with highest current_sum
48+
nums.each do |num|
49+
current_sum = 0 if current_sum < 0
50+
current_sum += num
51+
max_sum = [max_sum, current_sum].max
52+
end
53+
54+
#return answer
55+
max_sum
56+
end

0 commit comments

Comments
 (0)