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 2 Solution #360

Merged
merged 4 commits into from
Aug 25, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 시간복잡도: O(n2)
// 공간복잡도: O(n)
Copy link
Contributor

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.

이 부분은 헷갈려서 다른 분 시간복잡도를 참고했습니다.


var buildTree = function(preorder, inorder) {
if (!preorder.length || !inorder.length) return null;

const root = new TreeNode(preorder[0]);
const mid = inorder.indexOf(root.val);

root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));

return root;
};
21 changes: 21 additions & 0 deletions counting-bits/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 시간복잡도 O(n * log n)
// 공간복잡도 O(n)

/**
* @param {number} n
* @return {number[]}
*/
var countBits = function(n) {
const result = []

for (let i = 0 ; i <= n; i++) {
const binaryNumber = i.toString(2)
const oneLength = binaryNumber.replaceAll('0','').length

result.push(oneLength)
}

return result
};

console.log(countBits(5))
28 changes: 28 additions & 0 deletions valid-anagram/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 시간복잡도: O(n)
// 공간복잡도: O(k)
Copy link
Contributor

Choose a reason for hiding this comment

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

k가 무엇을 의미하나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

저장되는 문자열 개수를 뜻했는데 어처피 문자는 정해져있으니 O(1)이 될 수도 있겠네요..!


/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
if (s.length !== t.length) return false

const checkMap = new Map();

for (let i = 0; i < s.length; i++) {
checkMap.set(s[i], (checkMap.get(s[i]) || 0) + 1)
}


for (let j = 0; j < t.length; j++) {
if (!checkMap.get(t[j]) || checkMap.get(t[j]) < 0) return false
checkMap.set(t[j], (checkMap.get(t[j]) || 0 ) - 1);
}

return true
};


console.log(isAnagram("anagram","nagaram"))
Loading