From 6d1ee5a3330b4ece452d15c2b62c5f500575a19b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Sun, 11 Aug 2024 23:25:15 +0900 Subject: [PATCH 01/50] [LC] feat: contains-duplicate --- contains-duplicate/hajunyoo.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 contains-duplicate/hajunyoo.py diff --git a/contains-duplicate/hajunyoo.py b/contains-duplicate/hajunyoo.py new file mode 100644 index 000000000..ded649695 --- /dev/null +++ b/contains-duplicate/hajunyoo.py @@ -0,0 +1,6 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + string_len = len(nums) + set_len = len(set(nums)) + + return string_len != set_len \ No newline at end of file From d022e736fe637dba893917376c8f87c437eb4b93 Mon Sep 17 00:00:00 2001 From: mykoo Date: Mon, 12 Aug 2024 18:16:30 +0900 Subject: [PATCH 02/50] =?UTF-8?q?[=EA=B5=AC=EB=AC=B8=EC=98=81]=20Week1=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/GUMUNYEONG.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 contains-duplicate/GUMUNYEONG.js diff --git a/contains-duplicate/GUMUNYEONG.js b/contains-duplicate/GUMUNYEONG.js new file mode 100644 index 000000000..694118a71 --- /dev/null +++ b/contains-duplicate/GUMUNYEONG.js @@ -0,0 +1,14 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function (nums) { + const setObj = new Set(nums); + + const diff = !(nums.length === setObj.size); + + return diff; +}; + +// TC: O(n) +// SC: O(n) \ No newline at end of file From f3b80dc6ce476dcc594c0b6465e61c869824f4d9 Mon Sep 17 00:00:00 2001 From: mykoo Date: Tue, 13 Aug 2024 15:14:04 +0900 Subject: [PATCH 03/50] GUMUNYEONG : number-of-1-bit solution --- number-of-1-bits/GUMUNYEONG.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 number-of-1-bits/GUMUNYEONG.js diff --git a/number-of-1-bits/GUMUNYEONG.js b/number-of-1-bits/GUMUNYEONG.js new file mode 100644 index 000000000..af5e4cff6 --- /dev/null +++ b/number-of-1-bits/GUMUNYEONG.js @@ -0,0 +1,17 @@ +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function (n) { + let i = "1"; + + while (n > 2) { + n % 2 ? i += "1" : ""; + n = Math.floor(n / 2); + } + + return i.length; +}; + +// TC: O(log n) +// SC: O(log n) \ No newline at end of file From 49339cc2522c20286200e1d974101aa4084693ce Mon Sep 17 00:00:00 2001 From: mykoo Date: Tue, 13 Aug 2024 15:16:53 +0900 Subject: [PATCH 04/50] =?UTF-8?q?GUMUNYEONG=20:=20=EB=A7=88=EC=A7=80?= =?UTF-8?q?=EB=A7=89=EC=A4=84=20=EA=B0=9C=ED=96=89=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- number-of-1-bits/GUMUNYEONG.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number-of-1-bits/GUMUNYEONG.js b/number-of-1-bits/GUMUNYEONG.js index af5e4cff6..c82f12c5a 100644 --- a/number-of-1-bits/GUMUNYEONG.js +++ b/number-of-1-bits/GUMUNYEONG.js @@ -14,4 +14,4 @@ var hammingWeight = function (n) { }; // TC: O(log n) -// SC: O(log n) \ No newline at end of file +// SC: O(log n) From a2d162185faedec2cf3627731899d47299ea28be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Wed, 14 Aug 2024 02:01:33 +0900 Subject: [PATCH 05/50] [LC] feat: number-of-bits --- number-of-1-bits/hajunyoo.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 number-of-1-bits/hajunyoo.py diff --git a/number-of-1-bits/hajunyoo.py b/number-of-1-bits/hajunyoo.py new file mode 100644 index 000000000..7609a495a --- /dev/null +++ b/number-of-1-bits/hajunyoo.py @@ -0,0 +1,9 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + n = int(n) + cnt = 0 + while n > 0: + if n % 2 == 1: + cnt += 1 + n = n // 2 + return cnt \ No newline at end of file From 374f84338c1a09ee68a7784a22e0315ead563841 Mon Sep 17 00:00:00 2001 From: mykoo Date: Wed, 14 Aug 2024 16:46:49 +0900 Subject: [PATCH 06/50] GUMUNYEONG.js : top-k-frequent-elements solution --- top-k-frequent-elements/GUMUNYEONG.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 top-k-frequent-elements/GUMUNYEONG.js diff --git a/top-k-frequent-elements/GUMUNYEONG.js b/top-k-frequent-elements/GUMUNYEONG.js new file mode 100644 index 000000000..0ac4a6f09 --- /dev/null +++ b/top-k-frequent-elements/GUMUNYEONG.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function (nums, k) { + let result = []; + let fObj = {}; + + for (let i = 0; i < nums.length; i++) { + const n = nums[i]; + fObj[n] ? fObj[n]++ : fObj[n] = 1; + } + + Object + .entries(fObj) + .sort((a, b) => b[1] - a[1]) + .slice(0, k) + .filter(v => result.push(v[0])); + + return result; +}; From ca0a2b411e67bf56e1b24ff8098ef558e28f98c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Thu, 15 Aug 2024 23:29:45 +0900 Subject: [PATCH 07/50] [LC] feat: top k frequent elements --- top-k-frequent-elements/hajunyoo.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 top-k-frequent-elements/hajunyoo.py diff --git a/top-k-frequent-elements/hajunyoo.py b/top-k-frequent-elements/hajunyoo.py new file mode 100644 index 000000000..b432bb1ca --- /dev/null +++ b/top-k-frequent-elements/hajunyoo.py @@ -0,0 +1,19 @@ +from collections import defaultdict +from typing import List + + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + counter_dict = defaultdict(int) + + for n in nums: + counter_dict[n] += 1 + + count_list = [] + for key, val in counter_dict.items(): + count_list.append((key, val)) + + count_list.sort(key=lambda x: x[1], reverse=True) + answer = [a for a, b in count_list[:k]] + + return answer \ No newline at end of file From 833fda5cb9fda26f2b996587e675fb08d0bd0593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Fri, 16 Aug 2024 00:22:13 +0900 Subject: [PATCH 08/50] [LC] feat: top k frequent elements (bst) --- kth-smallest-element-in-a-bst/hajunyoo.py | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/hajunyoo.py diff --git a/kth-smallest-element-in-a-bst/hajunyoo.py b/kth-smallest-element-in-a-bst/hajunyoo.py new file mode 100644 index 000000000..f2d512831 --- /dev/null +++ b/kth-smallest-element-in-a-bst/hajunyoo.py @@ -0,0 +1,30 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +from collections import defaultdict + + +class Solution: + def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: + + self.count = 0 + self.result = 0 + + def dfs(node): + if not node: + return + + dfs(node.left) + + self.count += 1 + if self.count == k: + self.result = node.val + return + + dfs(node.right) + + dfs(root) + return self.result \ No newline at end of file From 22e0e0ccee428083150eef224a08c03261a307e6 Mon Sep 17 00:00:00 2001 From: whewchews Date: Sat, 17 Aug 2024 13:46:57 +0900 Subject: [PATCH 09/50] 1: contains duplicate --- contains-duplicate/whewchews.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 contains-duplicate/whewchews.ts diff --git a/contains-duplicate/whewchews.ts b/contains-duplicate/whewchews.ts new file mode 100644 index 000000000..7a939eac4 --- /dev/null +++ b/contains-duplicate/whewchews.ts @@ -0,0 +1,18 @@ +function containsDuplicate(nums: number[]): boolean { + const dict: Set = new Set(); + + // O(n) + for (let i = 0; i <= nums.length; i++) { + const n = nums[i]; + + // O(1) + if (dict.has(n)) return true; + // O(1) + dict.add(n); + } + + return false; +} + +// TC: O(N) +// SC: O(N) From ca6916cbd96c09b429d38f6e9532849277f84d2f Mon Sep 17 00:00:00 2001 From: whewchews Date: Sat, 17 Aug 2024 13:59:07 +0900 Subject: [PATCH 10/50] 2: number of 1 bits --- number-of-1-bits/whewchews.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 number-of-1-bits/whewchews.ts diff --git a/number-of-1-bits/whewchews.ts b/number-of-1-bits/whewchews.ts new file mode 100644 index 000000000..2e7ccf03c --- /dev/null +++ b/number-of-1-bits/whewchews.ts @@ -0,0 +1,15 @@ +function hammingWeight(n: number): number { + // SC: log(1) + let val = 0; + while (n > 0) { + val += n % 2; + + // TC: log₂(n) + n = Math.floor(n / 2); + } + + return val; +} + +// TC: O(log N) +// SC: O(1) From 906e832b54ad498acc9f17c77846a1474de150be Mon Sep 17 00:00:00 2001 From: CodyMan0 Date: Sat, 17 Aug 2024 14:13:24 +0900 Subject: [PATCH 11/50] containsDuplicate solution --- contains-duplicate/codyman0.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 contains-duplicate/codyman0.py diff --git a/contains-duplicate/codyman0.py b/contains-duplicate/codyman0.py new file mode 100644 index 000000000..9367db22b --- /dev/null +++ b/contains-duplicate/codyman0.py @@ -0,0 +1,15 @@ +""" +https://leetcode.com/problems/contains-duplicate/ +""" + +# Time complexity : O(n) + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + sortedArrary = sorted(nums) + for i in range(len(sortedArrary)): + if i == len(sortedArrary) - 1: + return False + if sortedArrary[i] == sortedArrary[i + 1] : + return True + return False \ No newline at end of file From 99b69acc054a2a758a3694af42e77916e32f8962 Mon Sep 17 00:00:00 2001 From: CodyMan0 Date: Sat, 17 Aug 2024 14:19:45 +0900 Subject: [PATCH 12/50] =?UTF-8?q?:bug:=20=EB=B3=80=EC=88=98=20=EC=98=A4?= =?UTF-8?q?=ED=83=88=EC=9E=90=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/codyman0.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contains-duplicate/codyman0.py b/contains-duplicate/codyman0.py index 9367db22b..28267b7bb 100644 --- a/contains-duplicate/codyman0.py +++ b/contains-duplicate/codyman0.py @@ -6,10 +6,10 @@ class Solution: def containsDuplicate(self, nums: List[int]) -> bool: - sortedArrary = sorted(nums) - for i in range(len(sortedArrary)): - if i == len(sortedArrary) - 1: + sortedArray = sorted(nums) + for i in range(len(sortedArray)): + if i == len(sortedArray) - 1: return False - if sortedArrary[i] == sortedArrary[i + 1] : + if sortedArray[i] == sortedArray[i + 1] : return True return False \ No newline at end of file From a1748988d1acb060131ed6df01501ba123570edc Mon Sep 17 00:00:00 2001 From: whewchews Date: Sat, 17 Aug 2024 14:20:09 +0900 Subject: [PATCH 13/50] 3: top k frequent elements --- top-k-frequent-elements/whewchews.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 top-k-frequent-elements/whewchews.ts diff --git a/top-k-frequent-elements/whewchews.ts b/top-k-frequent-elements/whewchews.ts new file mode 100644 index 000000000..633a6498a --- /dev/null +++ b/top-k-frequent-elements/whewchews.ts @@ -0,0 +1,28 @@ +function topKFrequent(nums: number[], k: number): number[] { + const dict: Map = new Map(); + + // TC: O(N) + // SC: O(N) + nums.forEach((n) => { + if (!dict.has(n)) dict.set(n, 1); + else { + dict.set(n, dict.get(n) + 1); + } + }); + + // TC: O(N) + // SC: O(N) + const buckets: number[][] = Array(nums.length + 1) + .fill(0) + .map((_) => []); + Array.from(dict.entries()).forEach(([num, cnt]) => { + buckets[cnt].push(num); + }); + + // TC: O(N) + O(k) = O(N) + // SC: O(N) + return buckets.flat().slice(-k); +} + +// TC: O(N) +// SC: O(N) From 718bde0895adf32004cd5f9b5e18ec812c956564 Mon Sep 17 00:00:00 2001 From: whewchews Date: Sat, 17 Aug 2024 14:28:51 +0900 Subject: [PATCH 14/50] 4: kth smallest element in a bst --- kth-smallest-element-in-a-bst/whewchews.ts | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/whewchews.ts diff --git a/kth-smallest-element-in-a-bst/whewchews.ts b/kth-smallest-element-in-a-bst/whewchews.ts new file mode 100644 index 000000000..65b632ec8 --- /dev/null +++ b/kth-smallest-element-in-a-bst/whewchews.ts @@ -0,0 +1,36 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function kthSmallest(root: TreeNode | null, k: number): number { + // SC: O(N) + const values: number[] = []; + + // TC: O(N) + const dfs = (node: TreeNode | null) => { + if (node == null) return; + dfs(node.left); + values.push(node.val); + dfs(node.right); + }; + + // SC: O(h) + // h: the height of the tree + dfs(root); + + // TC: O(1) + return values[k - 1]; +} + +// TC: O(N) +// SC: O(N) From 34a18cd321281dc675b41b340ced8614ff01fde8 Mon Sep 17 00:00:00 2001 From: whewchews Date: Sat, 17 Aug 2024 14:52:38 +0900 Subject: [PATCH 15/50] 5: palindromic-substrings --- palindromic-substrings/whewchews.ts | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 palindromic-substrings/whewchews.ts diff --git a/palindromic-substrings/whewchews.ts b/palindromic-substrings/whewchews.ts new file mode 100644 index 000000000..294c721f4 --- /dev/null +++ b/palindromic-substrings/whewchews.ts @@ -0,0 +1,35 @@ +function countSubstrings(s: string): number { + // SC: O(N^2) + const dict: Map = new Map(); + const n = s.length; + + // TC: O(N^2) + for (let start = 0; start < n; start++) { + for (let end = start; end >= 0; end--) { + if (start === end) { + dict.set(`${start}:${end}`, true); + } else if (start + 1 === end) { + dict.set(`${start}:${end}`, s[start] === s[end]); + } else { + const flag = s[start] === s[end]; + const mid = dict.get(`${start + 1}:${end - 1}`); + dict.set(`${start}:${end}`, flag && mid); + } + } + } + + let cnt = 0; + + // TC: O(N^2) + // SC: O(1) + for (const v of dict.values()) { + if (v) { + cnt++; + } + } + + return cnt; +} + +// TC: O(N^2) +// SC: O(N^2) From 6139c9c3d1cec00466333052e315344a6b7cba96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Sat, 17 Aug 2024 15:33:29 +0900 Subject: [PATCH 16/50] [LC] feat: palindromic-substring --- palindromic-substrings/hajunyoo.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 palindromic-substrings/hajunyoo.py diff --git a/palindromic-substrings/hajunyoo.py b/palindromic-substrings/hajunyoo.py new file mode 100644 index 000000000..87569cd34 --- /dev/null +++ b/palindromic-substrings/hajunyoo.py @@ -0,0 +1,18 @@ +class Solution: + def countSubstrings(self, s: str) -> int: + self.count = 0 + n = len(s) + + def two_pointer_expand(left, right): + while left >= 0 and right < n and s[left] == s[right]: + self.count += 1 + left -= 1 + right += 1 + + for i in range(0, n): + # 1, 3, 5 ... + two_pointer_expand(i, i) + # 2, 4, 6 ... + two_pointer_expand(i, i + 1) + + return self.count \ No newline at end of file From 0c8f19768a1654657561d8a274ae03cdd40368a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Sat, 17 Aug 2024 22:13:10 +0900 Subject: [PATCH 17/50] [LC] 240817 add time complexity --- contains-duplicate/hajunyoo.py | 1 + kth-smallest-element-in-a-bst/hajunyoo.py | 1 + number-of-1-bits/hajunyoo.py | 1 + palindromic-substrings/hajunyoo.py | 1 + top-k-frequent-elements/hajunyoo.py | 1 + 5 files changed, 5 insertions(+) diff --git a/contains-duplicate/hajunyoo.py b/contains-duplicate/hajunyoo.py index ded649695..207e5ff75 100644 --- a/contains-duplicate/hajunyoo.py +++ b/contains-duplicate/hajunyoo.py @@ -1,4 +1,5 @@ class Solution: + # Time complexity: O(n) def containsDuplicate(self, nums: List[int]) -> bool: string_len = len(nums) set_len = len(set(nums)) diff --git a/kth-smallest-element-in-a-bst/hajunyoo.py b/kth-smallest-element-in-a-bst/hajunyoo.py index f2d512831..518e64a03 100644 --- a/kth-smallest-element-in-a-bst/hajunyoo.py +++ b/kth-smallest-element-in-a-bst/hajunyoo.py @@ -8,6 +8,7 @@ class Solution: + # Time complexity: O(n) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: self.count = 0 diff --git a/number-of-1-bits/hajunyoo.py b/number-of-1-bits/hajunyoo.py index 7609a495a..86c450dcd 100644 --- a/number-of-1-bits/hajunyoo.py +++ b/number-of-1-bits/hajunyoo.py @@ -1,4 +1,5 @@ class Solution: + # Time complexity: O(log n) def hammingWeight(self, n: int) -> int: n = int(n) cnt = 0 diff --git a/palindromic-substrings/hajunyoo.py b/palindromic-substrings/hajunyoo.py index 87569cd34..7ab18dec9 100644 --- a/palindromic-substrings/hajunyoo.py +++ b/palindromic-substrings/hajunyoo.py @@ -1,4 +1,5 @@ class Solution: + # Time complexity: O(n^2) = O(n) * O(n) def countSubstrings(self, s: str) -> int: self.count = 0 n = len(s) diff --git a/top-k-frequent-elements/hajunyoo.py b/top-k-frequent-elements/hajunyoo.py index b432bb1ca..7b986a3d7 100644 --- a/top-k-frequent-elements/hajunyoo.py +++ b/top-k-frequent-elements/hajunyoo.py @@ -3,6 +3,7 @@ class Solution: + # Time complexity: O(nlogn) -> O(n) + O(nlogn) + O(k) def topKFrequent(self, nums: List[int], k: int) -> List[int]: counter_dict = defaultdict(int) From 5bd7b754d532a8ef3bd74d33a888cd6dd94e4e0b Mon Sep 17 00:00:00 2001 From: whewchews <111553718+whewchews@users.noreply.github.com> Date: Sun, 18 Aug 2024 08:57:15 +0900 Subject: [PATCH 18/50] Fix: reverse loop direction to check palindrome Co-authored-by: Dale Seo <5466341+DaleSeo@users.noreply.github.com> --- palindromic-substrings/whewchews.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/palindromic-substrings/whewchews.ts b/palindromic-substrings/whewchews.ts index 294c721f4..fa9e302f1 100644 --- a/palindromic-substrings/whewchews.ts +++ b/palindromic-substrings/whewchews.ts @@ -4,8 +4,8 @@ function countSubstrings(s: string): number { const n = s.length; // TC: O(N^2) - for (let start = 0; start < n; start++) { - for (let end = start; end >= 0; end--) { + for (let start = n; start >= 0; start--) { + for (let end = start; end < n; end++) { if (start === end) { dict.set(`${start}:${end}`, true); } else if (start + 1 === end) { From 986a34abc13df80125d5f739a3e4da2e8302b45e Mon Sep 17 00:00:00 2001 From: Cody Kim <50035753+0-Chan@users.noreply.github.com> Date: Sun, 18 Aug 2024 09:18:14 +0900 Subject: [PATCH 19/50] contains duplicate --- contains-duplicate/0-chan.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 contains-duplicate/0-chan.ts diff --git a/contains-duplicate/0-chan.ts b/contains-duplicate/0-chan.ts new file mode 100644 index 000000000..e768f9c9e --- /dev/null +++ b/contains-duplicate/0-chan.ts @@ -0,0 +1,8 @@ +/** + * time complexity : O(n) + * space complexity : O(n) + */ +function containsDuplicate(nums: number[]): boolean { + const hasDuplicate = new Set(nums).size !== nums.length; + return hasDuplicate; +}; From b589fd2d2539421c52c20a830f4f1e1209530ba2 Mon Sep 17 00:00:00 2001 From: Cody Kim <50035753+0-Chan@users.noreply.github.com> Date: Sun, 18 Aug 2024 09:22:23 +0900 Subject: [PATCH 20/50] number of 1 bits --- number-of-1-bits/0-chan.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 number-of-1-bits/0-chan.ts diff --git a/number-of-1-bits/0-chan.ts b/number-of-1-bits/0-chan.ts new file mode 100644 index 000000000..17b8fdba2 --- /dev/null +++ b/number-of-1-bits/0-chan.ts @@ -0,0 +1,10 @@ +/** + * time complexity : O(logn) + * space complexity : O(logn) + */ +function hammingWeight(n: number): number { + const MAX_NUM = 2147483648 - 1; + + const bitwiseOperated = (n & MAX_NUM).toString(2); + return bitwiseOperated.replaceAll('0', '').length; +}; From b9ae75028048721f89475d7807359361200db615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Sun, 18 Aug 2024 10:14:52 +0900 Subject: [PATCH 21/50] [LC] 240818 fix line break lint --- contains-duplicate/hajunyoo.py | 2 +- kth-smallest-element-in-a-bst/hajunyoo.py | 3 +-- number-of-1-bits/hajunyoo.py | 3 +-- palindromic-substrings/hajunyoo.py | 2 +- top-k-frequent-elements/hajunyoo.py | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/contains-duplicate/hajunyoo.py b/contains-duplicate/hajunyoo.py index 207e5ff75..058172a4b 100644 --- a/contains-duplicate/hajunyoo.py +++ b/contains-duplicate/hajunyoo.py @@ -4,4 +4,4 @@ def containsDuplicate(self, nums: List[int]) -> bool: string_len = len(nums) set_len = len(set(nums)) - return string_len != set_len \ No newline at end of file + return string_len != set_len diff --git a/kth-smallest-element-in-a-bst/hajunyoo.py b/kth-smallest-element-in-a-bst/hajunyoo.py index 518e64a03..3deb0e4a6 100644 --- a/kth-smallest-element-in-a-bst/hajunyoo.py +++ b/kth-smallest-element-in-a-bst/hajunyoo.py @@ -10,7 +10,6 @@ class Solution: # Time complexity: O(n) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: - self.count = 0 self.result = 0 @@ -28,4 +27,4 @@ def dfs(node): dfs(node.right) dfs(root) - return self.result \ No newline at end of file + return self.result diff --git a/number-of-1-bits/hajunyoo.py b/number-of-1-bits/hajunyoo.py index 86c450dcd..b8e0e07fc 100644 --- a/number-of-1-bits/hajunyoo.py +++ b/number-of-1-bits/hajunyoo.py @@ -1,10 +1,9 @@ class Solution: # Time complexity: O(log n) def hammingWeight(self, n: int) -> int: - n = int(n) cnt = 0 while n > 0: if n % 2 == 1: cnt += 1 n = n // 2 - return cnt \ No newline at end of file + return cnt diff --git a/palindromic-substrings/hajunyoo.py b/palindromic-substrings/hajunyoo.py index 7ab18dec9..08982c61b 100644 --- a/palindromic-substrings/hajunyoo.py +++ b/palindromic-substrings/hajunyoo.py @@ -16,4 +16,4 @@ def two_pointer_expand(left, right): # 2, 4, 6 ... two_pointer_expand(i, i + 1) - return self.count \ No newline at end of file + return self.count diff --git a/top-k-frequent-elements/hajunyoo.py b/top-k-frequent-elements/hajunyoo.py index 7b986a3d7..a26351c77 100644 --- a/top-k-frequent-elements/hajunyoo.py +++ b/top-k-frequent-elements/hajunyoo.py @@ -17,4 +17,4 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: count_list.sort(key=lambda x: x[1], reverse=True) answer = [a for a, b in count_list[:k]] - return answer \ No newline at end of file + return answer From 5ae087302615105fc062798f922a46ff3cadc0f8 Mon Sep 17 00:00:00 2001 From: GUMUNYEONG Date: Sun, 18 Aug 2024 11:40:21 +0900 Subject: [PATCH 22/50] =?UTF-8?q?GUMUNYEONG=20:=20=EB=A7=88=EC=A7=80?= =?UTF-8?q?=EB=A7=89=EC=A4=84=20=EA=B0=9C=ED=96=89=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/GUMUNYEONG.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contains-duplicate/GUMUNYEONG.js b/contains-duplicate/GUMUNYEONG.js index 694118a71..fca1f6f68 100644 --- a/contains-duplicate/GUMUNYEONG.js +++ b/contains-duplicate/GUMUNYEONG.js @@ -11,4 +11,4 @@ var containsDuplicate = function (nums) { }; // TC: O(n) -// SC: O(n) \ No newline at end of file +// SC: O(n) From 7934db91235337845629d0b861c9f4910edd2c34 Mon Sep 17 00:00:00 2001 From: mykoo Date: Mon, 19 Aug 2024 11:59:03 +0900 Subject: [PATCH 23/50] =?UTF-8?q?number-of-1-bits=20SC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- number-of-1-bits/GUMUNYEONG.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number-of-1-bits/GUMUNYEONG.js b/number-of-1-bits/GUMUNYEONG.js index c82f12c5a..02805653b 100644 --- a/number-of-1-bits/GUMUNYEONG.js +++ b/number-of-1-bits/GUMUNYEONG.js @@ -14,4 +14,4 @@ var hammingWeight = function (n) { }; // TC: O(log n) -// SC: O(log n) +// SC: O(1) From 297341270572de6e94938e6845daeb6e256a8136 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 12:51:52 +0900 Subject: [PATCH 24/50] solution: valid anagram --- valid-anagram/flynn.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 valid-anagram/flynn.py diff --git a/valid-anagram/flynn.py b/valid-anagram/flynn.py new file mode 100644 index 000000000..b45d142a5 --- /dev/null +++ b/valid-anagram/flynn.py @@ -0,0 +1,25 @@ +''' +For N, length of the given strings, + +Time Complexity: O(N) +- first iteration: O(N) +- second iteration: O(N) + +Space Compelxity: O(N) +- dictionaries for each strings: O(N) +''' + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + count_s, count_t = {}, {} + for i in range(len(s)): + count_s[s[i]] = count_s.get(s[i], 0) + 1 + count_t[t[i]] = count_t.get(t[i], 0) + 1 + + for c in count_s: + if c not in count_t or count_s[c] != count_t[c]: + return False + return True From 5e38b0b1075517a098e51ad643f787c32fb90170 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 13:07:22 +0900 Subject: [PATCH 25/50] Solution: Valid Anagram - rewrite in cpp --- valid-anagram/flynn.cpp | 22 ++++++++++++++++++++++ valid-anagram/flynn.py | 25 ------------------------- 2 files changed, 22 insertions(+), 25 deletions(-) create mode 100644 valid-anagram/flynn.cpp delete mode 100644 valid-anagram/flynn.py diff --git a/valid-anagram/flynn.cpp b/valid-anagram/flynn.cpp new file mode 100644 index 000000000..02d98fa73 --- /dev/null +++ b/valid-anagram/flynn.cpp @@ -0,0 +1,22 @@ +/** + * For length of given strings N, + * + * Time complexity: O(N) + * - iteration for given strings + * + * Space complexity: O(1) + * - the size of the container `count` is constant + */ + +class Solution { +public: + bool isAnagram(string s, string t) { + int count[26] = {0}; + + for (auto c : s) count[c - 'a']++; + for (auto c : t) count[c - 'a']--; + + for (int i = 0; i < 26; i++) if (count[i]) return false; + return true; + } +}; \ No newline at end of file diff --git a/valid-anagram/flynn.py b/valid-anagram/flynn.py deleted file mode 100644 index b45d142a5..000000000 --- a/valid-anagram/flynn.py +++ /dev/null @@ -1,25 +0,0 @@ -''' -For N, length of the given strings, - -Time Complexity: O(N) -- first iteration: O(N) -- second iteration: O(N) - -Space Compelxity: O(N) -- dictionaries for each strings: O(N) -''' - -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - if len(s) != len(t): - return False - - count_s, count_t = {}, {} - for i in range(len(s)): - count_s[s[i]] = count_s.get(s[i], 0) + 1 - count_t[t[i]] = count_t.get(t[i], 0) + 1 - - for c in count_s: - if c not in count_t or count_s[c] != count_t[c]: - return False - return True From 0876fb5859ec926f6e93a182c7c453be962176c5 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 13:14:04 +0900 Subject: [PATCH 26/50] Chore: Valid Anagram - line break --- valid-anagram/flynn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/valid-anagram/flynn.cpp b/valid-anagram/flynn.cpp index 02d98fa73..b6df7007e 100644 --- a/valid-anagram/flynn.cpp +++ b/valid-anagram/flynn.cpp @@ -19,4 +19,4 @@ class Solution { for (int i = 0; i < 26; i++) if (count[i]) return false; return true; } -}; \ No newline at end of file +}; From 6f5a4f17f62e06e8842a9456546c3e8632367c9f Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 13:38:28 +0900 Subject: [PATCH 27/50] Solution: Counting Bits --- counting-bits/flynn.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 counting-bits/flynn.cpp diff --git a/counting-bits/flynn.cpp b/counting-bits/flynn.cpp new file mode 100644 index 000000000..5067bda98 --- /dev/null +++ b/counting-bits/flynn.cpp @@ -0,0 +1,22 @@ +/** + * Time complexity: O(N) + * + * Space complexity: O(1) + */ + +class Solution { +public: + vector countBits(int n) { + vector res; + res.push_back(0); + + int i = 1, j = 1; + while (i <= n) { + res.push_back(res[i - j] + 1); + i++; + if (i == j * 2) j *= 2; + } + + return res; + } +}; From 6b3f60e87aed825ae84ed590536186b17011c0a8 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 15:49:45 +0900 Subject: [PATCH 28/50] Solution: Encode and Decode Strings --- encode-and-decode-strings/flynn.cpp | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 encode-and-decode-strings/flynn.cpp diff --git a/encode-and-decode-strings/flynn.cpp b/encode-and-decode-strings/flynn.cpp new file mode 100644 index 000000000..a09391275 --- /dev/null +++ b/encode-and-decode-strings/flynn.cpp @@ -0,0 +1,58 @@ +/** + * Let's say sum of lengths of given strings N, and the length of the longest string M + * + * Encode + * - Time complexity: O(N) + * - Space complexity: O(1) + * + * Decode + * - Time complexity: O(N) + * - Space complexity: O(M) + */ + +class Codec { +public: + + // Encodes a list of strings to a single string. + string encode(vector& strs) { + string res = ""; + for (auto str : strs) { + res += to_string(str.size()); + res.push_back('.'); + res += str; + } + return res; + } + + // Decodes a single string to a list of strings. + vector decode(string s) { + vector res; + int str_size = 0; + string tmp = ""; + + auto it = s.begin(); + while (it != s.end()) { + do { + str_size = str_size * 10 + (*it - '0'); + it++; + } while (*it != '.'); + + it++; + + for (int i = 0; i < str_size; i++) { + tmp.push_back(*it); + it++; + } + + res.push_back(tmp); + str_size = 0; + tmp = ""; + } + + return res; + } +}; + +// Your Codec object will be instantiated and called as such: +// Codec codec; +// codec.decode(codec.encode(strs)); From 3e80c0b9508f54a48a0d1e681f3f0230a9ceae47 Mon Sep 17 00:00:00 2001 From: jeongdalma Date: Mon, 19 Aug 2024 17:22:59 +0900 Subject: [PATCH 29/50] =?UTF-8?q?2=EC=A3=BC=EC=B0=A8=20=EB=8B=B5=EC=95=88?= =?UTF-8?q?=20=EC=A0=9C=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jdalma.kt | 57 +++++++++++ counting-bits/jdalma.kt | 62 ++++++++++++ decode-ways/jdalma.kt | 95 +++++++++++++++++++ encode-and-decode-strings/jdalma.kt | 39 ++++++++ valid-anagram/jdalma.kt | 55 +++++++++++ 5 files changed, 308 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/jdalma.kt create mode 100644 counting-bits/jdalma.kt create mode 100644 decode-ways/jdalma.kt create mode 100644 encode-and-decode-strings/jdalma.kt create mode 100644 valid-anagram/jdalma.kt diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/jdalma.kt b/construct-binary-tree-from-preorder-and-inorder-traversal/jdalma.kt new file mode 100644 index 000000000..586423d14 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/jdalma.kt @@ -0,0 +1,57 @@ +package leetcode_study + +import io.kotest.matchers.equals.shouldBeEqual +import org.junit.jupiter.api.Test + +class `construct-binary-tree-from-preorder-and-inorder-traversal` { + + /** + * preorder : 현재(부모) 노드부터 왼쪽 자식 노드, 오른쪽 자식 노드 + * inorder : 왼쪽 자식 노드 부터 부모 노드, 오른쪽 자식 노드 + */ + fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? { + val inorderIndices = inorder.withIndex().associate { it.value to it.index } + return traversal(preorder, inorder, inorderIndices) + } + + /** + * preorder에서 조회한 부모 노드의 값은 inorder의 중간에 위치한다. + * 그 중간 위치 기준으로 왼쪽 노드, 오른쪽 노드로 분리하여 재귀적으로 탐색할 수 있다. + */ + private fun traversal( + preorder: IntArray, inorder: IntArray, inorderIndices: Map, + preStart: Int = 0, inStart: Int = 0, inEnd: Int = inorder.size - 1 + ): TreeNode? { + if (preStart > preorder.size - 1 || inStart > inEnd) { + println("preStart: $preStart, inStart: $inStart, inEnd: $inEnd --- return null") + return null + } + val value = preorder[preStart] + val rootIndexInInorder = inorderIndices[value]!! + + println("value: $value, preStart: $preStart, rootIndexInInorder: $rootIndexInInorder, inStart: $inStart, inEnd: $inEnd") + return TreeNode(value).apply { + this.left = traversal( + preorder, inorder, inorderIndices, + preStart + 1, inStart, rootIndexInInorder - 1 + ) + this.right = traversal( + preorder, inorder, inorderIndices, + preStart + rootIndexInInorder - inStart + 1, rootIndexInInorder + 1, inEnd + ) + } + } + + @Test + fun `전위 순회, 중위 순회 순서의 정수 배열을 기준으로 이진트리를 생성하여 반환한다`() { + val actual = buildTree(intArrayOf(3,9,20,15,7), intArrayOf(9,3,15,20,7))!! + val expect = TreeNode.of(3,9,20,null,null,15,7)!! + + actual shouldBeEqual expect + + val actual1 = buildTree(intArrayOf(3,9,8,10,20,15,7), intArrayOf(8,9,10,3,15,20,7))!! + val expect1 = TreeNode.of(3,9,20,8,10,15,7)!! + + actual1 shouldBeEqual expect1 + } +} diff --git a/counting-bits/jdalma.kt b/counting-bits/jdalma.kt new file mode 100644 index 000000000..13d93e7fb --- /dev/null +++ b/counting-bits/jdalma.kt @@ -0,0 +1,62 @@ +package leetcode_study + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test + +class `counting-bits` { + + fun countBits(n: Int): IntArray { + return usingDPAndLeastSignificantBit(n) + } + + // 1. 입력받은 정수만큼 순회하며 bit 카운트 + // 시간복잡도: O(n * log(n)), 공간복잡도: O(1) + private fun usingBinary(n: Int): IntArray { + fun binary(n: Int): Int { + var calc = n + var count = 0 + while(calc > 0) { + if (calc % 2 != 0) { + count++ + } + calc /= 2 + } + return count + } + + return (0 .. n).map { binary(it) }.toIntArray() + } + + // 2. MSB, 즉 최상위 비트를 활용하여 십진수가 두 배가 될때마다 MSB를 갱신하여 이전의 결과를 활용 + // 시간복잡도: O(n), 공간복잡도: O(n) + private fun usingDPAndMostSignificantBit(n: Int): IntArray { + val dp = IntArray(n + 1) + var msb = 1 + + for (index in 1 .. n) { + if (index == msb shl 1) { + msb = index + } + dp[index] = 1 + dp[index - msb] + } + + return dp + } + + // 3. 최하위 비트를 제거한 결과를 재활용한다. (최하위 비트를 제거한 결과) + (현재 십진수의 최하위비트) + // 시간복잡도: O(n), 공간복잡도: O(n) + private fun usingDPAndLeastSignificantBit(n: Int): IntArray { + val dp = IntArray(n + 1) + for (index in 1 .. n) { + dp[index] = dp[index shr 1] + (index and 1) + } + + return dp + } + + @Test + fun `정수가 주어지면 각 i(0 ~ i)에 대해 이진 표현에서 1의 개수를 저장하는 배열을 반환한다`() { + countBits(2) shouldBe intArrayOf(0,1,1) + countBits(5) shouldBe intArrayOf(0,1,1,2,1,2) + } +} diff --git a/decode-ways/jdalma.kt b/decode-ways/jdalma.kt new file mode 100644 index 000000000..75808315b --- /dev/null +++ b/decode-ways/jdalma.kt @@ -0,0 +1,95 @@ +package leetcode_study + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test + +class `decode-ways` { + + fun numDecodings(s: String): Int { + return usingOptimizedDP(s) + } + + /** + * 1. 문자열의 첫 인덱스부터 DFS로 확인하면서 결과를 증가시킨다. → 시간초과 + * 0부터 시작하는 문자열은 존재하지 않기에 바로 0으로 반환하고 그 뒤 숫자부터 DFS를 연이어 실행한다. + * 시간복잡도: O(2^n), 공간복잡도: O(n) + */ + private fun usingDfs(s: String): Int { + fun dfs(index: Int): Int = + if (index == s.length) 1 + else if (s[index] == '0') 0 + else if (index + 1 < s.length && (s[index] == '1' || (s[index] == '2' && s[index + 1] < '7')) ) + dfs(index + 1) + dfs(index + 2) + else dfs(index + 1) + + return dfs(0) + } + + /** + * 2. 1번 풀이에서 중복되는 연산에 top-down 방향으로 메모이제이션 적용 + * 시간복잡도: O(n), 공간복잡도: O(n) + */ + private fun usingMemoization(s: String): Int { + fun dfs(index: Int, mem: IntArray): Int { + println(index) + mem[index] = if (index == s.length) 1 + else if (s[index] == '0') 0 + else if (mem[index] != 0) mem[index] + else if (index + 1 < s.length && (s[index] == '1' || (s[index] == '2' && s[index + 1] < '7')) ) + dfs(index + 1, mem) + dfs(index + 2, mem) + else dfs(index + 1, mem) + + return mem[index] + } + return dfs(0, IntArray(s.length + 1) { 0 }) + } + + /** + * 3. 마지막 숫자부터 bottom-up 방향 DP + * 시간복잡도: O(n), 공간복잡도: O(n) + */ + private fun usingDP(s: String): Int { + val dp = IntArray(s.length + 1).apply { + this[s.length] = 1 + } + + (s.length - 1 downTo 0).forEach { index -> + if (s[index] == '0') dp[index] = 0 + else if(index + 1 < s.length && (s[index] == '1' || (s[index] == '2' && s[index + 1] < '7'))) + dp[index] = dp[index + 1] + dp[index + 2] + else dp[index] = dp[index + 1] + } + + return dp[0] + } + + /** + * 4. 배열을 사용하지 않고 DP 적용 + * 시간복잡도: O(n), 공간복잡도: O(1) + */ + private fun usingOptimizedDP(s: String): Int { + var (memo, result) = 0 to 1 + + (s.length - 1 downTo 0).forEach { index -> + var tmp = if (s[index] == '0') 0 else result + + if (index + 1 < s.length && (s[index] == '1' || (s[index] == '2' && s[index + 1] < '7'))) { + tmp += memo + } + memo = result + result = tmp + } + + return result + } + + @Test + fun `입력받은 문자열의 디코딩 가능한 경우의 수를 반환한다`() { + numDecodings("12") shouldBe 2 + numDecodings("226") shouldBe 3 + numDecodings("06") shouldBe 0 + numDecodings("1011") shouldBe 2 + numDecodings("10112266") shouldBe 8 + numDecodings("1025") shouldBe 2 + } +} diff --git a/encode-and-decode-strings/jdalma.kt b/encode-and-decode-strings/jdalma.kt new file mode 100644 index 000000000..6710b29a6 --- /dev/null +++ b/encode-and-decode-strings/jdalma.kt @@ -0,0 +1,39 @@ +package leetcode_study + +import io.kotest.matchers.equals.shouldBeEqual +import org.junit.jupiter.api.Test + +/** + * 인코딩과 디코딩을 해결할 때 구분자를 256개의 ASCII 문자 중 하나를 사용해야한다면 아래와 같은 방법을 사용할 수 있다. + * 시간복잡도: O(n), 공간복잡도: O(1) + */ +class `encode-and-decode-strings` { + + private val DELIMITER = ":" + + fun encode(strings: List): String { + return strings.joinToString(separator = "") { e -> "${e.length}$DELIMITER$e" } + } + + fun decode(string: String): List { + var index = 0 + val result = mutableListOf() + while (index < string.length) { + val delimiterIndex = string.indexOf(DELIMITER, startIndex = index) + val size = string.substring(index , delimiterIndex).toInt() + result.add(string.substring(delimiterIndex + 1, delimiterIndex + size + 1)) + index = delimiterIndex + size + 1 + } + return result + } + + @Test + fun `문자열 목록을 하나의 문자열로 인코딩한다`() { + encode(listOf("leet","co:de","l:o:v:e","you")) shouldBeEqual "4:leet5:co:de7:l:o:v:e3:you" + } + + @Test + fun `문자열을 문자열 목록으로 디코딩한다`() { + decode("4:leet5:co:de7:l:o:v:e3:you") shouldBeEqual listOf("leet","co:de","l:o:v:e","you") + } +} diff --git a/valid-anagram/jdalma.kt b/valid-anagram/jdalma.kt new file mode 100644 index 000000000..4c460228c --- /dev/null +++ b/valid-anagram/jdalma.kt @@ -0,0 +1,55 @@ +package leetcode_study + +import org.junit.jupiter.api.Test + +class `valid-anagram` { + + fun isAnagram(s: String, t: String): Boolean { + return usingArray(s, t) + } + + // 1. 두 문자열을 정렬하여 비교한다. + // 시간복잡도: O(n * log(n)), 공간복잡도: O(n) + private fun usingSort(s: String, t: String): Boolean { + val sorted1 = s.toCharArray().apply { this.sort() } + val sorted2 = t.toCharArray().apply { this.sort() } + + return sorted1.contentEquals(sorted2) + } + + // 2. 배열에 문자 수 가감 + // 시간복잡도: O(n), 공간복잡도: O(n) + private fun usingArray(s: String, t: String): Boolean { + if (s.length != t.length) { + return false + } + + /* 해시맵 사용 + val map: Map = mutableMapOf().apply { + (s.indices).forEach { index -> + this[s[index]] = this.getOrDefault(s[index], 0) + 1 + this[t[index]] = this.getOrDefault(t[index], 0) - 1 + } + } + return map.values.find { it > 0 } == null + */ + + return IntArray(26).apply { + for (index in s.indices) { + this[s[index] - 'a'] = this[s[index] - 'a'] + 1 + this[t[index] - 'a'] = this[t[index] - 'a'] - 1 + } + }.find { it > 0 } == null + } + + @Test + fun `입력받은 두 문자열이 애너그램이라면 참을 반환한다`() { + isAnagram("anagram", "nagaram") + isAnagram("test", "estt") + } + + @Test + fun `입력받은 두 문자열이 애너그램이 아니라면 거짓을 반환한다`() { + isAnagram("cat", "rat") + } +} From 1c880abd15194b94223591df042453165521f3b0 Mon Sep 17 00:00:00 2001 From: f-exuan21 Date: Mon, 12 Aug 2024 21:12:35 +0900 Subject: [PATCH 30/50] contains-duplicate solution --- contains-duplicate/f-exuan21.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 contains-duplicate/f-exuan21.java diff --git a/contains-duplicate/f-exuan21.java b/contains-duplicate/f-exuan21.java new file mode 100644 index 000000000..a43e3bf97 --- /dev/null +++ b/contains-duplicate/f-exuan21.java @@ -0,0 +1,15 @@ +// time : O(n) +// space : O(n) + +class Solution { + public boolean containsDuplicate(int[] nums) { + HashSet set = new HashSet<>(); + for(int i : nums) { + if(set.contains(i)) { + return true; + } + set.add(i); + } + return false; + } +} From b21432d05772424cb4701b4ef45c795522c800fe Mon Sep 17 00:00:00 2001 From: f-exuan21 Date: Mon, 12 Aug 2024 21:27:13 +0900 Subject: [PATCH 31/50] commit --- contains-duplicate/f-exuan21.java | 1 + 1 file changed, 1 insertion(+) diff --git a/contains-duplicate/f-exuan21.java b/contains-duplicate/f-exuan21.java index a43e3bf97..28ba431cd 100644 --- a/contains-duplicate/f-exuan21.java +++ b/contains-duplicate/f-exuan21.java @@ -13,3 +13,4 @@ public boolean containsDuplicate(int[] nums) { return false; } } + From 5a960f81d456e16e7886f8992573ac956c4f4fa9 Mon Sep 17 00:00:00 2001 From: A-Hyeon <71107963+f-exuan21@users.noreply.github.com> Date: Wed, 14 Aug 2024 23:08:50 +0900 Subject: [PATCH 32/50] Create f-exuan21.java number of 1 bits solutions --- number-of-1-bits/f-exuan21.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 number-of-1-bits/f-exuan21.java diff --git a/number-of-1-bits/f-exuan21.java b/number-of-1-bits/f-exuan21.java new file mode 100644 index 000000000..9c06c4329 --- /dev/null +++ b/number-of-1-bits/f-exuan21.java @@ -0,0 +1,16 @@ +// time : O(1) +// space : O(1) + +class Solution { + public int hammingWeight(int n) { + int count = 0; + + while(n != 0) { + if((n&1) == 1) count++; + n = n >> 1; + } + + return count; + } +} + From dbbe7b3da9016ffd4dda837687eb73108300e45e Mon Sep 17 00:00:00 2001 From: A-Hyeon <71107963+f-exuan21@users.noreply.github.com> Date: Wed, 14 Aug 2024 23:16:20 +0900 Subject: [PATCH 33/50] Create f-exuan21.java Top K Frequent Elements Solutions --- top-k-frequent-elements/f-exuan21.java | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 top-k-frequent-elements/f-exuan21.java diff --git a/top-k-frequent-elements/f-exuan21.java b/top-k-frequent-elements/f-exuan21.java new file mode 100644 index 000000000..8863d678d --- /dev/null +++ b/top-k-frequent-elements/f-exuan21.java @@ -0,0 +1,35 @@ + +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map map = new HashMap<>(); + for(int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + + PriorityQueue> queue = new PriorityQueue<>( + (a, b) -> Integer.compare(b.getValue(), a.getValue()) + ); + + for(Map.Entry entry : map.entrySet()) { + queue.offer(entry); + } + + int[] res = new int[k]; + + for(int i = 0; i < k; i++) { + res[i] = queue.poll().getKey(); + } + + return res; + } +} + +// time : O(n) + O(m log m) + O(k log m) = O(n + m*logm + k*logm) +// +// 최악의 경우 n log n 이 될 수 있음 +// space : O(m) + O(m) + O(k) = O(m + k) +// 최악의 경우 n + k 가 될 수 있음 + +// n : nums의 길이 +// m : nums에서 서로 다른 숫자의 개수 + From a069025b892e7c7cc8345588d5408507b93979cc Mon Sep 17 00:00:00 2001 From: A-Hyeon <71107963+f-exuan21@users.noreply.github.com> Date: Fri, 16 Aug 2024 22:06:38 +0900 Subject: [PATCH 34/50] Update f-exuan21.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 시간복잡도 공간복잡도 수정 --- top-k-frequent-elements/f-exuan21.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/top-k-frequent-elements/f-exuan21.java b/top-k-frequent-elements/f-exuan21.java index 8863d678d..412b6fe93 100644 --- a/top-k-frequent-elements/f-exuan21.java +++ b/top-k-frequent-elements/f-exuan21.java @@ -24,12 +24,16 @@ public int[] topKFrequent(int[] nums, int k) { } } -// time : O(n) + O(m log m) + O(k log m) = O(n + m*logm + k*logm) -// -// 최악의 경우 n log n 이 될 수 있음 -// space : O(m) + O(m) + O(k) = O(m + k) -// 최악의 경우 n + k 가 될 수 있음 - // n : nums의 길이 // m : nums에서 서로 다른 숫자의 개수 +// time : O(n) + O(m*logm) + O(k*logm) = O(n + m*logm + k*logm) +// 최악의 경우, nums 가 다 unique 하기 때문에 n == m == k 가 됨 +// 따라서, O(n*logn) + +// space : O(m) + O(m) + O(k) = O(m + k) +// 최악의 경우 n == m == k 가 됨 +// 따라서, O(n) + + + From cd946095401da76f00f76b669cdb57dea9593f5a Mon Sep 17 00:00:00 2001 From: A-Hyeon <71107963+f-exuan21@users.noreply.github.com> Date: Fri, 16 Aug 2024 22:07:39 +0900 Subject: [PATCH 35/50] Update f-exuan21.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit HashSet -> Set 수정 --- contains-duplicate/f-exuan21.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contains-duplicate/f-exuan21.java b/contains-duplicate/f-exuan21.java index 28ba431cd..acb71d219 100644 --- a/contains-duplicate/f-exuan21.java +++ b/contains-duplicate/f-exuan21.java @@ -3,7 +3,7 @@ class Solution { public boolean containsDuplicate(int[] nums) { - HashSet set = new HashSet<>(); + Set set = new HashSet<>(); for(int i : nums) { if(set.contains(i)) { return true; From f89d7e1e0cc27fddfa3dec9f8f34d042d4ce39c8 Mon Sep 17 00:00:00 2001 From: A-Hyeon <71107963+f-exuan21@users.noreply.github.com> Date: Fri, 16 Aug 2024 23:25:17 +0900 Subject: [PATCH 36/50] Create f-exuan21.java kth-smallest-element-in-a-bst solution --- kth-smallest-element-in-a-bst/f-exuan21.java | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/f-exuan21.java diff --git a/kth-smallest-element-in-a-bst/f-exuan21.java b/kth-smallest-element-in-a-bst/f-exuan21.java new file mode 100644 index 000000000..6672ab9c0 --- /dev/null +++ b/kth-smallest-element-in-a-bst/f-exuan21.java @@ -0,0 +1,50 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + + private int count = 0; + private TreeNode resultNode = null; + + public int kthSmallest(TreeNode root, int k) { + searchNode(root, k); + return resultNode.val; + } + + public void searchNode(TreeNode node, int k) { + + if(resultNode != null) return; + + if(node.left != null) { + searchNode(node.left, k); + } + + count++; + + if(count == k) { + resultNode = node; + return; + } + + if(node.right != null) { + searchNode(node.right, k); + } + } +} + +// n : 노드 개수 +// time : O(n) 최악의 경우 모든 노드를 탐색해야함 +// space : O(n) 최악의 경우 한 쪽으로 노드가 치우쳐져 있음 +// -> 재귀 호출이 이루어지므로 스택에 쌓임 -> 한 쪽으로 쏠려 있으면 트리의 높이가 n이 됨 (트리의 최대 높이가 스택의 최대 깊) \ No newline at end of file From b6eefac5e4eadd3d295156bc4149c8745ef22092 Mon Sep 17 00:00:00 2001 From: A-Hyeon <71107963+f-exuan21@users.noreply.github.com> Date: Fri, 16 Aug 2024 23:32:52 +0900 Subject: [PATCH 37/50] Update f-exuan21.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 주석 오타 수정 --- kth-smallest-element-in-a-bst/f-exuan21.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kth-smallest-element-in-a-bst/f-exuan21.java b/kth-smallest-element-in-a-bst/f-exuan21.java index 6672ab9c0..95433a739 100644 --- a/kth-smallest-element-in-a-bst/f-exuan21.java +++ b/kth-smallest-element-in-a-bst/f-exuan21.java @@ -47,4 +47,4 @@ public void searchNode(TreeNode node, int k) { // n : 노드 개수 // time : O(n) 최악의 경우 모든 노드를 탐색해야함 // space : O(n) 최악의 경우 한 쪽으로 노드가 치우쳐져 있음 -// -> 재귀 호출이 이루어지므로 스택에 쌓임 -> 한 쪽으로 쏠려 있으면 트리의 높이가 n이 됨 (트리의 최대 높이가 스택의 최대 깊) \ No newline at end of file +// -> 재귀 호출이 이루어지므로 스택에 쌓임 -> 한 쪽으로 쏠려 있으면 트리의 높이가 n이 됨 (트리의 최대 높이가 스택의 최대 깊이) From 8efcb4ff809fcbf81de02eb41ad3698ba1145c66 Mon Sep 17 00:00:00 2001 From: obzva Date: Mon, 19 Aug 2024 22:30:04 +0900 Subject: [PATCH 38/50] Fix: Encode and Decode - Big-O analysis --- encode-and-decode-strings/flynn.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/encode-and-decode-strings/flynn.cpp b/encode-and-decode-strings/flynn.cpp index a09391275..63305ef40 100644 --- a/encode-and-decode-strings/flynn.cpp +++ b/encode-and-decode-strings/flynn.cpp @@ -1,12 +1,12 @@ /** - * Let's say sum of lengths of given strings N, and the length of the longest string M + * For the number of given strings N, and the length of the longest string M, * * Encode * - Time complexity: O(N) * - Space complexity: O(1) * * Decode - * - Time complexity: O(N) + * - Time complexity: O(NM) * - Space complexity: O(M) */ From 911ee8ece8e3c004e9ec57823b4147f11ba24135 Mon Sep 17 00:00:00 2001 From: obzva Date: Tue, 20 Aug 2024 14:56:01 +0900 Subject: [PATCH 39/50] Solution: Construct Binary Tree from Preorder and Inorder Traversal --- .../flynn.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp b/construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp new file mode 100644 index 000000000..9f7593623 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/flynn.cpp @@ -0,0 +1,49 @@ +/** + * For the number of given nodes N, + * + * Time complexity: O(N) + * + * Space complexity: O(N) at worst + */ + +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* buildTree(vector& preorder, vector& inorder) { + unordered_map inorder_index_map; + stack tree_stack; + + for (int i = 0; i < inorder.size(); i++) inorder_index_map[inorder[i]] = i; + + TreeNode* root = new TreeNode(preorder[0]); + tree_stack.push(root); + + for (int i = 1; i < preorder.size(); i++) { + TreeNode* curr = new TreeNode(preorder[i]); + + if (inorder_index_map[curr->val] < inorder_index_map[tree_stack.top()->val]) { + tree_stack.top()->left = curr; + } else { + TreeNode* parent; + while (!tree_stack.empty() && inorder_index_map[curr->val] > inorder_index_map[tree_stack.top()->val]) { + parent = tree_stack.top(); + tree_stack.pop(); + } + parent->right = curr; + } + tree_stack.push(curr); + } + + return root; + } +}; From 8223d78c46bff3b3b72c2d18459eee0ab9b64406 Mon Sep 17 00:00:00 2001 From: obzva Date: Tue, 20 Aug 2024 15:57:12 +0900 Subject: [PATCH 40/50] Solution: Decode Ways --- decode-ways/flynn.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 decode-ways/flynn.cpp diff --git a/decode-ways/flynn.cpp b/decode-ways/flynn.cpp new file mode 100644 index 000000000..0009f0f31 --- /dev/null +++ b/decode-ways/flynn.cpp @@ -0,0 +1,29 @@ +/** + * For the length of the given string N, + * + * Time complexity: O(N) + * + * Space complexity: O(N) + */ + +class Solution { +public: + int numDecodings(string s) { + if (s[0] == '0') return 0; + + int memo[s.size() + 1]; + + fill(memo, memo + s.size() + 1, 0); + memo[0] = 1; + memo[1] = 1; + + for (int i = 2; i <= s.size(); i++) { + int s_i = i - 1; + if (s[s_i] != '0') memo[i] = memo[i - 1]; + int two_digits = stoi(s.substr(s_i - 1, 2)); + if (10 <= two_digits && two_digits <= 26) memo[i] += memo[i - 2]; + } + + return memo[s.size()]; + } +}; From 6eed7ba9130650906fb05554de64c3f6bcb1dcb9 Mon Sep 17 00:00:00 2001 From: obzva Date: Tue, 20 Aug 2024 16:39:31 +0900 Subject: [PATCH 41/50] Solution: optimize Decode Ways --- decode-ways/flynn.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/decode-ways/flynn.cpp b/decode-ways/flynn.cpp index 0009f0f31..05d9b6eec 100644 --- a/decode-ways/flynn.cpp +++ b/decode-ways/flynn.cpp @@ -27,3 +27,27 @@ class Solution { return memo[s.size()]; } }; + +/** + * Space complexity O(1) solution + */ + +// class Solution { +// public: +// int numDecodings(string s) { +// if (s[0] == '0') return 0; + +// int one_digit_memo = 1, two_digit_memo = 1; + +// for (int i = 1; i < s.size(); i++) { +// int tmp = 0; +// if (s[i] != '0') tmp = one_digit_memo; +// int two_digits = stoi(s.substr(i - 1, 2)); +// if (10 <= two_digits && two_digits <= 26) tmp += two_digit_memo; +// two_digit_memo = one_digit_memo; +// one_digit_memo = tmp; +// } + +// return one_digit_memo; +// } +// }; From 16f9fe54d75460c8f5cca35e852d8f4913796568 Mon Sep 17 00:00:00 2001 From: CodyMan0 Date: Tue, 20 Aug 2024 22:02:18 +0900 Subject: [PATCH 42/50] =?UTF-8?q?:bug:=20lineBreak=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/codyman0.py | 1 + 1 file changed, 1 insertion(+) diff --git a/contains-duplicate/codyman0.py b/contains-duplicate/codyman0.py index 28267b7bb..c98893955 100644 --- a/contains-duplicate/codyman0.py +++ b/contains-duplicate/codyman0.py @@ -3,6 +3,7 @@ """ # Time complexity : O(n) +# lineBreak : True class Solution: def containsDuplicate(self, nums: List[int]) -> bool: From 559dfdbeb6f63a6a74ba2d16fbbe9d3f9f9c11dd Mon Sep 17 00:00:00 2001 From: Borahm Lee Date: Tue, 20 Aug 2024 22:12:44 +0900 Subject: [PATCH 43/50] ci: add pr langauge labeler (#335) * ci: add pr langauge labeler * rename job * apply reviews (Integrate into integration workflow) * Rename job --- .github/workflows/integration.yaml | 74 ++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index cd481d9a9..cff96df39 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -9,3 +9,77 @@ jobs: steps: - uses: actions/checkout@v4 - uses: fernandrone/linelint@0.0.6 + + label-lang: + runs-on: ubuntu-latest + continue-on-error: true + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Create package.json + run: echo '{}' > package.json + + - name: Install dependencies + run: npm install @octokit/rest node-fetch + + - name: Detect languages and add labels + env: + GITHUB_TOKEN: ${{ github.token }} + PR_NUM: ${{ github.event.number }} + run: | + node --input-type=module -e " + import { Octokit } from '@octokit/rest'; + import path from 'path'; + import fetch from 'node-fetch'; + + const octokit = new Octokit({ + auth: process.env.GITHUB_TOKEN, + request: { fetch } + }); + + const extensionsToLanguages = { + js: 'js', + ts: 'ts', + py: 'py', + java: 'java', + kt: 'kotlin', + cpp: 'c++', + go: 'go', + exs: 'elixir', + swift: 'swift' + // 필요한 다른 확장자와 언어 매핑 추가 + }; + + async function run() { + const { data: files } = await octokit.pulls.listFiles({ + owner: process.env.GITHUB_REPOSITORY.split('/')[0], + repo: process.env.GITHUB_REPOSITORY.split('/')[1], + pull_number: process.env.PR_NUM, + }); + + const languages = new Set(); + files.forEach(file => { + const ext = path.extname(file.filename).slice(1); + if (extensionsToLanguages[ext]) { + languages.add(extensionsToLanguages[ext]); + } + }); + + if (languages.size > 0) { + await octokit.issues.addLabels({ + owner: process.env.GITHUB_REPOSITORY.split('/')[0], + repo: process.env.GITHUB_REPOSITORY.split('/')[1], + issue_number: process.env.PR_NUM, + labels: Array.from(languages), + }); + } + } + + run().catch(err => console.error(err)); + " From c04de2b488bed1980201f4ead75dcf2fe0baf203 Mon Sep 17 00:00:00 2001 From: CodyMan0 Date: Tue, 20 Aug 2024 22:11:38 +0900 Subject: [PATCH 44/50] =?UTF-8?q?:bug:=20lineBreak=20=EC=9E=AC=EC=8B=9C?= =?UTF-8?q?=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/codyman0.py | 1 - 1 file changed, 1 deletion(-) diff --git a/contains-duplicate/codyman0.py b/contains-duplicate/codyman0.py index c98893955..28267b7bb 100644 --- a/contains-duplicate/codyman0.py +++ b/contains-duplicate/codyman0.py @@ -3,7 +3,6 @@ """ # Time complexity : O(n) -# lineBreak : True class Solution: def containsDuplicate(self, nums: List[int]) -> bool: From a4213be27cea3c731d3b3f8fff55cf0612bde2c2 Mon Sep 17 00:00:00 2001 From: CodyMan0 Date: Tue, 20 Aug 2024 22:29:07 +0900 Subject: [PATCH 45/50] =?UTF-8?q?:bug:=20line=20break=20=EC=9E=AC=EC=8B=9C?= =?UTF-8?q?=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/codyman0.py | 1 + 1 file changed, 1 insertion(+) diff --git a/contains-duplicate/codyman0.py b/contains-duplicate/codyman0.py index 28267b7bb..2975c3a38 100644 --- a/contains-duplicate/codyman0.py +++ b/contains-duplicate/codyman0.py @@ -3,6 +3,7 @@ """ # Time complexity : O(n) +# class Solution: def containsDuplicate(self, nums: List[int]) -> bool: From c095286a52978c0df734e90a368eb98f66e9545f Mon Sep 17 00:00:00 2001 From: CodyMan0 Date: Tue, 20 Aug 2024 22:31:31 +0900 Subject: [PATCH 46/50] =?UTF-8?q?:bug:=20lineBreak=20=EC=9E=AC=EC=8B=9C?= =?UTF-8?q?=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/codyman0.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contains-duplicate/codyman0.py b/contains-duplicate/codyman0.py index 2975c3a38..1cc2b3e02 100644 --- a/contains-duplicate/codyman0.py +++ b/contains-duplicate/codyman0.py @@ -3,7 +3,7 @@ """ # Time complexity : O(n) -# + class Solution: def containsDuplicate(self, nums: List[int]) -> bool: @@ -13,4 +13,4 @@ def containsDuplicate(self, nums: List[int]) -> bool: return False if sortedArray[i] == sortedArray[i + 1] : return True - return False \ No newline at end of file + return False From 3abfd93dd063d8be5c2109acadb99c7bb8ec2cbe Mon Sep 17 00:00:00 2001 From: obzva Date: Wed, 21 Aug 2024 15:01:31 +0900 Subject: [PATCH 47/50] Refactor: Encode and Decode Strings --- encode-and-decode-strings/flynn.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/encode-and-decode-strings/flynn.cpp b/encode-and-decode-strings/flynn.cpp index 63305ef40..955572279 100644 --- a/encode-and-decode-strings/flynn.cpp +++ b/encode-and-decode-strings/flynn.cpp @@ -27,15 +27,16 @@ class Codec { // Decodes a single string to a list of strings. vector decode(string s) { vector res; - int str_size = 0; - string tmp = ""; auto it = s.begin(); while (it != s.end()) { - do { + int str_size = 0; + string tmp = ""; + + while (*it != '.') { str_size = str_size * 10 + (*it - '0'); it++; - } while (*it != '.'); + } it++; @@ -45,8 +46,6 @@ class Codec { } res.push_back(tmp); - str_size = 0; - tmp = ""; } return res; From 3eadda09115fee9445532d0dab6c0f391f8f8925 Mon Sep 17 00:00:00 2001 From: jeongdalma Date: Wed, 21 Aug 2024 15:28:37 +0900 Subject: [PATCH 48/50] =?UTF-8?q?=EB=B9=85=EC=98=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jdalma.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/jdalma.kt b/construct-binary-tree-from-preorder-and-inorder-traversal/jdalma.kt index 586423d14..d2327cb6d 100644 --- a/construct-binary-tree-from-preorder-and-inorder-traversal/jdalma.kt +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/jdalma.kt @@ -17,19 +17,18 @@ class `construct-binary-tree-from-preorder-and-inorder-traversal` { /** * preorder에서 조회한 부모 노드의 값은 inorder의 중간에 위치한다. * 그 중간 위치 기준으로 왼쪽 노드, 오른쪽 노드로 분리하여 재귀적으로 탐색할 수 있다. + * 시간복잡도: O(n), 공간복잡도: O(n) */ private fun traversal( preorder: IntArray, inorder: IntArray, inorderIndices: Map, preStart: Int = 0, inStart: Int = 0, inEnd: Int = inorder.size - 1 ): TreeNode? { if (preStart > preorder.size - 1 || inStart > inEnd) { - println("preStart: $preStart, inStart: $inStart, inEnd: $inEnd --- return null") return null } val value = preorder[preStart] val rootIndexInInorder = inorderIndices[value]!! - println("value: $value, preStart: $preStart, rootIndexInInorder: $rootIndexInInorder, inStart: $inStart, inEnd: $inEnd") return TreeNode(value).apply { this.left = traversal( preorder, inorder, inorderIndices, From b02d06f62dbd1d85ad9ea2d97bddd688eb6ef7d8 Mon Sep 17 00:00:00 2001 From: Hyunjun Jeong Date: Wed, 21 Aug 2024 15:29:02 +0900 Subject: [PATCH 49/50] Update encode-and-decode-strings/jdalma.kt Co-authored-by: Dale Seo <5466341+DaleSeo@users.noreply.github.com> --- encode-and-decode-strings/jdalma.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encode-and-decode-strings/jdalma.kt b/encode-and-decode-strings/jdalma.kt index 6710b29a6..724700236 100644 --- a/encode-and-decode-strings/jdalma.kt +++ b/encode-and-decode-strings/jdalma.kt @@ -20,7 +20,7 @@ class `encode-and-decode-strings` { val result = mutableListOf() while (index < string.length) { val delimiterIndex = string.indexOf(DELIMITER, startIndex = index) - val size = string.substring(index , delimiterIndex).toInt() + val size = string.substring(index, delimiterIndex).toInt() result.add(string.substring(delimiterIndex + 1, delimiterIndex + size + 1)) index = delimiterIndex + size + 1 } From 81d4e12911a3108dd0da8138ab09cef843213765 Mon Sep 17 00:00:00 2001 From: Borahm Lee Date: Thu, 22 Aug 2024 23:18:50 +0900 Subject: [PATCH 50/50] ci: add permissions for pr labels (#354) * ci: add permissions on contents and pull-requests * ci: remove catch block to recognize failure * ci: apply review --- .github/workflows/integration.yaml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index cff96df39..f924e0c28 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -13,6 +13,11 @@ jobs: label-lang: runs-on: ubuntu-latest continue-on-error: true + + permissions: + contents: write + pull-requests: write + steps: - name: Checkout code uses: actions/checkout@v4 @@ -81,5 +86,5 @@ jobs: } } - run().catch(err => console.error(err)); + run(); "