From 6fab29a1d90827334e3fa42e59be6b2555b56b66 Mon Sep 17 00:00:00 2001 From: chaeseungyun Date: Mon, 19 Aug 2024 01:06:40 +0900 Subject: [PATCH] =?UTF-8?q?5=EC=A3=BC=EC=B0=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week-05/3sum/chaeseungyun.py.md | 39 +++++++++++++++++++ .../chaeseungyun.py.md | 22 +++++++++++ .../chaeseungyun.py.md | 34 ++++++++++++++++ .../chaeseungyun.py.md | 35 +++++++++++++++++ .../chaeseungyun.py.md | 25 ++++++++++++ 5 files changed, 155 insertions(+) create mode 100644 week-05/3sum/chaeseungyun.py.md create mode 100644 week-05/encode-and-decode-strings/chaeseungyun.py.md create mode 100644 week-05/longest-consecutive-sequence/chaeseungyun.py.md create mode 100644 week-05/product-of-array-except-self/chaeseungyun.py.md create mode 100644 week-05/top-k-frequent-elements/chaeseungyun.py.md diff --git a/week-05/3sum/chaeseungyun.py.md b/week-05/3sum/chaeseungyun.py.md new file mode 100644 index 0000000..0d4d290 --- /dev/null +++ b/week-05/3sum/chaeseungyun.py.md @@ -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 +투 포인터에 대한 개념을 확실히 잡아간 듯하다. 다른 문제에도 적용할 수 있으면 좋겠다 \ No newline at end of file diff --git a/week-05/encode-and-decode-strings/chaeseungyun.py.md b/week-05/encode-and-decode-strings/chaeseungyun.py.md new file mode 100644 index 0000000..9de1106 --- /dev/null +++ b/week-05/encode-and-decode-strings/chaeseungyun.py.md @@ -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("😄") +``` diff --git a/week-05/longest-consecutive-sequence/chaeseungyun.py.md b/week-05/longest-consecutive-sequence/chaeseungyun.py.md new file mode 100644 index 0000000..a3c91e4 --- /dev/null +++ b/week-05/longest-consecutive-sequence/chaeseungyun.py.md @@ -0,0 +1,34 @@ +# Intuition +정렬을 하면 접근하기 쉬울 것 같다 + +# Approach +1. 리스트를 정렬한다. +2. 대소관계가 확실하므로 자신과 다음 원소를 비교하며 차가 1이면 h에 현재 가장 큰 연속된 수의 길이를 저장한다. +3. 현재 가장 긴 연속된 수의 길이와 지금까지 저장한 가장 큰 연속된 수의 길이를 비교해서 더 큰 값을 반환한다. + +# Complexity +- Time complexity: $$O(nlog(n))$$ + +- 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 +``` \ No newline at end of file diff --git a/week-05/product-of-array-except-self/chaeseungyun.py.md b/week-05/product-of-array-except-self/chaeseungyun.py.md new file mode 100644 index 0000000..4c45ae4 --- /dev/null +++ b/week-05/product-of-array-except-self/chaeseungyun.py.md @@ -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라고 할 수 있는지 좀 더 탐구해봐야겠다 \ No newline at end of file diff --git a/week-05/top-k-frequent-elements/chaeseungyun.py.md b/week-05/top-k-frequent-elements/chaeseungyun.py.md new file mode 100644 index 0000000..876c384 --- /dev/null +++ b/week-05/top-k-frequent-elements/chaeseungyun.py.md @@ -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:] +``` +