Skip to content

Commit

Permalink
two sum solution
Browse files Browse the repository at this point in the history
  • Loading branch information
yoon-turtle committed Jan 2, 2025
1 parent e2b6507 commit 0e1b8fa
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions two-sum/yoonthecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var twoSum = function (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);
}
};

// Time complexity: O(n)
// Space complexity: O(n) - hash map storage

0 comments on commit 0e1b8fa

Please sign in to comment.