Skip to content

[채승윤] 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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions week-05/3sum/chaeseungyun.py.md
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
투 포인터에 대한 개념을 확실히 잡아간 듯하다. 다른 문제에도 적용할 수 있으면 좋겠다
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 어려운 문젠데 잘 풀었네요 ㅎㅎㅎ

22 changes: 22 additions & 0 deletions week-05/encode-and-decode-strings/chaeseungyun.py.md
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("😄")
```
34 changes: 34 additions & 0 deletions week-05/longest-consecutive-sequence/chaeseungyun.py.md
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))$$
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
```
35 changes: 35 additions & 0 deletions week-05/product-of-array-except-self/chaeseungyun.py.md
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라고 할 수 있는지 좀 더 탐구해봐야겠다
25 changes: 25 additions & 0 deletions week-05/top-k-frequent-elements/chaeseungyun.py.md
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,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:]
```