Skip to content

Commit

Permalink
Merge pull request #325 from JEONGHWANMIN/main
Browse files Browse the repository at this point in the history
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week1 ๋ฌธ์ œํ’€์ด
  • Loading branch information
JEONGHWANMIN authored Aug 18, 2024
2 parents bdf08b0 + 074bd58 commit e55594c
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
10 changes: 10 additions & 0 deletions contains-duplicate/hwanminini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
return nums.length !== new Set(nums).size
};
31 changes: 31 additions & 0 deletions kth-smallest-element-in-a-bst/hwanminini.js
Original file line number Diff line number Diff line change
@@ -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]
};
20 changes: 20 additions & 0 deletions number-of-1-bits/hwanmini.js
Original file line number Diff line number Diff line change
@@ -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));
35 changes: 35 additions & 0 deletions palindromic-substrings/hwanminini.js
Original file line number Diff line number Diff line change
@@ -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"));
30 changes: 30 additions & 0 deletions top-k-frequent-elements/hwanmini.js
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit e55594c

Please sign in to comment.