2021-03-11 17:26:48 +01:00
|
|
|
# Challenge name: Two Sum
|
2021-03-12 06:58:44 +01:00
|
|
|
#
|
2021-03-11 17:26:48 +01:00
|
|
|
# Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
|
|
|
|
#
|
|
|
|
# You may assume that each input would have exactly one solution, and you may not use the same element twice.
|
|
|
|
#
|
|
|
|
# You can return the answer in any order.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Examples
|
|
|
|
#
|
|
|
|
# Input: nums = [2, 7, 11, 15], target = 9
|
|
|
|
# Output: [0,1]
|
|
|
|
# Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
|
|
|
|
#
|
|
|
|
# Input: nums = [3, 2, 4], target = 6
|
|
|
|
# Output: [1,2]
|
|
|
|
#
|
|
|
|
# Input: nums = [3, 3], target = 6
|
|
|
|
# Output: [0,1]
|
|
|
|
# Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
|
|
|
|
#
|
|
|
|
# @param {Integer[]} nums
|
|
|
|
# @param {Integer} target
|
|
|
|
# @return {Integer[]}
|
|
|
|
|
2021-03-12 06:58:44 +01:00
|
|
|
#
|
2021-03-12 00:53:01 +01:00
|
|
|
# Approach 1: Brute Force with Addition
|
2021-03-11 17:26:48 +01:00
|
|
|
#
|
2021-03-12 06:58:44 +01:00
|
|
|
|
|
|
|
# Complexity analysis
|
|
|
|
|
2021-03-12 06:22:39 +01:00
|
|
|
# Time Complexity: O(n^2). For each element, we try to find its complement
|
|
|
|
# by looping through the rest of the array which takes O(n) time.
|
|
|
|
# Therefore, the time complexity is O(n^2)
|
2021-03-12 06:58:44 +01:00
|
|
|
|
2021-03-12 06:22:39 +01:00
|
|
|
# Space complexity: O(1)
|
2021-03-12 06:58:44 +01:00
|
|
|
|
2021-03-11 17:26:48 +01:00
|
|
|
def two_sum(nums, target)
|
|
|
|
result_array = []
|
|
|
|
|
2021-03-12 06:58:44 +01:00
|
|
|
nums.count.times do |i|
|
|
|
|
nums.count.times do |j|
|
|
|
|
next unless i != j && i < j
|
|
|
|
|
|
|
|
current_sum = nums[i] + nums[j]
|
|
|
|
|
|
|
|
return [i, j] if current_sum == target
|
2021-03-11 17:26:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-12 06:58:44 +01:00
|
|
|
print(two_sum([2, 7, 11, 15], 9))
|
2021-03-11 18:20:26 +01:00
|
|
|
# => [0,1]
|
2021-03-12 06:58:44 +01:00
|
|
|
|
|
|
|
print(two_sum([3, 2, 4], 6))
|
2021-03-11 18:20:26 +01:00
|
|
|
# => [1,2]
|
2021-03-12 06:58:44 +01:00
|
|
|
|
|
|
|
print(two_sum([3, 3], 6))
|
2021-03-11 18:20:26 +01:00
|
|
|
# => [0,1]
|
|
|
|
|
|
|
|
#
|
2021-03-12 00:53:01 +01:00
|
|
|
# Approach 2: Brute Force with Difference
|
2021-03-11 18:20:26 +01:00
|
|
|
#
|
2021-03-12 06:58:44 +01:00
|
|
|
# Complexity analysis
|
|
|
|
#
|
2021-03-11 18:20:26 +01:00
|
|
|
# Time Complexity: O(N^2), where N is the length of the array
|
|
|
|
#
|
|
|
|
def two_sum(nums, target)
|
2021-03-12 07:01:02 +01:00
|
|
|
nums.each_with_index do |num, i|
|
|
|
|
target_difference = target - num
|
2021-03-12 06:58:44 +01:00
|
|
|
|
2021-03-11 18:20:26 +01:00
|
|
|
nums.each_with_index do |num, j|
|
2021-09-03 22:24:58 +02:00
|
|
|
return [i, j] if i != j && num == target_difference
|
2021-03-11 18:20:26 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-12 06:58:44 +01:00
|
|
|
print(two_sum([2, 7, 11, 15], 9))
|
2021-03-12 00:53:01 +01:00
|
|
|
# => [0,1]
|
2021-03-12 06:58:44 +01:00
|
|
|
|
|
|
|
print(two_sum([3, 2, 4], 6))
|
2021-03-12 00:53:01 +01:00
|
|
|
# => [1,2]
|
2021-03-12 06:58:44 +01:00
|
|
|
|
|
|
|
print(two_sum([3, 3], 6))
|
2021-03-12 00:53:01 +01:00
|
|
|
# => [0,1]
|