Skip to content

Commit

Permalink
Merge pull request #766 from KwonNayeon/main
Browse files Browse the repository at this point in the history
[KwonNayeon] Week 3
  • Loading branch information
KwonNayeon authored Dec 28, 2024
2 parents a7f0f96 + 15eca17 commit 439086a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
38 changes: 38 additions & 0 deletions product-of-array-except-self/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Constraints:
1. 2 <= nums.length <= 10^5
2. -30 <= nums[i] <= 30
3. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer
Time Complexity: O(n)
- ๋ฐฐ์—ด์„ ๋‘ ๋ฒˆ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(n)
Space Complexity: O(1)
- ์ถœ๋ ฅ ๋ฐฐ์—ด(answer)์„ ์ œ์™ธํ•˜๋ฉด ์ถ”๊ฐ€ ๊ณต๊ฐ„์ด ์ƒ์ˆ˜๋งŒํผ๋งŒ ํ•„์š”(left, right ๋ณ€์ˆ˜)
ํ’€์ด ๋ฐฉ๋ฒ•:
1. answer ๋ฐฐ์—ด์„ 1๋กœ ์ดˆ๊ธฐํ™” (๊ณฑ์…ˆ์—์„œ๋Š” 1์ด ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Œ)
2. ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ˆœํšŒ:
- answer[i]์— ํ˜„์žฌ๊นŒ์ง€์˜ left ๋ˆ„์ ๊ฐ’์„ ๊ณฑํ•จ
- left *= nums[i]๋กœ ๋‹ค์Œ์„ ์œ„ํ•ด left ๊ฐ’์„ ์—…๋ฐ์ดํŠธ
3. ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ˆœํšŒ (range(n-1, -1, -1) ์‚ฌ์šฉ):
- answer[i]์— ํ˜„์žฌ๊นŒ์ง€์˜ right ๋ˆ„์ ๊ฐ’์„ ๊ณฑํ•จ
- right *= nums[i]๋กœ ๋‹ค์Œ์„ ์œ„ํ•ด right ๊ฐ’์„ ์—…๋ฐ์ดํŠธ
"""

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

left = 1
for i in range(n):
answer[i] *= left
left *= nums[i]

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

return answer
24 changes: 24 additions & 0 deletions reverse-bits/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Constraints:
- The input must be a binary string of length 32
Time Complexity: O(1)
- ํ•ญ์ƒ ๊ณ ์ •๋œ 32๋น„ํŠธ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ์—ฐ์‚ฐํ•˜๋ฏ€๋กœ ์ƒ์ˆ˜ ์‹œ๊ฐ„
Space Complexity: O(1)
- 32๋น„ํŠธ ๊ณ ์ • ํฌ๊ธฐ์˜ ๋ฌธ์ž์—ด ์—ฐ์‚ฐ๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ƒ์ˆ˜ ๊ณต๊ฐ„
ํ’€์ด ๋ฐฉ๋ฒ•:
1. format(n, '032b')๋ฅผ ์‚ฌ์šฉํ•ด ์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜๋ฅผ 32๋น„ํŠธ ์ด์ง„์ˆ˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•จ
2. ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ [::-1]์œผ๋กœ ๋น„ํŠธ๋ฅผ ๋’ค์ง‘์Œ
3. int(reversed_binary, 2)๋กœ ๋’ค์ง‘์€ ์ด์ง„์ˆ˜ ๋ฌธ์ž์—ด์„ ๋‹ค์‹œ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•จ
"""

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

binary = format(n, '032b')

reversed_binary = binary[::-1]

return int(reversed_binary, 2)
25 changes: 25 additions & 0 deletions two-sum/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Constraints:
- 2 <= nums.length <= 10^4
- -10^9 <= nums[i] <= 10^9
- -10^9 <= target <= 10^9
- Only one valid answer exists.
Time Complexity: O(nยฒ)
- ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ
- ์ฒซ ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ: n๋ฒˆ
- ๊ฐ๊ฐ์— ๋Œ€ํ•ด ๋‘ ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ: n-1, n-2, ... 1๋ฒˆ
- ๋”ฐ๋ผ์„œ ์ด ์—ฐ์‚ฐ ํšŸ์ˆ˜๋Š” n * (n-1)/2๋กœ O(nยฒ)
Space Complexity: O(1)
- ์ถ”๊ฐ€ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
- result ๋ฆฌ์ŠคํŠธ๋Š” ํ•ญ์ƒ ํฌ๊ธฐ๊ฐ€ 2๋กœ ๊ณ ์ •
"""
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
result = []

for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[j] == target - nums[i]:
return [i, j]

0 comments on commit 439086a

Please sign in to comment.