-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1. TwoSum.py
More file actions
39 lines (29 loc) · 897 Bytes
/
1. TwoSum.py
File metadata and controls
39 lines (29 loc) · 897 Bytes
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
"""
Problem: Two Sum
LeetCode: https://leetcode.com/problems/two-sum/
Time Complexity: O(n)
Space Complexity: O(n)
Why optimal: Uses a hash map to look up the complement in constant time O(1).
"""
#Two Sum
# Idea (Hashing)
# Traverse array
# For current number x, check if target - x already exists
# Store numbers with their indices
def two_sum(nums, target):
index_map = {} # stores number -> index
for i in range(len(nums)):
complement = target - nums[i]
# If complement already seen, we found the answer
if complement in index_map:
return [index_map[complement], i]
# Store current number with index
index_map[nums[i]] = i
return [] # return empty if no solution found
# Example usage:
nums = [2, 7, 11, 15]
target = 9
result = two_sum(nums, target)
print(result) # Output: [0, 1]
# ⏱ Time: O(n)
# 📦 Space: O(n)