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

[재호] WEEK 01 solutions #297

Merged
merged 5 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions contains-duplicate/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// TC: O(N)
// SC: O(N)

/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function (nums) {
return nums.length !== new Set(nums).size;
};
41 changes: 41 additions & 0 deletions kth-smallest-element-in-a-bst/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// TC: O(N)
// SC: 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) {
let result = null;

dfs(root);

return result;

function dfs(current) {
if (result !== null) {
return;
}
if (!current) {
return;
}

dfs(current.left);
if (current.val >= 0) {
if (k === 1) {
result = current.val;
}
k -= 1;
}
Comment on lines +33 to +38
Copy link
Contributor

@bky373 bky373 Aug 16, 2024

Choose a reason for hiding this comment

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

따로 리스트를 사용하지 않으신 부분이 인상 깊네요! 고생하셨습니다~ 👍

dfs(current.right);
}
};
13 changes: 13 additions & 0 deletions number-of-1-bits/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// TC: O(log n)
// SC: O(1)

/**
* @param {number} n
* @return {number}
*/
var hammingWeight = function (n) {
return n
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 hamming weight가 뭔지 몰랐는데, Brian Kernighan's Algorithm 라는 방법도 있더라고요 참고해보시면 도움되실 것 같습니다

.toString(2)
.split("")
.filter((s) => s === "1").length;
};
32 changes: 32 additions & 0 deletions palindromic-substrings/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// TC: O(n^3)
// SC: O(1)

/**
* @param {string} s
* @return {number}
*/
var countSubstrings = function(s) {
let count = 0;

for (let left = 0; left < s.length; left++) {
for (let right = left; right < s.length; right++) {
if(checkIsPalinDrome(left, right)) {
count += 1;
}
}
}

return count;

function checkIsPalinDrome(left, right) {
while (left < right) {
if (s[left] !== s[right]) {
return false;
}
left += 1;
right -= 1;
}

return true;
}
};
19 changes: 19 additions & 0 deletions top-k-frequent-elements/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TC: O(n logn)
// SC: O(N)

/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function (nums, k) {
const numAndCountBoard = nums.reduce((result, current) => {
result[current] = result.hasOwnProperty(current) ? result[current] + 1 : 1;
return result;
}, {});

return Object.entries(numAndCountBoard)
.sort((a, b) => b[1] - a[1])
.slice(0, k)
.map((element) => element[0]);
};