From 2706dea2a1f54f3686c96db58791c967fad41b1a Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Mon, 12 Aug 2024 04:13:27 +0900 Subject: [PATCH 1/6] Contains Duplicate --- contains-duplicate/sunjae95.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 contains-duplicate/sunjae95.js diff --git a/contains-duplicate/sunjae95.js b/contains-duplicate/sunjae95.js new file mode 100644 index 000000000..ecea62667 --- /dev/null +++ b/contains-duplicate/sunjae95.js @@ -0,0 +1,22 @@ +/** + * @description + * time complexity: O(n) + * space complexity: O(n) + * approach/strategy: + * 1. brute force + hash table + */ + +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function (nums) { + const hashTable = new Set(); + + for (const num of nums) { + if (hashTable.has(num)) return true; + hashTable.add(num); + } + + return false; +}; From cafa20cce91aa5d56d5168755c83ddf108cb33c2 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Mon, 12 Aug 2024 05:04:11 +0900 Subject: [PATCH 2/6] Number of 1Bits --- number-of-1-bits/sunjae95.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 number-of-1-bits/sunjae95.js diff --git a/number-of-1-bits/sunjae95.js b/number-of-1-bits/sunjae95.js new file mode 100644 index 000000000..3d6ef7f2b --- /dev/null +++ b/number-of-1-bits/sunjae95.js @@ -0,0 +1,22 @@ +/** + * @description + * time complexity: O(logN) + * space complexity: O(1) + * approach/strategy: + * 1. decimal to binary + */ + +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function (n) { + let answer = 0; + + while (n > 0) { + answer += n % 2; + n = Math.floor(n / 2); + } + + return answer; +}; From e56eacb21569c6abb114bcff1775a4f00c77a952 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Mon, 12 Aug 2024 17:58:24 +0900 Subject: [PATCH 3/6] Top K Frequent Elements --- top-k-frequent-elements/sunjae95.js | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 top-k-frequent-elements/sunjae95.js diff --git a/top-k-frequent-elements/sunjae95.js b/top-k-frequent-elements/sunjae95.js new file mode 100644 index 000000000..b760132be --- /dev/null +++ b/top-k-frequent-elements/sunjae95.js @@ -0,0 +1,31 @@ +/** + * @description + * time complexity: O(N logN) + * space complexity: O(N) + * + * brainstorming: + * 1. javascript sort method + * 2. priority queue + * + * strategy: + * javascript sort method + * + * reason: + * javascript sort method is easier to implement. + */ + +var topKFrequent = function (nums, k) { + const answer = []; + const array = []; + const hashTable = new Map(); + + nums.forEach((num) => hashTable.set(num, (hashTable.get(num) ?? 0) + 1)); + + hashTable.forEach((count, number) => array.push({ number, count })); + + array.sort((a, b) => b.count - a.count); + + for (let i = 0; i < k; i++) answer.push(array[i].number); + + return answer; +}; From af07ac3276675be799fef31729ea466773eb3e5c Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Mon, 12 Aug 2024 19:03:20 +0900 Subject: [PATCH 4/6] Kth Smallest Element in a Bst --- kth-smallest-element-in-a-bst/sunjae95.js | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/sunjae95.js diff --git a/kth-smallest-element-in-a-bst/sunjae95.js b/kth-smallest-element-in-a-bst/sunjae95.js new file mode 100644 index 000000000..cbf4996f7 --- /dev/null +++ b/kth-smallest-element-in-a-bst/sunjae95.js @@ -0,0 +1,33 @@ +/** + * @description + * time complexity: O(N) + * space complexity: O(N) + * + * brainstorming: + * 1. BFS, DFS + * 2. Brute force + * + * strategy: + * inOrder search + * + * reason: + * tree features + */ +var kthSmallest = function (root, k) { + let answer = 0; + + inOrder(root, (value) => { + k -= 1; + if (k > 0) return false; + if (k === 0) answer = value; + return true; + }); + + return answer; +}; + +function inOrder(tree, isEnd) { + if (tree.left) inOrder(tree.left, isEnd); + if (isEnd(tree.val)) return; + if (tree.right) inOrder(tree.right, isEnd); +} From 30eb1b15ef594d74645068c7b980130defe4288b Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Thu, 15 Aug 2024 08:59:02 +0900 Subject: [PATCH 5/6] Palindromic Substrings --- palindromic-substrings/sunjae95.js | 90 ++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 palindromic-substrings/sunjae95.js diff --git a/palindromic-substrings/sunjae95.js b/palindromic-substrings/sunjae95.js new file mode 100644 index 000000000..1e3598be0 --- /dev/null +++ b/palindromic-substrings/sunjae95.js @@ -0,0 +1,90 @@ +/** + * @description + * time complexity: O(N^3) + * space complexity: O(N) + * + * brainstorming: + * 1. stack, permutation + * 2. Brute force + * + * strategy: + * Brute force, calculate + * + * reason: + * intuitive way + * + * @param {string} s + * @return {number} + */ +var countSubstrings = function (s) { + let answer = 0; + const len = s.length; + + for (let i = 0; i < len; i++) { + for (let j = i + 1; j < len + 1; j++) { + const subStr = s.slice(i, j); + if (isPalindrome(subStr)) answer++; + } + } + + return answer; +}; + +function isPalindrome(str) { + const len = str.length; + const middleIndex = Math.floor(len / 2); + + for (let i = 0; i < middleIndex; i++) { + if (str[i] !== str[len - 1 - i]) return false; + } + + return true; +} + +/** + * @description + * time complexity: O(N^2) + * space complexity: O(N^2) + * + * brainstorming: + * 1. https://sbslc.tistory.com/56 + * + * strategy: + * dynamic programming + * + * reason: + * to challenge dp + * + * @param {string} s + * @return {number} + */ +var countSubstrings = function (s) { + const answer = []; + const MAX_LENGTH = s.length; + const dp = Array.from({ length: MAX_LENGTH }, (_, i) => + Array.from({ length: MAX_LENGTH }, (_, j) => { + if (i === j) answer.push(s[i]); + return i === j; + }) + ); + // Check next step ex) aa, bb, cc + for (let i = 0; i < MAX_LENGTH - 1; i++) { + const nextIndex = i + 1; + if (s[i] === s[nextIndex]) { + dp[i][nextIndex] = true; + answer.push(s.slice(i, nextIndex + 1)); + } + } + // Check against values calculated in the previous step + for (let len = 2; len <= MAX_LENGTH; len++) { + for (let i = 0; i < MAX_LENGTH - len; i++) { + const lastIndex = len + i; + if (s[i] === s[lastIndex] && dp[i + 1][lastIndex - 1]) { + dp[i][lastIndex] = true; + answer.push(s.slice(i, lastIndex + 1)); + } + } + } + + return answer.length; +}; From 81fdc178003b69dc463cefa6d2ceb220311c307c Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Thu, 15 Aug 2024 17:39:06 +0900 Subject: [PATCH 6/6] =?UTF-8?q?top-k-frequent-elements=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- top-k-frequent-elements/sunjae95.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/top-k-frequent-elements/sunjae95.js b/top-k-frequent-elements/sunjae95.js index b760132be..2d449ff44 100644 --- a/top-k-frequent-elements/sunjae95.js +++ b/top-k-frequent-elements/sunjae95.js @@ -16,16 +16,14 @@ var topKFrequent = function (nums, k) { const answer = []; - const array = []; const hashTable = new Map(); nums.forEach((num) => hashTable.set(num, (hashTable.get(num) ?? 0) + 1)); - hashTable.forEach((count, number) => array.push({ number, count })); + hashTable.forEach((count, number) => answer.push({ number, count })); - array.sort((a, b) => b.count - a.count); - - for (let i = 0; i < k; i++) answer.push(array[i].number); - - return answer; + return answer + .sort((a, b) => b.count - a.count) + .slice(0, k) + .map(({ number }) => number); };