From 60d90ecbbcefdf1db20e3682776e768d14229b8e Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Tue, 1 Apr 2025 22:16:37 +0900 Subject: [PATCH 01/12] valid anagram solution(js) --- valid-anagram/hi-rachel.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 valid-anagram/hi-rachel.js diff --git a/valid-anagram/hi-rachel.js b/valid-anagram/hi-rachel.js new file mode 100644 index 000000000..fb917a050 --- /dev/null +++ b/valid-anagram/hi-rachel.js @@ -0,0 +1,27 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function (s, t) { + let hashMap1 = new Map(); + let hashMap2 = new Map(); + + for (let i = 0; i < s.length; i++) { + hashMap1.set(s[i], (hashMap1.get(s[i]) || 0) + 1); + } + + for (let i = 0; i < t.length; i++) { + hashMap2.set(t[i], (hashMap2.get(t[i]) || 0) + 1); + } + + function areMapsEqual(map1, map2) { + if (map1.size !== map2.size) return false; + + for (let [key, value] of map1) { + if (map2.get(key) !== value) return false; + } + return true; + } + return areMapsEqual(hashMap1, hashMap2); +}; From 1c8bafb583b294fc23650621caba54ab2751ea54 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Tue, 1 Apr 2025 22:29:54 +0900 Subject: [PATCH 02/12] add time, space complexity --- valid-anagram/hi-rachel.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/valid-anagram/hi-rachel.js b/valid-anagram/hi-rachel.js index fb917a050..0be425e65 100644 --- a/valid-anagram/hi-rachel.js +++ b/valid-anagram/hi-rachel.js @@ -1,3 +1,8 @@ +/** + * O(n) time + * O(문자수) space + */ + /** * @param {string} s * @param {string} t From 93c320b9378346be56af764e1d8a778bc4d3afb3 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Thu, 3 Apr 2025 21:52:30 +0900 Subject: [PATCH 03/12] two sum solution(py, js) --- two-sum/hi-rachel.py | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 two-sum/hi-rachel.py diff --git a/two-sum/hi-rachel.py b/two-sum/hi-rachel.py new file mode 100644 index 000000000..76ffed5c0 --- /dev/null +++ b/two-sum/hi-rachel.py @@ -0,0 +1,50 @@ +""" +처음 풀이 +O(N^2) time, O(N) space +""" + +# class Solution: +# def twoSum(self, nums: List[int], target: int) -> List[int]: +# result = [] +# for i in range(len(nums)): +# rest = target - nums[i] +# rest_nums = nums[i+1:] +# if rest in rest_nums: +# result.extend([i, rest_nums.index(rest)+i+1]) +# break +# return result + + +""" +개선 코드 +O(N) time, O(N) space +""" + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + indices = {} + + for i, v in enumerate(nums): + diff = target - v + if diff in indices: + j = indices[diff] + return [i, j] + indices[v] = i + +# JS 풀이 +# /** +# * @param {number[]} nums +# * @param {number} target +# * @return {number[]} +# */ +# var twoSum = function(nums, target) { +# let indices = new Map(); + +# for (let i = 0; i < nums.length; i++) { +# diff = target - nums[i]; +# if (indices.has(diff)) { +# return [i, indices.get(diff)]; +# } +# indices.set(nums[i], i); +# } +# }; \ No newline at end of file From 4842af2fba3c5f1fd90cff7f1e09eb7e68d590fd Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Thu, 3 Apr 2025 22:03:58 +0900 Subject: [PATCH 04/12] contains duplicate solution(ts) --- contains-duplicate/hi-rachel.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 contains-duplicate/hi-rachel.ts diff --git a/contains-duplicate/hi-rachel.ts b/contains-duplicate/hi-rachel.ts new file mode 100644 index 000000000..d42b54b4c --- /dev/null +++ b/contains-duplicate/hi-rachel.ts @@ -0,0 +1,18 @@ +/** + * 어떤 value든 array에서 2번 나오면 true 반환 + * 다 unique한 값이면 false 반환 + * O(N) time, O(N) space + * */ + +function containsDuplicate(nums: number[]): boolean { + let numMap = new Map(); + + for (let i = 0; i < nums.length; i++) { + if (numMap.has(nums[i])) { + return true; + } else { + numMap.set(nums[i], 1); + } + } + return false; +} From ee7f94185776e7484ed1b772f9a2c03f22fc359d Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Thu, 3 Apr 2025 23:01:47 +0900 Subject: [PATCH 05/12] top k frequent elements solution(py) --- top-k-frequent-elements/hi-rachel.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 top-k-frequent-elements/hi-rachel.py diff --git a/top-k-frequent-elements/hi-rachel.py b/top-k-frequent-elements/hi-rachel.py new file mode 100644 index 000000000..b5e315e15 --- /dev/null +++ b/top-k-frequent-elements/hi-rachel.py @@ -0,0 +1,21 @@ +# 가장 자주 등장한 상위 K개의 문자 배열 반환 +# O(n log n) time, O(n) space + +from collections import defaultdict + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + numdict = defaultdict(int); + result = [] + + for num in nums: + if num in numdict: + numdict[num] += 1 + else: + numdict[num] = 1 + + sort_dict = dict(sorted(numdict.items(), key=lambda item: item[1], reverse=True)) + + keys = list(sort_dict) + + return keys[:k] From e41deb1943fe945477ec49c70595b85d264221ac Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Thu, 3 Apr 2025 23:13:03 +0900 Subject: [PATCH 06/12] =?UTF-8?q?2=EC=A3=BC=EC=B0=A8=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=20=ED=92=80=EC=9D=B4=20=EC=BD=94=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-anagram/hi-rachel.js | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 valid-anagram/hi-rachel.js diff --git a/valid-anagram/hi-rachel.js b/valid-anagram/hi-rachel.js deleted file mode 100644 index 0be425e65..000000000 --- a/valid-anagram/hi-rachel.js +++ /dev/null @@ -1,32 +0,0 @@ -/** - * O(n) time - * O(문자수) space - */ - -/** - * @param {string} s - * @param {string} t - * @return {boolean} - */ -var isAnagram = function (s, t) { - let hashMap1 = new Map(); - let hashMap2 = new Map(); - - for (let i = 0; i < s.length; i++) { - hashMap1.set(s[i], (hashMap1.get(s[i]) || 0) + 1); - } - - for (let i = 0; i < t.length; i++) { - hashMap2.set(t[i], (hashMap2.get(t[i]) || 0) + 1); - } - - function areMapsEqual(map1, map2) { - if (map1.size !== map2.size) return false; - - for (let [key, value] of map1) { - if (map2.get(key) !== value) return false; - } - return true; - } - return areMapsEqual(hashMap1, hashMap2); -}; From e937b3238fc105033d7224cd7a5e14eb9d4f9565 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Thu, 3 Apr 2025 23:14:37 +0900 Subject: [PATCH 07/12] fix line lint --- two-sum/hi-rachel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/two-sum/hi-rachel.py b/two-sum/hi-rachel.py index 76ffed5c0..4b317cdc0 100644 --- a/two-sum/hi-rachel.py +++ b/two-sum/hi-rachel.py @@ -47,4 +47,4 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: # } # indices.set(nums[i], i); # } -# }; \ No newline at end of file +# }; From 6c958b8bbd9133645ee210ef728eaaa1e8acb609 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Sat, 5 Apr 2025 15:32:27 +0900 Subject: [PATCH 08/12] improve top k frequent elements solution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 올바른 default 사용 - 사용하지 않는 배열 삭제 --- top-k-frequent-elements/hi-rachel.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/top-k-frequent-elements/hi-rachel.py b/top-k-frequent-elements/hi-rachel.py index b5e315e15..8d0aac0ca 100644 --- a/top-k-frequent-elements/hi-rachel.py +++ b/top-k-frequent-elements/hi-rachel.py @@ -6,13 +6,9 @@ class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: numdict = defaultdict(int); - result = [] for num in nums: - if num in numdict: - numdict[num] += 1 - else: - numdict[num] = 1 + numdict[num] += 1 sort_dict = dict(sorted(numdict.items(), key=lambda item: item[1], reverse=True)) From cc48c3a677b1ba987a27b3debc332786896ad013 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Sat, 5 Apr 2025 18:56:11 +0900 Subject: [PATCH 09/12] longest consecutive sequence solution(py, ts) --- longest-consecutive-sequence/hi-rachel.py | 121 ++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 longest-consecutive-sequence/hi-rachel.py diff --git a/longest-consecutive-sequence/hi-rachel.py b/longest-consecutive-sequence/hi-rachel.py new file mode 100644 index 000000000..5c244ac5e --- /dev/null +++ b/longest-consecutive-sequence/hi-rachel.py @@ -0,0 +1,121 @@ +# 처음 풀이 +# O(n log n) time, O(n) space + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if nums == []: + return 0 + + nums.sort() + result = [] + cnt = 1 + + for i in range(len(nums)-1): + if nums[i]+1 == nums[i+1]: + cnt += 1 + elif nums[i] == nums[i+1]: + continue + else: + result.append(cnt) + cnt = 1 + result.append(cnt) + + return max(result) + +# 공간 복잡도 O(1) 개선 풀이 +# result 배열 사용시 최악의 경우 최대 n개의 숫자가 저장되므로 O(n) 공간 사용 +# longest 정수 변수 사용시 상수 공간 사용 + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if not nums: # pythonic code + return 0 + + nums.sort() + longest = 0 + cnt = 1 + + for i in range(len(nums)-1): + if nums[i]+1 == nums[i+1]: + cnt += 1 + elif nums[i] == nums[i+1]: + continue + else: + longest = max(cnt, longest) + cnt = 1 + longest = max(cnt, longest) + + return longest + +# 시간 복잡도 O(n) 개선 풀이 +# 현재 문제의 요구사항은 시간 복잡도 O(n)이었으므로, 위에 풀이들은 틀렸음 (You must write an algorithm that runs in O(n) time.) +# O(n) time, O(n) space -> 리트 코드 통과 x + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + num_set = set(nums) + longest = 0 + + for num in nums: + if num - 1 in num_set: + continue + cnt = 1 + while num + cnt in num_set: + cnt += 1 + longest = max(cnt, longest) + + return longest + + +# 최종 개선 풀이 +# O(n) time, O(n) space +# 위 풀이에서 한쪽으로 구간을 찾지 않고, 양쪽으로 찾으며 숫자를 집합에서 제거하며 +# 집합에서 원소가 하나도 남지 않을 때까지 하면 가장 긴 구간의 길이를 구할 수 있다. +# 배열의 모든 정수를 set에 저장했으므로 공간 복잡도는 O(n) + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + longest = 0 + num_set = set(nums) + + while num_set: + num = num_set.pop() + left, right = 1, 1 + + while num - left in num_set: + num_set.remove(num - left) + left += 1 + + while num + right in num_set: + num_set.remove(num + right) + right += 1 + longest = max(left + right - 1, longest) + + return longest + +# TS 풀이 +# O(n) time, O(n) space +# JavaScript Set에서 값을 꺼내고자 할때는 **numSet.values().next().value** 사용 + +# function longestConsecutive(nums: number[]): number { +# let numSet = new Set(nums); +# let longest = 0; + +# while (numSet.size !== 0) { +# let num = numSet.values().next().value; +# numSet.delete(num); +# let [left, right] = [1, 1]; + +# while (numSet.has(num - left)) { +# numSet.delete(num - left); +# left += 1; +# } + +# while (numSet.has(num + right)) { +# numSet.delete(num + right); +# right += 1; +# } +# longest = Math.max(left + right - 1, longest); +# } +# return longest; +# }; \ No newline at end of file From 3edfb0ff15e4894be5f14861284d5dc83b544f13 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Sat, 5 Apr 2025 18:56:49 +0900 Subject: [PATCH 10/12] fix line lint --- longest-consecutive-sequence/hi-rachel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-consecutive-sequence/hi-rachel.py b/longest-consecutive-sequence/hi-rachel.py index 5c244ac5e..bc6a3265a 100644 --- a/longest-consecutive-sequence/hi-rachel.py +++ b/longest-consecutive-sequence/hi-rachel.py @@ -118,4 +118,4 @@ def longestConsecutive(self, nums: List[int]) -> int: # longest = Math.max(left + right - 1, longest); # } # return longest; -# }; \ No newline at end of file +# }; From 91feb1533fc7a776bc977c339c729f53023eb610 Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Sat, 5 Apr 2025 19:47:05 +0900 Subject: [PATCH 11/12] house robber solution (ts, py) --- house-robber/hi-rachel.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 house-robber/hi-rachel.py diff --git a/house-robber/hi-rachel.py b/house-robber/hi-rachel.py new file mode 100644 index 000000000..0b31d3a2a --- /dev/null +++ b/house-robber/hi-rachel.py @@ -0,0 +1,32 @@ +# O(n) time, O(n) space + +class Solution: + def rob(self, nums: List[int]) -> int: + if not nums: return 0 + if len(nums) == 1: return nums[0] + + dp = [0] * len(nums) + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + + for i in range(2, len(nums)): + dp[i] = max(dp[i - 1], nums[i] + dp[i - 2]) + + return dp[-1] + + +# TS 코드 +# function rob(nums: number[]): number { +# if (nums.length === 0) return 0; +# if (nums.length === 1) return nums[0]; + +# const dp: number[] = new Array(nums.length).fill(0); +# dp[0] = nums[0]; +# dp[1] = Math.max(nums[0], nums[1]); + +# for (let i = 2; i < nums.length; i++) { +# dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]); +# } + +# return dp[nums.length - 1]; +# } From 821aa4a002e0f11052775567656c49b0b8548c3c Mon Sep 17 00:00:00 2001 From: hi-rachel Date: Sat, 5 Apr 2025 19:54:45 +0900 Subject: [PATCH 12/12] =?UTF-8?q?house=20robber=20solution=20=EC=84=A4?= =?UTF-8?q?=EB=AA=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- house-robber/hi-rachel.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/house-robber/hi-rachel.py b/house-robber/hi-rachel.py index 0b31d3a2a..3b99750bc 100644 --- a/house-robber/hi-rachel.py +++ b/house-robber/hi-rachel.py @@ -1,4 +1,14 @@ # O(n) time, O(n) space +# dp[i]는 i번째 집까지 봤을 때의 최대 누적 금액 + +# 두 가지 선택지를 고려: +# 1. 이 집을 턴다: +# 이전 집은 털 수 없으니 dp[i-2] + nums[i] +# 2. 이 집을 안 턴다: +# 그냥 전 집까지의 최대 금액 유지: dp[i-1] +# 두 가지 선택지 중 큰 걸 선택 +# **dp[i] = max(dp[i-1], dp[i-2] + nums[i])** +# nums 길이가 2인 경우 range(2, 2)는 for문 안 돈다. class Solution: def rob(self, nums: List[int]) -> int: @@ -13,7 +23,7 @@ def rob(self, nums: List[int]) -> int: dp[i] = max(dp[i - 1], nums[i] + dp[i - 2]) return dp[-1] - + # TS 코드 # function rob(nums: number[]): number {