-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #766 from KwonNayeon/main
[KwonNayeon] Week 3
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |