Skip to content

Commit

Permalink
Merge pull request #88 from BrianLusina/feat/two-sum
Browse files Browse the repository at this point in the history
Algorithms | Arrays - Two sum
  • Loading branch information
BrianLusina authored Mar 15, 2024
2 parents 20f7bf8 + bc7475c commit 1f56e15
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
12 changes: 12 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* [Test Remove Duplicates](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/remove_duplicates/test_remove_duplicates.py)
* Remove Element
* [Test Remove Element](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/remove_element/test_remove_element.py)
* Two Sum
* [Test Two Sum](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/arrays/two_sum/test_two_sum.py)
* Backtracking
* Combination
* [Test Combination 2](https://github.com/BrianLusina/PythonSnips/blob/master/algorithms/backtracking/combination/test_combination_2.py)
Expand Down Expand Up @@ -373,6 +375,8 @@
* [Test Array 3 Pointers](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/array_3_pointers/test_array_3_pointers.py)
* Can Place Flowers
* [Test Can Place Flowers](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/can_place_flowers/test_can_place_flowers.py)
* Candy
* [Test Candy](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/candy/test_candy.py)
* Container With Most Water
* [Test Container With Most Water](https://github.com/BrianLusina/PythonSnips/blob/master/puzzles/arrays/container_with_most_water/test_container_with_most_water.py)
* Gas Stations
Expand Down Expand Up @@ -568,6 +572,8 @@
* [Test Mini Max Sum](https://github.com/BrianLusina/PythonSnips/blob/master/pymath/mini_max_sum/test_mini_max_sum.py)
* Multiply 5
* [Test Multiply 5](https://github.com/BrianLusina/PythonSnips/blob/master/pymath/multiply_5/test_multiply_5.py)
* Pascals Triangle
* [Test Pascals Triangle](https://github.com/BrianLusina/PythonSnips/blob/master/pymath/pascals_triangle/test_pascals_triangle.py)
* Perfect Square
* [Test Perfect Squares](https://github.com/BrianLusina/PythonSnips/blob/master/pymath/perfect_square/test_perfect_squares.py)
* Super Size
Expand All @@ -593,6 +599,8 @@
* [Test Balanced Paren](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/balanced_paren/test_balanced_paren.py)
* Domain Name
* [Extract Host](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/domain_name/extract_host.py)
* First Occurrence
* [Test First Occurrence](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/first_occurrence/test_first_occurrence.py)
* Greatest Common Divisor
* [Test Greatest Common Divisor](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/greatest_common_divisor/test_greatest_common_divisor.py)
* Issubsequence
Expand Down Expand Up @@ -625,6 +633,8 @@
* [Test Reverse Words](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/reverse_words/test_reverse_words.py)
* String Compression
* [Test String Compression](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/string_compression/test_string_compression.py)
* Text Justification
* [Test Text Justification](https://github.com/BrianLusina/PythonSnips/blob/master/pystrings/text_justification/test_text_justification.py)

## Reactivex
* [Concurrency](https://github.com/BrianLusina/PythonSnips/blob/master/reactivex/concurrency.py)
Expand Down Expand Up @@ -986,6 +996,8 @@
* [String1](https://github.com/BrianLusina/PythonSnips/blob/master/utils/basic/string1.py)
* [String2](https://github.com/BrianLusina/PythonSnips/blob/master/utils/basic/string2.py)
* [Wordcount](https://github.com/BrianLusina/PythonSnips/blob/master/utils/basic/wordcount.py)
* Benchmark
* [Timer](https://github.com/BrianLusina/PythonSnips/blob/master/utils/benchmark/timer.py)
* Context Mgrs
* [Array Init Test](https://github.com/BrianLusina/PythonSnips/blob/master/utils/context_mgrs/array_init_test.py)
* [Perf Timer](https://github.com/BrianLusina/PythonSnips/blob/master/utils/context_mgrs/perf_timer.py)
Expand Down
42 changes: 42 additions & 0 deletions algorithms/arrays/two_sum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Two Sum - Input Array Is Sorted

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that
they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <=
index1 < index2 <= numbers.length.

Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.

The tests are generated such that there is exactly one solution. You may not use the same element twice.

Your solution must use only constant extra space.

```text
Example 1:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
```

```text
Example 2:
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
```

```text
Example 3:
Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
```

## Related Topics

- Array
- Two Pointers
- Binary Search
12 changes: 12 additions & 0 deletions algorithms/arrays/two_sum/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import List


def two_sum(numbers: List[int], target: int) -> List[int]:
m = {}

for idx, num in enumerate(numbers, start=1):
complement = target - num

if complement in m:
return [m[complement], idx]
m[num] = idx
32 changes: 32 additions & 0 deletions algorithms/arrays/two_sum/test_two_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
from . import two_sum


class TwoSumTestCase(unittest.TestCase):
def test_1(self):
"""numbers = [2,7,11,15], target = 9"""
numbers = [2, 7, 11, 15]
target = 9
expected = [1, 2]
actual = two_sum(numbers, target)
self.assertEqual(expected, actual)

def test_2(self):
"""numbers = [2,3,4], target = 6"""
numbers = [2, 3, 4]
target = 6
expected = [1, 3]
actual = two_sum(numbers, target)
self.assertEqual(expected, actual)

def test_3(self):
"""numbers = [-1,0], target = -1"""
numbers = [-1, 0]
target = -1
expected = [1, 2]
actual = two_sum(numbers, target)
self.assertEqual(expected, actual)


if __name__ == '__main__':
unittest.main()

0 comments on commit 1f56e15

Please sign in to comment.