-
-
Notifications
You must be signed in to change notification settings - Fork 338
[YOOHYOJEONG] WEEK 02 solutions #2400
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
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,37 @@ | ||
| # https://leetcode.com/problems/3sum | ||
|
|
||
| class Solution(object): | ||
| def threeSum(self, nums): | ||
| nums.sort() | ||
| result = [] | ||
|
|
||
| for i in range(len(nums) - 2): | ||
|
|
||
| if i > 0 and nums[i] == nums[i - 1]: | ||
| continue | ||
|
|
||
| left = i + 1 | ||
| right = 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]]) | ||
|
|
||
| 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 result |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # https://leetcode.com/problems/climbing-stairs | ||
|
|
||
| class Solution(object): | ||
| def climbStairs(self, n): | ||
| if n <= 2: | ||
| return n | ||
|
|
||
| a, b = 1, 2 | ||
| for i in range(3, n+1): | ||
| a, b = b, a+b | ||
|
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. 다른 변수를 할당하지 않고 이렇게도 풀이할 수 있군요 ㅎㅎ👍 |
||
|
|
||
| return b | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # https://leetcode.com/problems/product-of-array-except-self/ | ||
|
|
||
| class Solution(object): | ||
| def productExceptSelf(self, nums): | ||
|
|
||
| n = len(nums) | ||
| output = [1] * n | ||
|
|
||
| left = 1 | ||
| for i in range(n): | ||
| output[i] = left | ||
| left *= nums[i] | ||
|
|
||
| right = 1 | ||
| for i in range(n-1, -1, -1): | ||
|
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. 개인적으로는 for문 조건에서 감산하는 형태로 사용하는 걸 지양하는 편인데, for i in range(n):
idx = n - i
output[idx] *= right
...
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. 좋은 의견 감사합니다!! Python에서는 보통 reversed(range(n)) 같은 방식으로도 표현할 수 있을 것 같아서 그 부분도 한 번 고려해 볼 수 있을 것 같습니다!! |
||
| output[i] *= right | ||
| right *= nums[i] | ||
|
|
||
| return output | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # https://leetcode.com/problems/valid-anagram/ | ||
|
|
||
| class Solution(object): | ||
| def isAnagram(self, s, t): | ||
| if sorted(s) == sorted(t): | ||
|
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. sorted함수가 대부분의 언어에서 n log n 시간복잡도를 가지기 때문에,
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. @hyeri0903 님 덕분에 Counter를 사용하면 정렬 없이 O(n) 시간복잡도로 해결할 수 있다는 점을 알게 되었습니다!! 두 분 모두 감사합니다! |
||
| return True | ||
| else: | ||
|
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. if 구문을 사용하지 않고 한줄로 작성할수도 있을것 같네요! Counter 를 사용한다면 시간 복잡도를 O(n)으로 줄일 수 있을것 같습니다.
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. 정렬을 이용한 방식이 간단한 것 같아서 이렇게 풀었는데 말씀해주신 것처럼 Counter를 사용하면 O(n)으로 더 효율적으로 해결할 수 있을 것 같습니다! 좋은 방법 공유해주셔서 감사합니다! |
||
| return False | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # https://leetcode.com/problems/validate-binary-search-tree/description/ | ||
|
|
||
| # 도저히 안풀어서 지피티의 도움을 받았습니다..TT | ||
| # 새로 알게 된 개념 | ||
| # 이진 트리는 하나의 노드가 최대 두 개의 자식 노드를 가지는 트리 자료구조 | ||
| # 두 개의 포인터를 가지고 있으며, 루트에서 시작해서 아래로 내려가는 계층 구조 | ||
| # Binary Search Tree(BST)는 이진 트리의 한 종류로 어떤 노드의 왼쪽 서브트리에 있는 모든 값은 해당 노드보다 작고 오른쪽 서브트리에 있는 모든 값은 해당 노드보다 큼 | ||
| # 이 문제는 주어진 트리가 이 규칙을 만족하는지 확인하는 문제임 | ||
| # 각 노드가 가질 수 있는 허용 범위(min, max)를 유지하면서 검사해야 함 | ||
| class Solution: | ||
| def isValidBST(self, root): | ||
|
|
||
| def dfs(node, low, high): | ||
| if not node: | ||
| return True | ||
|
|
||
| if node.val <= low or node.val >= high: | ||
| return False | ||
|
|
||
| return ( | ||
| dfs(node.left, low, node.val) and | ||
| dfs(node.right, node.val, high) | ||
| ) | ||
|
|
||
| return dfs(root, float("-inf"), float("inf")) |
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.
파이썬의 다중 할당이 편리하네요.