๊ฐœ๋ฐœ/๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

[leetcode] 167. Two Sum II - Input Array Is Sorted ํ’€์ด, ํ•ด์„ค (python)

ttoance 2023. 10. 24. 20:52

๋ฌธ์ œ ๋งํฌ 

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

 

Two Sum II - Input Array Is Sorted - LeetCode

Can you solve this real interview question? Two Sum II - Input Array Is Sorted - Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two n

leetcode.com

 

 

1์ฐจ ํ’€์ด 

๋‹จ์ˆœํ•˜๊ฒŒ ๋ชจ๋“  ์กฐํ•ฉ์„ ๋น„๊ตํ•˜๋ฉด O(n*n)์ด ๊ฑธ๋ฆฌ์ง€๋งŒ, ๋ฌธ์ œ๋Š” ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™์•˜๋‹ค. 

์ œ์•ฝ์กฐ๊ฑด์ƒ n์ด 30000 ๊ฐœ์ด๋ฏ€๋กœ ์ตœ๋Œ€ 30000 * 30000 ํ•ด์„œ ๊ทธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์ ‘๊ทผํ•˜๋ฉด ์•ˆ๋  ๊ฒƒ ๊ฐ™์•„์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ–ˆ๋‹ค. 

 

๊ทธ๋‹ค์Œ ์ƒ๊ฐํ–ˆ๋˜ ๋ฐฉ์‹์€, target๊ณผ ํ˜„์žฌ ๊ฐ’์„ ๋น„๊ตํ•ด์„œ, binary search๋ฅผ ํ™œ์šฉํ•ด์„œ ์ค‘๊ฐ„๊ฐ’๋ถ€ํ„ฐ ํ•ด์„œ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•ด๋ดค๋Š”๋ฐ, 

[-90, 1, 10, 100] ์ด ์žˆ์„๋•Œ target=10์ด๋ผ๊ณ  ํ–ˆ์„๋•Œ ๋‹ต์€ [100,-90]์ด๊ธฐ ๋•Œ๋ฌธ์— binary search๋กœ target๊ณผ ๊ทผ์‚ฌ๊ฐ’ 10์„ ์ฐพ๋”๋ผ๋„ ํ•ด๊ฒฐ์ฑ…์ด ์•„๋‹ ์ˆ˜๋„ ์žˆ์„ ๊ฑฐ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. 

 

๊ทธ๋ž˜์„œ ๊ฐ ๋ฐฐ์—ด์ด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด์žˆ๋Š” ๊ฒƒ์„ ํžŒํŠธ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. 

์ฃผ์–ด์ง„ ์˜ˆ์‹œ, [2,7,11,15]์—์„œ ํ•ฉ 18์„ ๊ตฌํ•œ๋‹ค๊ณ  ํ–ˆ์„๋•Œ, 

- 15 ์„ ํƒ, 15 + 2 = 17 < 18 ๋‹ค์Œ๊ฑฐ ๊ณ„์‚ฐ, 15 + 7 = 22 ๋ฉˆ์ถค

- 11 ์„ ํƒ, 11 + 7 = 18 ์ข…๋ฃŒ ์ด๋Ÿฐ์‹์œผ๋กœ ๋œ๋‹ค.

์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— target๊ณผ ๋น„๊ตํ•ด์„œ ๋„˜์–ด๊ฐˆ ๊ฒฝ์šฐ, ๋”์ด์ƒ ๋‹ค์Œ ๊ฐ’์„ ๋ณผ ํ•„์š”๊ฐ€ ์—†๋‹ค. 

- ๋ฐ˜๋Œ€๋กœ 2์„ ํƒ, 2 + 7 = 9, 2 + 11 = 13, 2 + 15 = 17 target์„ ์ฐพ์„๋•Œ๊นŒ์ง€ ๊ณ„์† ๋ด์•ผํ•˜๊ณ  

- 7 ์„ ํƒ, 7 + 11 = 18 ์ข…๋ฃŒ  ์ด๋Ÿฐ์‹์œผ๋กœ ๋œ๋‹ค. 

 

 

์ด ๊ฐœ๋…์„ ๊ฐ€์ง€๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์งœ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

class Solution(object):
    def twoSum(self, numbers, target):
        """
        :type numbers: List[int]
        :type target: int
        :rtype: List[int]
        """
        
        l, r = 0, len(numbers) - 1
        while l < r:
            if (numbers[l] + numbers[r]) == target : 
                break;
            elif (numbers[l] + numbers[r]) > target :
                r = r -1 
            elif (numbers[l] + numbers[r]) < target :
                l = l + 1 
        
        return [l + 1, r + 1]

 

 

ํ•ด์„ค 

https://www.youtube.com/watch?v=cQ1Oz4ckceM

์œ„์— ๋ฌธ์ œ ํ’€์—ˆ์„๋•Œ, ์ƒ๊ฐํ–ˆ๋˜ ๊ฐœ๋…์„ ํ•ด์„ค์—์„œ ์กฐ๊ธˆ ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด์„œ ๊ฐ€์ ธ์˜ค์ž๋ฉด, 

[1,3,4,5,7,11]์ด ์žˆ๊ณ  target=9์ธ ๊ฒฝ์šฐ, 

๋จผ์ € 1 + 11 = 13 < 9 ์ธ๊ฒฝ์šฐ ํฌ์ธํ„ฐ๋กœ ํ•ฉ์„ ์ค„์ผ๋ ค๋ฉด ์˜ค๋ฅธ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ์•ˆ์ชฝ์œผ๋กœ ํ•œ ์นธ ๋•ก๊ฒจ์•ผ ํ•œ๋‹ค. 

๊ทธ๋Ÿฌ๋ฉด 1 + 7 = 8 > 9 ๋กœ ์˜คํžˆ๋ ค ๊ฐ’์ด target๋ณด๋‹ค ์ค„์–ด๋“ค์–ด์„œ, ์ด์ œ๋Š” ์™ผ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ๋Š˜๋ ค์•ผ ํ•œ๋‹ค. 

 

์ด๋Ÿฐ์‹์œผ๋กœ ์ƒ๊ฐํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. 

๋ฐ˜์‘ํ˜•