Published:
Updated:


Solution (Brute Force)

from typing import List


class Solution:
    # dict() ๋ฐฉ์‹
    # time: O(N)
    # Runtime: 59ms
    # Beats: 86.75%
    # ์ž๋ฐ” Map์œผ๋กœ ํ’€์—ˆ์„ ๋• ์ง„์งœ ๋ถˆํŽธํ–ˆ๋Š”๋ฐ ์—ญ์‹œ ํŒŒ์ด์ฌ
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        answer = []

        # ํ•œ๋ฒˆ ์ž˜ ์ƒ๊ฐํ•ด ๋ณด์ž
        # ์œ„ ์™„์ „ ํƒ์ƒ‰ ๋ฐฉ์‹์ฒ˜๋Ÿผ ๊ผญ ๋‘ ๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ๋น„๊ตํ•  ํ•„์š”๊ฐ€ ์—†๊ณ 
        # target์ด ์ด๋ฏธ ์ƒ์ˆ˜๊ฐ’์œผ๋กœ ์ฃผ์–ด์ ธ ์žˆ์œผ๋‹ˆ,
        # target์—์„œ nums์˜ ์š”์†Œ๋ฅผ ๋นผ์ฃผ๋ฉด์„œ ๋น„๊ต๋ฅผ ํ•˜๋ฉด ๋จ
        # ๊ทธ ์ฐจ์ด์˜ ๊ฐ’์ด ๋‹ค๋ฅธ ์š”์†Œ์— ์žˆ์œผ๋ฉด
        # ๋น„๊ต๋ฅผ ํ•œ ์ž๋ฆฌ์™€ ๋‹ค๋ฅธ ์š”์†Œ์˜ ์ž๋ฆฌ๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋จ
        # ์ค‘์š”! ์ด๋•Œ์˜ key์™€ value๋Š” ๊ฐ๊ฐ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•˜์ž
        # 0, 1, 2, 3์˜ index๊ฐ€ value์— ๊ฐ€๋„๋ก ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐ
        nums_dict = {}

        # key์™€ value ํ•œ๊บผ๋ฒˆ์— ๋ฝ‘์•„์˜ค๋Š” ๋ฐฉ๋ฒ•
        # list์—์„œ ๋ฝ‘์•„์˜ค๋Š” ๊ฑฐ๋‹ˆ key๋Š” ๊ทธ index๊ฐ€ ๋  ๊ฒƒ์ด๊ณ 
        for index, value in enumerate(nums):
            # target์—์„œ nums์˜ ์š”์†Œ๋ฅผ ๋นผ์ฃผ๋ฉด์„œ ๋น„๊ต
            difference = target - value

            # Only one valid answer exists. < ์ด ์ œํ•œ์ด ์žˆ์œผ๋‹ˆ
            # ๊ฐ™์€ index๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๊ฒŒ dict ์ฐจ์ด์ ์˜ value๊ฐ€ key๊ฐ’์ด๋ž‘ ๊ฐ™์ง€ ์•Š๋„๋ก
            # ์ฐจ์ด๊ฐ€ dict ์•ˆ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
            if difference in nums_dict:
                # ์กด์žฌํ•œ๋‹ค๋ฉด index์ธ key๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฐฐ์—ด์— ์ €์žฅ
                # difference key๊ฐ’์ธ value๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅ
                answer = [index, nums_dict[difference]]
            else:
                # ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด dict ์„ ์–ธํ•  ๋•Œ ๋งํ•  ๋Œ€๋กœ
                # index์™€ value๋ฅผ ๋ฐ˜๋Œ€๋กœ ์„ค์ •ํ•ด์„œ ๊ณ„์† ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ
                nums_dict[value] = index

        # ์ด ๋ฌธ์ œ์—์„œ value์™€ index๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•œ ์ด์œ ๋Š”
        # dict์—์„œ key๋ฅผ ๊ฐ€์ง€๊ณ  value๋ฅผ ์ฐพ๋Š” ๊ฑด ์‰ฝ์ง€๋งŒ,
        # value๋ฅผ ๊ฐ€์ง€๊ณ  key๋ฅผ ์ฐพ๋Š” ๊ฑด ๋ณต์žกํ•˜๊ธฐ ๋•Œ๋ฌธ
        return answer

Solution (Hash Table)

from typing import List


class Solution:
    # dict() ๋ฐฉ์‹
    # time: O(N)
    # Runtime: 59ms
    # Beats: 86.75%
    # ์ž๋ฐ” Map์œผ๋กœ ํ’€์—ˆ์„ ๋• ์ง„์งœ ๋ถˆํŽธํ–ˆ๋Š”๋ฐ ์—ญ์‹œ ํŒŒ์ด์ฌ
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        answer = []

        # ํ•œ๋ฒˆ ์ž˜ ์ƒ๊ฐํ•ด ๋ณด์ž
        # ์œ„ ์™„์ „ ํƒ์ƒ‰ ๋ฐฉ์‹์ฒ˜๋Ÿผ ๊ผญ ๋‘ ๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ๋น„๊ตํ•  ํ•„์š”๊ฐ€ ์—†๊ณ 
        # target์ด ์ด๋ฏธ ์ƒ์ˆ˜๊ฐ’์œผ๋กœ ์ฃผ์–ด์ ธ ์žˆ์œผ๋‹ˆ,
        # target์—์„œ nums์˜ ์š”์†Œ๋ฅผ ๋นผ์ฃผ๋ฉด์„œ ๋น„๊ต๋ฅผ ํ•˜๋ฉด ๋จ
        # ๊ทธ ์ฐจ์ด์˜ ๊ฐ’์ด ๋‹ค๋ฅธ ์š”์†Œ์— ์žˆ์œผ๋ฉด
        # ๋น„๊ต๋ฅผ ํ•œ ์ž๋ฆฌ์™€ ๋‹ค๋ฅธ ์š”์†Œ์˜ ์ž๋ฆฌ๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋จ
        # ์ค‘์š”! ์ด๋•Œ์˜ key์™€ value๋Š” ๊ฐ๊ฐ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•˜์ž
        # 0, 1, 2, 3์˜ index๊ฐ€ value์— ๊ฐ€๋„๋ก ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐ
        nums_dict = {}

        # key์™€ value ํ•œ๊บผ๋ฒˆ์— ๋ฝ‘์•„์˜ค๋Š” ๋ฐฉ๋ฒ•
        # list์—์„œ ๋ฝ‘์•„์˜ค๋Š” ๊ฑฐ๋‹ˆ key๋Š” ๊ทธ index๊ฐ€ ๋  ๊ฒƒ์ด๊ณ 
        for index, value in enumerate(nums):
            # target์—์„œ nums์˜ ์š”์†Œ๋ฅผ ๋นผ์ฃผ๋ฉด์„œ ๋น„๊ต
            difference = target - value

            # Only one valid answer exists. < ์ด ์ œํ•œ์ด ์žˆ์œผ๋‹ˆ
            # ๊ฐ™์€ index๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๊ฒŒ dict ์ฐจ์ด์ ์˜ value๊ฐ€ key๊ฐ’์ด๋ž‘ ๊ฐ™์ง€ ์•Š๋„๋ก
            # ์ฐจ์ด๊ฐ€ dict ์•ˆ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
            if difference in nums_dict:
                # ์กด์žฌํ•œ๋‹ค๋ฉด index์ธ key๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฐฐ์—ด์— ์ €์žฅ
                # difference key๊ฐ’์ธ value๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅ
                answer = [index, nums_dict[difference]]
            else:
                # ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด dict ์„ ์–ธํ•  ๋•Œ ๋งํ•  ๋Œ€๋กœ
                # index์™€ value๋ฅผ ๋ฐ˜๋Œ€๋กœ ์„ค์ •ํ•ด์„œ ๊ณ„์† ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ
                nums_dict[value] = index

        # ์ด ๋ฌธ์ œ์—์„œ value์™€ index๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•œ ์ด์œ ๋Š”
        # dict์—์„œ key๋ฅผ ๊ฐ€์ง€๊ณ  value๋ฅผ ์ฐพ๋Š” ๊ฑด ์‰ฝ์ง€๋งŒ,
        # value๋ฅผ ๊ฐ€์ง€๊ณ  key๋ฅผ ์ฐพ๋Š” ๊ฑด ๋ณต์žกํ•˜๊ธฐ ๋•Œ๋ฌธ
        return answer

Leave a comment