Skip to content

Commit

Permalink
solved
Browse files Browse the repository at this point in the history
  • Loading branch information
pmjuu committed Dec 26, 2024
1 parent 857d78f commit f9cabb5
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
33 changes: 33 additions & 0 deletions combination-sum/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import List


class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
result = []

def backtrack(start, target, current_combination):
# ์ข…๋ฃŒ ์กฐ๊ฑด
if target == 0:
result.append(list(current_combination))
return
if target < 0:
return

# ๋ฐฑํŠธ๋ž˜ํ‚น
for i in range(start, len(candidates)):
current_combination.append(candidates[i])
backtrack(i, target - candidates[i], current_combination) # ๊ฐ™์€ ์›์†Œ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์“ธ ์ˆ˜ ์žˆ๋„๋ก i๋ฅผ ๊ทธ๋Œ€๋กœ ๋‘ก๋‹ˆ๋‹ค.
current_combination.pop() # ๋Œ์•„๊ฐ€์„œ ๋‹ค์‹œ ์‹œ๋„ํ•  ์ˆ˜ ์žˆ๋„๋ก ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

backtrack(0, target, [])

return result


# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^t)
# - ํ›„๋ณด ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ ์ˆซ์ž๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, n๊ฐœ์˜ ํ›„๋ณด๋ฅผ ์‚ฌ์šฉํ•ด t๋ฒˆ์˜ ํƒ์ƒ‰์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
# - ๋”ฐ๋ผ์„œ ์ตœ์•…์˜ ๊ฒฝ์šฐ ํƒ์ƒ‰ ํšŸ์ˆ˜๋Š” O(n^t)๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
#
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(t)
# - ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์˜ ๊นŠ์ด๋Š” ์ตœ๋Œ€ target ๊ฐ’์ธ t์— ๋น„๋ก€ํ•˜๋ฏ€๋กœ, ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(t)์ž…๋‹ˆ๋‹ค.
# - ๋˜ํ•œ, ํ˜„์žฌ๊นŒ์ง€ ์„ ํƒ๋œ ์ˆซ์ž๋“ค์˜ ์กฐํ•ฉ์„ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„๋„ ์ตœ๋Œ€ t๊ฐœ๊นŒ์ง€ ์ €์žฅํ•˜๋ฏ€๋กœ, ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(t)์ž…๋‹ˆ๋‹ค.
20 changes: 20 additions & 0 deletions maximum-subarray/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List


class Solution:
def maxSubArray(self, nums: List[int]) -> int:
largest_sum = -float('inf')
current_sum = -float('inf')

for num in nums:
current_sum = max(current_sum + num, num)
largest_sum = max(largest_sum, current_sum)

return largest_sum


# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
# - nums ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ตœ๋Œ€ ๋ถ€๋ถ„ ๋ฐฐ์—ด ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.
#
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
# - ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜๋Š” largest_sum๊ณผ current_sum ๋‘ ๊ฐœ๋ฟ์ด๋ฏ€๋กœ ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(1)์ž…๋‹ˆ๋‹ค.
27 changes: 27 additions & 0 deletions product-of-array-except-self/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import List


class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)
result = [1] * n

prefix = 1
for i in range(n):
result[i] *= prefix
prefix *= nums[i]

suffix = 1
for i in range(-1, -n-1, -1):
result[i] *= suffix
suffix *= nums[i]

return result


# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
# - ์ž…๋ ฅ ๋ฐฐ์—ด nums๋ฅผ ๋‘ ๋ฒˆ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค.
#
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
# - ์ถ”๊ฐ€ ๊ณต๊ฐ„์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜๋Š” prefix์™€ suffix๋ฟ์ด๋ฉฐ,
# ์ถœ๋ ฅ ๋ฐฐ์—ด(result)์€ ์ถ”๊ฐ€ ๊ณต๊ฐ„์œผ๋กœ ๊ณ„์‚ฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
42 changes: 42 additions & 0 deletions reverse-bits/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution:
def reverseBits(self, n: int) -> int:
# ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ '0b' ์ œ๊ฑฐ
binary = bin(n)[2:]
# 32๋น„ํŠธ ๊ธธ์ด์— ๋งž๊ฒŒ ์•ž์ชฝ์— 0์„ ์ฑ„์›€
binary = binary.zfill(32)
# ์ด์ง„์ˆ˜๋ฅผ ๋’ค์ง‘์Œ
reversed_binary = binary[::-1]
# ๋’ค์ง‘ํžŒ ์ด์ง„์ˆ˜๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
return int(reversed_binary, 2)


# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(32)
# - bin(): O(32)
# - zfill(32): O(32)
# - ๋ฌธ์ž์—ด ๋’ค์ง‘๊ธฐ [::-1]: O(32)
# - int(๋ฌธ์ž์—ด, 2): O(32)
# ์ดํ•ฉ: O(32) (์ƒ์ˆ˜ ์‹œ๊ฐ„์œผ๋กœ ๊ฐ„์ฃผ ๊ฐ€๋Šฅ)

# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(32)
# - ์ด์ง„ ๋ฌธ์ž์—ด(binary)์™€ ๋’ค์ง‘ํžŒ ๋ฌธ์ž์—ด(reversed_binary)์„ ์ €์žฅํ•˜๋ฏ€๋กœ O(32).


class Solution:
def reverseBits(self, n: int) -> int:
result = 0

for i in range(32):
# result๋ฅผ ์™ผ์ชฝ์œผ๋กœ 1๋น„ํŠธ ์ด๋™ํ•˜๊ณ  n์˜ ๋งˆ์ง€๋ง‰ ๋น„ํŠธ๋ฅผ ์ถ”๊ฐ€
result = (result << 1) | n & 1
# n์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ 1๋น„ํŠธ ์ด๋™
n >>= 1

return result


# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(32)
# - ๋ฐ˜๋ณต๋ฌธ์ด 32๋ฒˆ ์‹คํ–‰๋˜๋ฉฐ ๊ฐ ์ž‘์—…(๋น„ํŠธ ์ด๋™ ๋ฐ OR ์—ฐ์‚ฐ)์€ O(1).
# ์ดํ•ฉ: O(32) (์ƒ์ˆ˜ ์‹œ๊ฐ„์œผ๋กœ ๊ฐ„์ฃผ ๊ฐ€๋Šฅ)

# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
# - ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜ result์™€ n๋งŒ ์ €์žฅํ•˜๋ฏ€๋กœ ์ƒ์ˆ˜ ๊ณต๊ฐ„.
20 changes: 20 additions & 0 deletions two-sum/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List


class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
subtract_map = {}

for i, num in enumerate(nums):
if num in subtract_map:
return [i, subtract_map[num]]
else:
subtract_map[target - num] = i


# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
# - nums ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ์š”์†Œ๋ฅผ ํ™•์ธํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.
#
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
# - ์ถ”๊ฐ€๋กœ ์‚ฌ์šฉํ•˜๋Š” subtract_map ๋”•์…”๋„ˆ๋ฆฌ์—๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ nums ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ €์žฅ๋˜๋ฏ€๋กœ
# ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.

0 comments on commit f9cabb5

Please sign in to comment.