-
Notifications
You must be signed in to change notification settings - Fork 3
[채승윤] 5주차 제출합니다 #38
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
Open
chaeseungyun
wants to merge
1
commit into
main
Choose a base branch
from
week-05-chaeseungyun
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[채승윤] 5주차 제출합니다 #38
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,39 @@ | ||
# Intuition | ||
two sum의 투 포인터를 활용한다 | ||
|
||
# Approach | ||
1. 주어진 배열을 정렬한다. | ||
2. low, high 포인터를 만들고 세 숫자의 합을 기준으로 좁혀나간다. | ||
3. 합이 0보다 작으면 low를 증가시켜 0에 가깝게 하고 반대는 high를 감소하며 0에 가깝도록 한다. | ||
4. 합이 0이면 튜플을 결과 set에 추가한다. | ||
5. i를 배열의 길이 - 2 만큼 순회하면 끝 | ||
|
||
# Complexity | ||
- Time complexity: $$O(n^2)$$ | ||
- 입력 배열의 길이 n에 대하여, `i`, `j와 k`를 순회한다. | ||
|
||
- Space complexity: $$O(n)$$ | ||
|
||
# Code | ||
|
||
```python | ||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
result = set() | ||
nums.sort() | ||
for i in range(len(nums) - 2): | ||
low, high = i + 1, len(nums) - 1 | ||
while low < high: | ||
three = nums[i] + nums[low] + nums[high] | ||
if three < 0: | ||
low += 1 | ||
elif three > 0: | ||
high -= 1 | ||
else: | ||
result.add((nums[i], nums[low], nums[high])) | ||
low, high = low + 1, high - 1 | ||
return list(result) | ||
``` | ||
|
||
# learn | ||
투 포인터에 대한 개념을 확실히 잡아간 듯하다. 다른 문제에도 적용할 수 있으면 좋겠다 | ||
This file contains hidden or 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,22 @@ | ||
# Intuition | ||
간단해보인다! | ||
|
||
# Approach | ||
1. 아스키 문자가 아닌 문자를 구분자로 두고 인코딩한다 | ||
2. 사용한 구분자를 통해 디코딩한다 | ||
|
||
|
||
# Complexity | ||
- Time complexity: $$O(n)$$ | ||
|
||
- Space complexity: $$O(n)$$ | ||
|
||
# Code | ||
```python | ||
class Solution: | ||
def encode(self, strs: List[str]) -> str: | ||
return "😄".join(strs) | ||
|
||
def decode(self, s: str) -> List[str]: | ||
return s.split("😄") | ||
``` |
This file contains hidden or 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,34 @@ | ||
# Intuition | ||
정렬을 하면 접근하기 쉬울 것 같다 | ||
|
||
# Approach | ||
1. 리스트를 정렬한다. | ||
2. 대소관계가 확실하므로 자신과 다음 원소를 비교하며 차가 1이면 h에 현재 가장 큰 연속된 수의 길이를 저장한다. | ||
3. 현재 가장 긴 연속된 수의 길이와 지금까지 저장한 가장 큰 연속된 수의 길이를 비교해서 더 큰 값을 반환한다. | ||
|
||
# Complexity | ||
- Time complexity: $$O(nlog(n))$$ | ||
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. 정렬을 이용하지 않는 다른풀이: week-05/longest-consecutive-sequence/ImTotem.py.md 도 있어요! |
||
|
||
- Space complexity: $$O(n+m)$$ | ||
|
||
# Code | ||
```python | ||
class Solution: | ||
def longestConsecutive(self, nums: List[int]) -> int: | ||
## 파이썬 정렬함수 n log n | ||
if len(nums) == 0: | ||
return 0 | ||
nums.sort() | ||
h = 1 | ||
result = 1 | ||
for i in range(len(nums) - 1): | ||
if nums[i+1] - nums[i] == 1: | ||
h += 1 | ||
elif nums[i+1] - nums[i] == 0: | ||
continue | ||
else: | ||
result = max(h, result) | ||
h = 1 | ||
result = max(h, result) | ||
return result | ||
``` |
This file contains hidden or 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,35 @@ | ||
# Intuition | ||
브루트포스 밖에 생각이 안났다. 이러면 시간 초과인데.. | ||
|
||
# Approach | ||
|
||
# Complexity | ||
- Time complexity: $$O(n)$$ | ||
|
||
|
||
- Space complexity: $$O(n)$$ | ||
|
||
|
||
# Code | ||
```python | ||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
up = [1] * len(nums) | ||
|
||
for i in range(len(nums) - 1): | ||
up[i + 1] = up[i] * nums[i] | ||
|
||
down = [1] * len(nums) | ||
|
||
for i in range(len(nums) - 1, 0, -1): | ||
down[i - 1] = down[i] * nums[i] | ||
|
||
result = [] | ||
for x in zip(up, down): | ||
result.append(x[0] * x[1]) | ||
|
||
return result | ||
``` | ||
|
||
# learn | ||
풀이를 보고 풀었다. 이걸 dp라고 할 수 있는지 좀 더 탐구해봐야겠다 |
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. |
This file contains hidden or 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 @@ | ||
# Intuition | ||
사전을 이용하면 빈도수 체크하는데 편할 것 같다. | ||
|
||
# Approach | ||
1. 해시테이블을 만들고 key에 숫자를 넣고 value에 빈도수를 저장한다. | ||
2. 값을 기준으로 정렬한다. | ||
3. 뒤에서 k개를 뽑는다 | ||
|
||
# Complexity | ||
- Time complexity: $$O(nlog(n))$$ | ||
|
||
- Space complexity: $$O(n+k)$$ | ||
|
||
# Code | ||
```python | ||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
freq = {} | ||
result = [] | ||
for num in nums: | ||
freq[num] = freq.get(num, 0) + 1 | ||
sortedList = sorted(freq, key=lambda num: freq[num]) | ||
return sortedList[-k:] | ||
``` | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍 어려운 문젠데 잘 풀었네요 ㅎㅎㅎ