์•Œ๊ณ ๋ฆฌ์ฆ˜๐Ÿฅš/๋ฌธ์ œํ’€์ด (Python)

[LeetCode] 1. two_sum (Easy) 2023/4/29

๐Ÿช„ํ•˜๋ฃจ๐Ÿช„ 2023. 4. 29. 08:28
728x90
๋ฌธ์ œ ์ œ๋ชฉ ์ •๋‹ต๋ฅ  ๋‚œ์ด๋„
1. two-sum 49.8% Easy
 

Two Sum - LeetCode

Can you solve this real interview question? Two Sum - Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not

leetcode.com

 

๋ฌธ์ œ์š”์•ฝ

ํƒ€๊นƒ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ฐฐ์—ด ์›์†Œ์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ผ

์กฐ๊ฑด 1) ์ •์ˆ˜ํ˜• ํƒ€๊นƒ ์ˆซ์ž์™€ ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„๋‹ค

์กฐ๊ฑด 2) ๊ฐ™์€ ์›์†Œ๋ฅผ ๋‘ ๋ฒˆ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€ํ•˜๋ฉฐ ๊ฐ ๋ฌธ์ œ์— ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ๋‹ต์ด ์กด์žฌํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ผ

 

 

์ƒ๊ฐํ–ˆ๋˜ ํ’€์ด 1. Brute-Froce(๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ํ•˜๋‚˜์”ฉ ๋Œ€์ž…) -> for ๋ฌธ์žฅ ๋‘ ๊ฐœ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ•˜๊ธฐ - (์‹œ๊ฐ„๋ณต์žก๋„ $O(n^{2})$)

์ƒ๊ฐํ–ˆ๋˜ ํ’€์ด 2. ์ •๋‹ต-์ฒซ๋ฒˆ์งธ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š”์ง€ ํ™•์ธ -> for๊ณผ in์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์ด - (์‹œ๊ฐ„๋ณต์žก๋„ $O(n^{2})$)

์ƒ๊ฐํ–ˆ๋˜ ํ’€์ด 3. ์ •๋‹ต-์ฒซ๋ฒˆ์งธ๊ฐ’์˜ ๋ฆฌ์ŠคํŠธ key ๊ฐ’์„ ์กฐํšŒ - (์‹œ๊ฐ„๋ณต์žก๋„ $O(n)$)

์ƒ๊ฐํ–ˆ๋˜ ํ’€์ด 4. ํˆฌ ํฌ์ธํ„ฐ ์ด์šฉ -> sort() ํ•จ์ˆ˜ ์ด์šฉํ•ด์„œ ์ •๋ ฌํ•œ ๋’ค ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋‚˜ ์ธ๋ฑ์Šค๊ฐ€ ์—‰๋ง์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ถ”์ฒœํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•จ

 

 

ํ’€์ด 2 (567 ms)

์ •๋‹ต-์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š”์ง€ ํ™•์ธ

 

Step1. ์ฒซ๋ฒˆ์งธ ์›์†Œ๊ฐ’์„ ๋บ€ ๋‚˜๋จธ์ง€ ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š”์ง€ ํ™•์ธ -> for, in ํ•จ์ˆ˜

- (์‹œ๊ฐ„๋ณต์žก๋„ $O(n^2)$)

 

๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค์™€ ๊ฐ’์— ๋ชจ๋‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜  enumerate(list)

 

 

Setp2. ํ•ด๋‹น ๊ฐ’์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜

 

๋ฆฌ์ŠคํŠธ์— ํ•ด๋‹น ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜ list.index(value)
์ค‘๋ณต๋˜๋Š” ์›์†Œ๋“ค์˜ ์ธ๋ฑ์Šค ๋ชจ๋‘ ์ฐพ๋Š” ํ•จ์ˆ˜ numpy_array=numpy.array(list)
numpy.where(numpy_array==๊ฐ’)[0]

 

 

๊ตฌํ˜„ 

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for first_index, first_num in enumerate(nums):
            second_num=target-first_num
            if(second_num in nums[first_index+1:]):
                return [first_index, nums[first_index+1:].index(second_num)+(first_index+1)]

 

 

ํ’€์ด3 (82ms)

์ •๋‹ต-์ฒซ ๋ฒˆ์งธ ๊ฐ’์˜ ๋ฆฌ์ŠคํŠธ key ๊ฐ’์„ ์กฐํšŒ -> ๋”•์…”๋„ˆ๋ฆฌ ์ด์šฉ

 

Step1. ์ฒซ๋ฒˆ์งธ ์›์†Œ๊ฐ’์„ ๋บ€ ๋‚˜๋จธ์ง€ ๊ฐ’์ด ๋”•์…”๋„ˆ๋ฆฌ์— ์žˆ๋Š”์ง€ ํ™•์ธ -> for, dict[] ํ•จ์ˆ˜ ์ด์šฉ

- (์‹œ๊ฐ„๋ณต์žก๋„ $O(n)$)

 

 

Setp2. ํ•ด๋‹น ๊ฐ’(๋”•์…”๋„ˆ๋ฆฌ์˜ key)์˜ ์ธ๋ฑ์Šค(๋”•์…”๋„ˆ๋ฆฌ์˜ value) ๋ฐ˜ํ™˜

 

 

๊ตฌํ˜„

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        nums_dict={}
        for index, num in enumerate(nums):
            nums_dict[num]=index
        for first_index, first_num in enumerate(nums):
            second_num=target-first_num
            if(second_num in nums_dict and first_index!=nums_dict[second_num]):
                return [first_index, nums_dict[second_num]]
728x90