๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

๋ฌธ์ œ๊ฐ€ ์ „์ฒด์ ์œผ๋กœ ์–ด๋ ค์›Œ์„œ ํ’€์ง€ ๋ชปํ–ˆ๋‹ค. ์ผ๋‹จ ๋‹ค์Œ 8์žฅ์œผ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ๋ฐฐ์—ด๊ณผ ๊ด€๋ จ๋œ ๋ฌธ์ œ๋“ค์„ ํ‘ธ๋Š” ๋ฐ ์‹œ๊ฐ„์„ ์Ÿ์•„์•ผ๊ฒ ๋‹ค.

๋ฐฐ์—ด์€ ๊ฐ’ ๋˜๋Š” ๋ณ€์ˆ˜ ์—˜๋ฆฌ๋จผํŠธ์˜ ์ง‘ํ•ฉ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ตฌ์กฐ๋กœ, ํ•˜๋‚˜ ์ด์ƒ์˜ ์ธ๋ฑ์Šค ๋˜๋Š” ํ‚ค๋กœ ์‹๋ณ„๋œ๋‹ค.

๋‘ ์ˆ˜์˜ ํ•ฉ

๋ธŒ๋ฃจํŠธ ํฌ์Šค ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ๋‹ค. ๋น„์Šทํ•˜๊ฒŒ ํ’€์–ด์„œ ๋”ฐ๋กœ ์ ์ง€๋Š” ์•Š์•˜์Œ

ํ’€์ด1. ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ๊ณ„์‚ฐ

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

def twoSum(self, nums: List[int], target: int) -> List[int]:
    for i in range(len(nums)):
        for j in range(i+1, len(nums)):
            if nums[i] + nums[j] == target:
                return [i, j]

ํ’€์ด2. in์„ ์ด์šฉํ•œ ํƒ์ƒ‰

๋ชจ๋“  ์กฐํ•ฉ์„ ๋น„๊ตํ•˜์ง€ ์•Š๊ณ  ํƒ€๊ฒŸ์—์„œ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋บ€ ๊ฐ’ tartget - n ์ด ์กด์žฌํ•˜๋Š”์ง€ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ์Œ
์‹œ๊ฐ„ ๋ณต์žก๋„ → O(n^2)์ด์ง€๋งŒ ๊ฐ™์€ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ผ๋„ in ์—ฐ์‚ฐ ์ชฝ์ด ํ›จ์”ฌ ๋” ๊ฐ€๋ณ๊ณ  ๋น ๋ฆ„

def twoSum(self, nums: List[int], target: int) -> List[int]:
    for i, n in enumerate(nums):
        complement = target - n
        if complement in nums[i + 1:]:
            return nums.index(n), nums[i+1:].index(complement) + (i+1)

ํ’€์ด3. ์ฒซ ๋ฒˆ์งธ ์ˆ˜๋ฅผ ๋บ€ ๊ฒฐ๊ณผ ํ‚ค ์กฐํšŒ

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

def twoSum(self, nums: List[int], target: int) -> List[int]:
    nums_map = {}
    # ํ‚ค์™€ ๊ฐ’์„ ๋ฐ”๊ฟ”์„œ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ €์žฅ
    for i, num in enumerate(nums):
        nums_map[num] = i
    # ํƒ€๊ฒŸ์—์„œ ์ฒซ ๋ฒˆ์งธ ์ˆ˜๋ฅผ ๋บ€ ๊ฒฐ๊ณผ๋ฅผ ํ‚ค๋กœ ์กฐํšŒ
    for i, num in enumerate(nums):
        if target - num in nums_map and i != nums_map[target - num]:
            return nums.index(num), nums_map[tartget - num]

ํ’€์ด4. ์กฐํšŒ ๊ตฌ์กฐ ๊ฐœ์„ 

์ „์ฒด๋ฅผ ๋ชจ๋‘ ์ €์žฅํ•  ํ•„์š” ์—†์ด ์ •๋‹ต์„ ์ฐพ๊ฒŒ ๋˜๋ฉด ํ•จ์ˆ˜๋ฅผ ๋ฐ”๋กœ ๋น ์ ธ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Œ. ๊ทธ๋Ÿฌ๋‚˜ ๋‘ ๋ฒˆ์งธ ๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์–ด์ฐจํ”ผ ๋งค๋ฒˆ ๋น„๊ตํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ž์„œ ํ’€์ด์— ๋น„ํ•ด ์„ฑ๋Šฅ์ƒ์˜ ํฐ ์ด์ ์€ ์—†์„ ๊ฑฐ ๊ฐ™์Œ
์‹œ๊ฐ„ ๋ณต์žก๋„ → O(n)

def twoSum(self, nums: List[int], target: int) -> List[int]:
    nums_map = {}
    # ํ•˜๋‚˜์˜ for ๋ฌธ์œผ๋กœ ํ†ตํ•ฉ
    for i, num in enumerate(nums):
        if target - num in nums_map:
            return [nums_map[target-num], i]
        nums_map[num] = i

ํ’€์ด5. ํˆฌ ํฌ์ธํ„ฐ ์ด์šฉ

ํˆฌ ํฌ์ธํ„ฐ๋ž€ ์™ผ์ชฝ ํฌ์ธํ„ฐ์™€ ์˜ค๋ฅธ์ชฝ ํฌ์ธํ„ฐ์˜ ํ•ฉ์ด ํƒ€๊ฒŸ๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ, ์ž‘๋‹ค๋ฉด ์™ผ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด์„œ ๊ฐ’์„ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ์‹

ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€ ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ

ํ’€์ด6. ๊ณ (Go) ๊ตฌํ˜„

์„ฑ๋Šฅ ๊ฐœ์„ ์˜ ํšจ๊ณผ๊ฐ€ ํฌ์ง€๋งŒ, ํ•ด๋‹น ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ค„ ๋ชฐ๋ผ์„œ ์ ์ง€ ์•Š์•˜์Œ

 

๋น—๋ฌผ ํŠธ๋ž˜ํ•‘

๋น—๋ฌผ ํŠธ๋ž˜ํ•‘๊ณผ ๋น„์Šทํ•œ ์œ ํ˜•์˜ ๋ฌธ์ œ(์‡ ๋ง‰๋Œ€๊ธฐ)๋ฅผ ์ž˜ ๋ชป ํ‘ธ๋Š” ํŽธ์ด๋ผ์„œ... ์ผ๋‹จ ์ด๋ฒˆ์—๋„ ํ’€์ด๋ฅผ ๋จธ๋ฆฌ์— ๋‹ด์•„๋‘๋Š” ๊ฑธ๋กœ ๋„˜์–ด๊ฐ”๋‹ค.

ํ’€์ด1. ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ์ตœ๋Œ€๋กœ ์ด๋™

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

def trap(self, height: List[int]) -> int:
    if not height:
        return 0

    volume = 0
    left, right = 0, len(height) - 1
    left_max, right_max = height[left], height[right]

    while left < right:
        left_max, right_max = max(height[left], left_max),
                              max(height[right], right_max)
        # ๋” ๋†’์€ ์ชฝ์„ ํ–ฅํ•ด ํˆฌ ํฌ์ธํ„ฐ ์ด๋™
        if left_max <= right_max:
            volume += left_max - height[left]
            left += 1
        else:
            volume += right_max - height[right]
            right -= 1
    return volume

ํ’€์ด2. ์Šคํƒ ์Œ“๊ธฐ

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

def trap(self, height: List[int]) -> int:
    stack = []
    volume = 0

    for i in range(len(height)):
        # ๋ณ€๊ณก์ ์„ ๋งŒ๋‚˜๋Š” ๊ฒฝ์šฐ
        while stack and height[i] > height[stack[-1]]:
            # ์Šคํƒ์—์„œ ๊บผ๋ƒ„
            top = stack.pop()

            if not len(stack):
                break

            # ์ด์ „๊ณผ์˜ ์ฐจ์ด๋งŒํผ ๋ฌผ ๋†’์ด ์ฒ˜๋ฆฌ
            distance = i - stack[-1] - 1
            waters = min(height[i], height[stack[-1]]) - height[top]

            volume += distance * waters
        stack.append(i)
    return volume

 

์„ธ ์ˆ˜์˜ ํ•ฉ

์ด ๋ฌธ์ œ๋Š” ๋ด๋„ ์Œ.. ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๋‹ค์Œ์— ํ•œ ๋ฒˆ ๋” ๋ณด๊ธฐ

ํ’€์ด1. ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ๊ณ„์‚ฐ

์‹œ๊ฐ„ ๋ณต์žก๋„ → O(n^3)

def threeSum(self, nums: List[int]) -> List[List[int]]:
    results = []
    nums.sort()

    # ๋ธŒ๋ฃจํŠธ ํฌ์Šค n^3 ๋ฐ˜๋ณต
    for i in range(len(nums) - 2):
        # ์ค‘๋ณต๋œ ๊ฐ’ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
        if i > 0 and nums[i] == nums[i - 1]:
            continue
        for j in range(i+1, len(nums)-1):
            if j > i + 1 and nums[j] == nums[j-1]:
                continue
            for k in range(j+1, len(nums)):
                if k > j + 1 and nums[k] == nums[k-1]:
                    continue
                if nums[i] + nums[j] + nums[k] == 0:
                    results.append((nums[i], nums[j], nums[k]))
    return results

ํ’€์ด2. ํˆฌ ํฌ์ธํ„ฐ๋กœ ํ•ฉ ๊ณ„์‚ฐ

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

def threeSum(self, nums: List[int]) -> List[List[int]]:
    results = []
    nums.sort()

    for i in range(len(nums) - 2):
        # ์ค‘๋ณต๋œ ๊ฐ’ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
        if i > 0 and nums[i] == nums[i - 1]:
            continue

        # ๊ฐ„๊ฒฉ์„ ์ขํ˜€๊ฐ€๋ฉฐ ํ•ฉ sum ๊ณ„์‚ฐ
        left, right = i+1, len(nums) - 1
        while left < right:
            sum = nums[i] + nums[left] + nums[right]
            if sum < 0:
                left += 1
            elif sum > 0:
                right -= 1
            else:
                # sum = 0์ธ ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ์ •๋‹ต ๋ฐ ์Šคํ‚ต ์ฒ˜๋ฆฌ
                results.append((nums[i], nums[left], nums[right]))

                while left < right and nums[left] == nums[left + 1]:
                    left += 1
                while left < right and nums[right] == nums[right - 1]:
                    right -= 1
                left += 1
                right -= 1

    return results

 

๋ฐฐ์—ด ํŒŒํ‹ฐ์…˜ I

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

def arrayPairSum(self, nums: List[int]) -> int:
    nums.sort()
    sum = 0

    for i in range(len(nums)):
        if i % 2 == 0:
            sum += nums[i]
    return sum

ํ’€์ด1. ์˜ค๋ฆ„์ฐจ์ˆœ ํ’€์ด

def arrayPairSum(self, nums: List[int]) -> int:
    sum = 0
    pair = []
    nums.sort()

    for n in nums:
        # ์•ž์—์„œ๋ถ€ํ„ฐ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ํŽ˜์–ด๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•ฉ ๊ณ„์‚ฐ
        pair.append(n)
        if len(pair) == 2:
            sum += min(pair)
            pair = []
    return sum

ํ’€์ด2. ์ง์ˆ˜ ๋ฒˆ์งธ ๊ฐ’ ๊ณ„์‚ฐ

def arrayPairSum(self, nums: List[int]) -> int:
    sum = 0
    nums.sort()

    for i, n in enumerate(nums):
        if i % 2 == 0:
            sum += n
    return sum

ํ’€์ด3. ํŒŒ์ด์ฌ๋‹ค์šด ๋ฐฉ์‹

def arrayPairSum(self, nums: List[int]) -> int:
    return sum(sorted(nums)[::2])

 

์ž์‹ ์„ ์ œ์™ธํ•œ ๋ฐฐ์—ด์˜ ๊ณฑ

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

์ด ์ฝ”๋“œ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๋กœ ์‹คํŒจ๊ฐ€ ๋œฌ๋‹ค. ๋“ค์–ด์˜ค๋Š” ๊ฐ’์ด

[-1,-1,1,-1,-1,1,-1,-1,-1,1,1,-1,1,1,1,1,-1,1,1,-1,-1,1,-1,-1,-1,1,1,-1,-1,-1,-1,-1,1,1,1,1,1,1,-1,-1,1,1,-1,-1,1,-1,1,1,1,-1,1,-1,-1,1,-1,-1,1...

์ด ์ •๋„๋กœ ๊ธธ ๋•Œ ์‹คํŒจํ–ˆ๋‹ค. ๐Ÿ˜ข

def productExceptSelf(self, nums: List[int]) -> List[int]:
    output = []

    for i, n in enumerate(nums):
        output.append(reduce((lambda x, y: x * y), nums[:i] + nums[i+1:]))
    return output

ํ’€์ด1. ์™ผ์ชฝ ๊ณฑ์…ˆ ๊ฒฐ๊ณผ์— ์˜ค๋ฅธ์ชฝ ๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๊ณฑ์…ˆ

def productExceptSelf(self, nums: List[int]) -> List[int]:
    out = []
    p = 1
    # ์™ผ์ชฝ ๊ณฑ์…ˆ
    for i in range(0, len(nums)):
        out.append(p)
        p = p * nums[i]
    p = 1
    # ์™ผ์ชฝ ๊ณฑ์…ˆ ๊ฒฐ๊ณผ์— ์˜ค๋ฅธ์ชฝ ๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๊ณฑ์…ˆ
    for i in range(len(nums)-1, 0 - 1, -1):
        out[i] = out[i] * p
        p = p * nums[i]
    return out

 

์ฃผ์‹์„ ์‚ฌ๊ณ ํŒ”๊ธฐ ๊ฐ€์žฅ ์ข‹์€ ์‹œ์ 

ํ’€์ด1. ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ๊ณ„์‚ฐ

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

def maxProfit(self, prices: List[int]) -> int:
    max_price = 0

    for i, price in enumerate(prices):
        for j in range(i, len(prices))):
            max_price = max(prices[j] - price, max_price)
    return max_price

ํ’€์ด2. ์ €์ ๊ณผ ํ˜„์žฌ ๊ฐ’๊ณผ์˜ ์ฐจ์ด ๊ณ„์‚ฐ

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

def maxProfit(self, prices: List[int]) -> int:
    profit = 0
    min_price = sys.maxsize

    # ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ๊ณ„์† ๊ฐฑ์‹ 
    for price in prices:
        min_price = min(min_price, price)
        profit = max(profit, price - min_price)

    return profit
๋ฐ˜์‘ํ˜•