diff --git a/two-sum/nakjun12.ts b/two-sum/nakjun12.ts new file mode 100644 index 000000000..8c1ac2570 --- /dev/null +++ b/two-sum/nakjun12.ts @@ -0,0 +1,20 @@ +/* + * TC: O(n) + * SC: O(n) + * */ +function twoSum(nums: number[], target: number): number[] { + const indices = {}; + + for (let i = 0; i < nums.length; i++) { + const curNum = nums[i]; + const complement = target - curNum; + + if (complement in indices) { + return [indices[complement], i]; + } + + indices[curNum] = i; + } + + return []; +}