Skip to content

[선재] Week1 문제 풀이 #306

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

Merged
merged 7 commits into from
Aug 16, 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
22 changes: 22 additions & 0 deletions contains-duplicate/sunjae95.js
Original file line number Diff line number Diff line change
@@ -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;
};
33 changes: 33 additions & 0 deletions kth-smallest-element-in-a-bst/sunjae95.js
Original file line number Diff line number Diff line change
@@ -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);
}
22 changes: 22 additions & 0 deletions number-of-1-bits/sunjae95.js
Original file line number Diff line number Diff line change
@@ -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;
};
90 changes: 90 additions & 0 deletions palindromic-substrings/sunjae95.js
Original file line number Diff line number Diff line change
@@ -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;
};
29 changes: 29 additions & 0 deletions top-k-frequent-elements/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @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.
Comment on lines +13 to +14
Copy link
Member

Choose a reason for hiding this comment

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

이렇게 이유를 남겨주시니 완전 납득이 되네요 ㅋㅋㅋ
다음 주차까지 아직 시간이 많으니 brainstorming하셨던 priority queue를 사용해서도 풀어보시면 좋을 것 같습니다. 물론 현실 프로젝트에서는 간단하고 읽기 쉬운 코드가 더 선호되지만, 코딩 테스트에서는 결국 더 효율적인 알고리즘을 제시하는 것이 목표이니까요.

*/

var topKFrequent = function (nums, k) {
const answer = [];
const hashTable = new Map();

nums.forEach((num) => hashTable.set(num, (hashTable.get(num) ?? 0) + 1));

hashTable.forEach((count, number) => answer.push({ number, count }));

return answer
.sort((a, b) => b.count - a.count)
.slice(0, k)
.map(({ number }) => number);
};