diff --git a/contains-duplicate/hwanminini.js b/contains-duplicate/hwanminini.js new file mode 100644 index 000000000..efe53211a --- /dev/null +++ b/contains-duplicate/hwanminini.js @@ -0,0 +1,10 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function(nums) { + return nums.length !== new Set(nums).size +}; diff --git a/kth-smallest-element-in-a-bst/hwanminini.js b/kth-smallest-element-in-a-bst/hwanminini.js new file mode 100644 index 000000000..1c9b30500 --- /dev/null +++ b/kth-smallest-element-in-a-bst/hwanminini.js @@ -0,0 +1,31 @@ +// 시간 복잡도: O(n log n) +// 공간 복잡도: O(n) + +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} k + * @return {number} + */ +var kthSmallest = function(root, k) { + const results = [] + + const dfs = (tree) => { + results.push(tree.val) + if (tree.left) dfs(tree.left) + if (tree.right) dfs(tree.right) + } + + dfs(root) + + results.sort((a,b) => a - b) + + return results[k-1] +}; diff --git a/number-of-1-bits/hwanmini.js b/number-of-1-bits/hwanmini.js new file mode 100644 index 000000000..dca0de297 --- /dev/null +++ b/number-of-1-bits/hwanmini.js @@ -0,0 +1,20 @@ +// 시간복잡도: O(log n) +// 공간복잡도: O(log n) + +const replaceZeroToEmptyString = (str) => str.replaceAll('0','') + + +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function(n) { + const binaryNum = n.toString(2) + const replacedNumber = replaceZeroToEmptyString(binaryNum) + return replacedNumber.length +}; + + +console.log(hammingWeight(11)); +console.log(hammingWeight(128)); +console.log(hammingWeight(2147483645)); diff --git a/palindromic-substrings/hwanminini.js b/palindromic-substrings/hwanminini.js new file mode 100644 index 000000000..53c4eb979 --- /dev/null +++ b/palindromic-substrings/hwanminini.js @@ -0,0 +1,35 @@ +// 시간 복잡도: O(n^3) +// 공간 복잡도: O(1) + +/** + * @param {string} s + * @return {number} + */ +var countSubstrings = function(s) { + let count = 0; + + let plusIndex = 0; + while (plusIndex !== s.length) { + for (let i = 0 ; i < s.length - plusIndex; i++) { + if (isValidSubstring(s, i, i + plusIndex)) count++ + } + + plusIndex++; + } + + return count; +}; + + +function isValidSubstring(s, left, right) { + while (left <= right) { + if (s[left] !== s[right]) return false; + + left++; + right--; + } + return true; +} + +console.log(countSubstrings("abc")); +console.log(countSubstrings("aaa")); diff --git a/top-k-frequent-elements/hwanmini.js b/top-k-frequent-elements/hwanmini.js new file mode 100644 index 000000000..2509f3c6d --- /dev/null +++ b/top-k-frequent-elements/hwanmini.js @@ -0,0 +1,30 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function(nums, k) { + const map = new Map() + + for (let i = 0; i < nums.length; i++) { + map.set(nums[i], (map.get(nums[i]) || 0) + 1); + } + + const buckets = Array.from({length: nums.length + 1}, () => []) + + for (const [num, frequency] of map.entries()) { + buckets[frequency].push(num); + } + + const result = []; + for (let i = buckets.length - 1; i >= 0 && result.length < k; i--) { + result.push(...buckets[i]); + } + + return result +}; + +console.log(topKFrequent([1,1,1,2,2,3],2))