-
-
Notifications
You must be signed in to change notification settings - Fork 295
/
two_sum_ii.rb
49 lines (37 loc) · 1.07 KB
/
two_sum_ii.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Given an array of integers numbers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
# Return the indices of the two numbers (1-indexed) as an integer array answer of size 2, where 1 <= answer[0] < answer[1] <= numbers.length.
# You may assume that each input would have exactly one solution and you may not use the same element twice.
#
# Approach 1: Two pointers
#
# Complexity analysis
# Time complexity: O(n). Each of the n elements is visited at
# most once, thus the time complexity is O(n).
# Space complexity: O(1). We only use two indexes, the space
# complexity is O(1).
def two_sum(numbers, target)
i = 0
j = numbers.length - 1
while i < j
sum = numbers[i] + numbers[j]
if target < sum
j -= 1
elsif target > sum
i += 1
else
return [i + 1, j + 1]
end
end
end
nums = [2, 7, 11, 15]
target = 9
print(two_sum(nums, target))
# => [1,2]
nums = [2, 3, 4]
target = 6
print(two_sum(nums, target))
# => [1,3]
nums = [-1, 0]
target = -1
print(two_sum(nums, target))
# => [1,2]