-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.js
More file actions
executable file
·51 lines (37 loc) · 1.21 KB
/
twoSum.js
File metadata and controls
executable file
·51 lines (37 loc) · 1.21 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
// given an array of integers and a target sum, find two numbers that add up to the target and return their indices.
// exampples: nums[2,7,11,15], target = 9; output: [0,1]
// ❌ The Naive Approach: Nested loops checking every pair → O(n²) time complexity This works, but can we do better?
// ✅ The Optimal Solution: Map/Object Magic!
/*
How it works:
Create a Map to store {number: index}
For each number, calculate complement = target - number
Check if complement exists in Map
If yes → return indices
If no → store current number and continue
*/
// using Map
function twoSum_map(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 [];
}
//using object
function twoSum_object(nums, target){
const seen = {};
for(let i = 0; i < nums.length; i++){
const complement = target - nums[i];
if(complement in seen){
return [seen[complement], i];
}
seen[nums[i]] = i;
}
}
console.log(twoSum_map([2,7,11,15], 9))
console.log(twoSum_object([17,9,7,2], 9))