Skip to content

[솔방울] week 1 문제 풀이 #312

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 5 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
7 changes: 7 additions & 0 deletions contains-duplicate/wooseok123.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// TC : O(n) | SC : O(n)

function containsDuplicate(nums) {
let original_length = nums.length;
let modified_length = new Set(nums).size;
return original_length !== modified_length;
}
19 changes: 19 additions & 0 deletions kth-smallest-element-in-a-bst/wooseok123.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TC : O(n log n) | SC : O(n)

let findAllValuesInTree = (root, obj) => {
obj[root.val] = true;
if (!root.left && !root.right) return obj;
if (root.left) findAllValuesInTree(root.left, obj);
if (root.right) findAllValuesInTree(root.right, obj);

return obj;
};

var kthSmallest = function (root, k) {
const obj = findAllValuesInTree(root, {});
const sortedList = Object.keys(obj)
.map(Number)
.sort((a, b) => a - b);
Comment on lines +13 to +16
Copy link
Contributor

@bky373 bky373 Aug 15, 2024

Choose a reason for hiding this comment

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

안녕하세요, 사소한 리뷰 글 남깁니다~
풀이를 보았을 때 재귀적으로 트리를 순회하여 모든 노드의 값을 객체에 저장한 후,
정렬을 위해 한 번 더 모든 값을 순회하는 것으로 이해하였습니다.

시간 복잡도의 차이는 없겠지만, 순회하는 단계를 1회로 줄여보실 수도 있을 것 같네요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

아하 BST에서 중위 순회를 하는 경우 이미 오름차순으로 정렬되기 때문에 추가로 순회하는 단계가 생략될 수 있다는 말씀이실까요!?


return sortedList[k - 1];
};
34 changes: 34 additions & 0 deletions number-of-1-bits/wooseok123.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
let hammingWeight = function (n) {
return DecToBinAndGetSetBits(n);
};

// TC : O(log n) | SC : O(1)

let DecToBinAndGetSetBits = (n) => {
let targetNum = n;
let result = 0;
while (targetNum > 0) {
let remainder = targetNum % 2;
if (remainder === 1) result += 1;
targetNum = parseInt(targetNum / 2);
}
return result;
};

// TC : O(log n) | SC : O(log n)
// 근데 사실 split 메서드 자체는 o(n)인데, toString과정을 통해 log(n)의 개수만큼 나와버린 것이면 o(log n)이라고 표기해도 되는걸까?
Copy link
Member

Choose a reason for hiding this comment

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

넹, 상관없을 것 같습니다. 내장 함수도 직접 구현하신 코드처럼 시간을 소모하니까요 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

아하 애매한 부분이었는데 감사합니다 :)


// let DecToBinAndGetSetBits = (n) => {
// let target = n;
// let bin = n.toString(2);
// return bin.split("").filter((el) => el == 1).length
// }

// TC : O(log n) | SC : O(log n)

// let DecToBinAndGetSetBits = (n) => {
// let target = n;
// let bin = n.toString(2);
// let matches = bin.match(/1/g);
// return matches.length
// }
24 changes: 24 additions & 0 deletions palindromic-substrings/wooseok123.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var countSubstrings = function (s) {
let result = 0;
// 개수를 키워나가며, 각 자리가 대칭을 이루는지 검사한다.

// substring의 개수 설정
for (let i = 0; i < s.length; i++) {
// 시작점 설정
for (let j = 0; j < s.length - i; j++) {
let isPalindromic = true;
// 대칭되는 요소를 하나씩 비교
for (let k = j; k < Math.ceil((j * 2 + i) / 2); k++) {
if (s[k] !== s[j * 2 + i - k]) {
isPalindromic = false;
break;
}
}
if (isPalindromic) result += 1;
}
}

return result;
};

// TC : o(n^3) | SC : o(1)
15 changes: 15 additions & 0 deletions top-k-frequent-elements/wooseok123.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// TC : o(n log n) | SC : o(n)

var topKFrequent = function (nums, k) {
const elements = countElments(nums);
const keys = Object.keys(elements).sort((a, b) => elements[b] - elements[a]);
return keys.slice(0, k);
};

let countElments = (nums) => {
const temp = {};
for (const num of nums) {
temp[num] = (count[num] || 0) + 1;
}
return temp;
};