-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path167.2_two_sum.rb
50 lines (38 loc) · 1.1 KB
/
167.2_two_sum.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
50
# frozen_string_literal: true
# https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
require_relative '../test_helper'
# Runtime: 56 ms, faster than 95.90% of Ruby online submissions for Two Sum II - Input Array Is Sorted.
# Memory Usage: 212.2 MB, less than 11.19% of Ruby online submissions for Two Sum II - Input Array Is Sorted.
class Solution
class << self
def two_sum(numbers, target)
left = 0
right = numbers.size - 1
while left < right
result = numbers[left] + numbers[right]
return [left + 1, right + 1] if result == target
result < target ? left += 1 : right -= 1
end
end
end
end
class TestSolution < Minitest::Test
def test_1
numbers = [2, 7, 11, 15]
target = 9
output = [1, 2]
assert_equal output, Solution.two_sum(numbers, target)
end
def test_2
numbers = [2, 3, 4]
target = 6
output = [1, 3]
assert_equal output, Solution.two_sum(numbers, target)
end
def test_3
numbers = [-1, 0]
target = -1
output = [1, 2]
assert_equal output, Solution.two_sum(numbers, target)
end
end