-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo-sum.js
More file actions
74 lines (56 loc) · 1.48 KB
/
two-sum.js
File metadata and controls
74 lines (56 loc) · 1.48 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Problem: Two Sum
* Link: https://leetcode.com/problems/two-sum/
* Difficulty: Easy
*
* Given an array of integers nums and an integer target, return indices of the
* two numbers such that they add up to target.
*
* Example:
* Input: nums = [2,7,11,15], target = 9
* Output: [0,1]
*
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
// JavaScript Solution - HashMap
function twoSum(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
return [];
}
// Test cases
console.log(twoSum([2, 7, 11, 15], 9)); // [0,1]
console.log(twoSum([3, 2, 4], 6)); // [1,2]
console.log(twoSum([3, 3], 6)); // [0,1]
module.exports = twoSum;
/* Python Solution (commented):
def two_sum(nums: list[int], target: int) -> list[int]:
"""
Find two numbers that add up to target
Args:
nums: Array of integers
target: Target sum
Returns:
Indices of two numbers
Time Complexity: O(n)
Space Complexity: O(n)
"""
num_map = {}
for i, num in enumerate(nums):
complement = target - num
if complement in num_map:
return [num_map[complement], i]
num_map[num] = i
return []
# Test cases
print(two_sum([2,7,11,15], 9)) # [0, 1]
print(two_sum([3,2,4], 6)) # [1, 2]
print(two_sum([3,3], 6)) # [0, 1]
*/