🧮

Two Sum

Two Sum
보통 알고리즘 +20pts

Problem

Given an array of numbers and a target, return the indices of two numbers that add up to the target.

Examples

Input: twoSum([2, 7, 11, 15], 9)
Output: [0, 1]
💡 nums[0] + nums[1] = 2 + 7 = 9

Explanation

이 문제는 **해시맵(Map)**을 활용한 최적화 기법을 학습합니다. LeetCode의 대표적인 문제로 코딩 인터뷰에 자주 등장합니다. **브루트 포스 접근 (O(n²))** 이중 for문으로 모든 쌍을 확인하는 방법은 간단하지만 비효율적입니다. **해시맵 최적화 (O(n))** 핵심 아이디어: "target - 현재값 = 보수(complement)" 각 숫자를 볼 때, 그 숫자의 보수가 이미 Map에 있는지 확인합니다. **[2, 7, 11, 15], target=9 처리 과정** - i=0, nums[0]=2: 보수=7, ...

View detailed explanation →

Key Concepts

해시맵(Map) 보수(complement) 개념 시간복잡도 최적화 한 번의 순회
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.