Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[환미니니] Week1 문제풀이 #325

Merged
merged 12 commits into from
Aug 18, 2024
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
};
29 changes: 29 additions & 0 deletions kth-smallest-element-in-a-bst/hwanminini.js
Copy link
Contributor

@naringst naringst Aug 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 문제 이렇게 작성했을 때 올바르게 동작하나요?! root.left가 배열로 나오는 경우들이 있는데, 그런 경우들에는 동작하지 않을 것 같아 질문드립니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존에 console.log 삭제하면서 sort가 삭제되어서 없어서 추가했고
leetcode 제출 시 문제는 없었던 거 같아요..!

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 시간 복잡도: O(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)

return results[k-1]
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마지막에 line break 없는 파일이 메인 브랜치에 유입되지 않도록 각별히 주의바랍니다. (참고: #334)

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
gitsunmin marked this conversation as resolved.
Show resolved Hide resolved
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))