-
-
Notifications
You must be signed in to change notification settings - Fork 362
[hellojoyworldz] WEEK 02 solutions #2697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a0fa13c
4eb7a4b
3869b3e
0425aa3
149c03f
addbe11
435576e
a340077
e9e209f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # 문제: https://leetcode.com/problems/3sum/ | ||
| # 해설: https://www.algodale.com/problems/3sum/ | ||
| # 위치: https://github.com/DaleStudy/leetcode-study/tree/main/3sum | ||
|
|
||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
| nums.sort() | ||
| result = [] | ||
|
|
||
| for i in range(len(nums)): | ||
| if i > 0 and nums[i] == nums[i - 1]: | ||
| continue | ||
|
|
||
| left, right = i + 1, len(nums) - 1 | ||
| while left < right: | ||
| total = nums[i] + nums[left] + nums[right] | ||
|
|
||
| if total < 0: | ||
| left += 1 | ||
| elif total > 0: | ||
| right -= 1 | ||
| else: | ||
| result.append([nums[i], nums[left], nums[right]]) | ||
| left += 1 | ||
| right -= 1 | ||
|
|
||
| while left < right and nums[left] == nums[left - 1]: | ||
| left += 1 | ||
| while left < right and nums[right] == nums[right + 1]: | ||
| right -= 1 | ||
|
|
||
| return result | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # 문제: https://leetcode.com/problems/climbing-stairs/ | ||
| # 해설: https://www.algodale.com/problems/climbing-stairs/ | ||
| # 위치: https://github.com/DaleStudy/leetcode-study/tree/main/climbing-stairs | ||
|
|
||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| prev2, prev1 = 1, 1 | ||
| for _ in range(n - 1): | ||
| prev2, prev1 = prev1, prev2 + prev1 | ||
| return prev1 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개인적으로는 이런 부분들이 읽으면서 생각을 한 번 더 하게 되었던 거 같습니다!
가독성을 고려한다면 배열을 사용해봐도 괜찮을 거 같습니다~ class Solution:
def climbStairs(self, n: int) -> int:
if n == 1:
return 1
arr = [0] * n
arr[0], arr[1] = 1, 2
for i in range(2, n):
arr[i] = arr[i - 1] + arr[i - 2]
return arr[-1]
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안 그래도 가독성 부분이 고민이였는데, |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # 문제: https://leetcode.com/problems/product-of-array-except-self/ | ||
| # 해설: https://www.algodale.com/problems/product-of-array-except-self/ | ||
| # 위치: https://github.com/DaleStudy/leetcode-study/tree/main/product-of-array-except-self | ||
|
|
||
| from typing import List | ||
|
|
||
|
|
||
| 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 reversed(range(n)): | ||
| answer[i] *= right | ||
| right *= nums[i] | ||
|
Comment on lines
+19
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reversed(range(n)) 사용하신거 간결하고 좋네요~ |
||
|
|
||
| return answer | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # 문제: https://leetcode.com/problems/valid-anagram/ | ||
| # 해설: https://www.algodale.com/problems/valid-anagram/ | ||
| # 위치: https://github.com/DaleStudy/leetcode-study/tree/main/valid-anagram | ||
|
|
||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| return sorted(s) == sorted(t) | ||
|
|
||
|
Comment on lines
+5
to
+8
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 간결하고 좋네요! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # 문제: https://leetcode.com/problems/validate-binary-search-tree/ | ||
| # 해설: https://www.algodale.com/problems/validate-binary-search-tree/ | ||
| # 위치: https://github.com/DaleStudy/leetcode-study/tree/main/validate-binary-search-tree | ||
|
|
||
| from typing import Optional | ||
|
|
||
| # Definition for a binary tree node. | ||
| class TreeNode: | ||
| def __init__(self, val=0, left=None, right=None): | ||
| self.val = val | ||
| self.left = left | ||
| self.right = right | ||
|
|
||
| class Solution: | ||
| def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
| def check(node: Optional[TreeNode], low: float, high: float) -> bool: | ||
| if not node: | ||
| return True | ||
|
|
||
| if not (low < node.val < high): | ||
| return False | ||
|
Comment on lines
+20
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (log < node.val < high) 사용하신거 좋네요! |
||
|
|
||
| return check(node.left, low, node.val) and check(node.right, node.val, high) | ||
|
|
||
| return check(root, float("-inf"), float("inf")) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
정렬되어 있는 상태라 nums[i] > 0 인 부분은 break해도 좋을 거 같습니다!
ex. [-1, 0, 1, 2, 3, 4, 5, 6, 7], i = 2 부터는
양수 + 양수 + 양수There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 그러네요, 제가 놓쳤습니다
세심한 피드백 정말 감사합니다! 🙇♂️