Skip to content

Commit

Permalink
Merge pull request #353 from taekwon-dev/main
Browse files Browse the repository at this point in the history
[์œคํƒœ๊ถŒ] Week2 ๋ฌธ์ œ ํ’€์ด
  • Loading branch information
taekwon-dev authored Aug 25, 2024
2 parents 63b996b + a0fc24d commit f02b7bf
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* https://www.algodale.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
*
* <ํ’€์ด1 ๊ธฐ์ค€> ์‹œ๋„!
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N^2) - Java ์—์„œ๋Š” Call-by-value, ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ธ์ž ๊ฐ’์ด ๋ณต์‚ฌ๋˜์–ด ์ „๋‹ฌ๋จ / ๋ฐฐ์—ด ๊ธธ์ด์— ๋”ฐ๋ผ์„œ ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ์ด ์ ์  ๊นŠ์–ด์ง (๊ฐ ํ˜ธ์ถœ๋งˆ๋‹ค ์„œ๋ธŒ ๋ฐฐ์—ด ์ƒ์„ฑ)
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: inorder ๋ฐฐ์—ด์—์„œ root ๋…ธ๋“œ๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๊ณผ์ •์ด O(N), ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ์žฌ๊ท€ ํ˜ธ์ถœ๋งˆ๋‹ค ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ O(N^2)
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTree(preorder, inorder, 0, 0, inorder.length - 1);
}

private TreeNode buildTree(int[] preorder, int[] inorder, int rootIdx, int inorderStartIdx, int inorderEndIdx) {
if (inorderStartIdx > inorderEndIdx) {
return null;
}

int rootVal = preorder[rootIdx];
TreeNode root = new TreeNode(rootVal);

int rootIdxOnInorder = 0;
for (int i = inorderStartIdx; i <= inorderEndIdx; i++) {
if (inorder[i] == rootVal) {
rootIdxOnInorder = i;
break;
}
}

int leftLength = rootIdxOnInorder - inorderStartIdx;

root.left = buildTree(preorder, inorder, rootIdx + 1, inorderStartIdx, rootIdxOnInorder - 1);
root.right = buildTree(preorder, inorder, rootIdx + 1 + leftLength, rootIdxOnInorder + 1, inorderEndIdx);

return root;
}
}
19 changes: 19 additions & 0 deletions counting-bits/taekwon-dev.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N)
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N)
* - ๋งŒ์•ฝ 1 ~ N ๊นŒ์ง€ ๊ฐ ์ˆ˜์— ๋Œ€ํ•ด์„œ ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ–ˆ๋‹ค๋ฉด? - O(NlogN)
* Note:
* - a >> b: a ์˜ ์ด์ง„ ํ‘œํ˜„์„ ์˜คํ”ˆ์ชฝ์œผ๋กœ b ๋น„ํŠธ๋งŒํผ ์ด๋™
* - i >> 1: a ์˜ ์ด์ง„ ํ‘œํ˜„์—์„œ ๊ฐ€์žฅ ์˜คํ”ˆ์ชฝ ํ•˜๋‚˜๊ฐ€ ์ž˜๋ฆผ (i๋Š” ์–‘์ˆ˜ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ, ์™ผ์ชฝ์€ 0์œผ๋กœ ์ฑ„์›Œ์ง)
* - a & b: AND ์—ฐ์‚ฐ
* - i & 1: ์ด์ „ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋ฉ”๋ชจ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ๋‚ด๊ฐ€ ๊ถ๊ธˆํ•œ ๊ฒƒ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ ์ˆ˜ ๊ฐ’์ด 1์ธ์ง€ ์—ฌ๋ถ€.
*/
class Solution {
public int[] countBits(int n) {
int[] result = new int[n + 1];
for (int i = 1; i <= n; i++) {
result[i] = result[i >> 1] + (i & 1);
}
return result;
}
}
28 changes: 28 additions & 0 deletions valid-anagram/taekwon-dev.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* ์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋“ค์—ˆ๋˜ ์ƒ๊ฐ: ์ •๋ ฌ ์‹œ์ผœ์„œ ๊ฐ™์œผ๋ฉด anagram?
* ๊ทผ๋ฐ, ์ •๋ ฌ ์‹œ์ผœ์„œ ๊ฐ™๋‹ค๋ฉด, ๊ฒฐ๊ตญ ๋ฌธ์ž์—ด์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ ๋ฌธ์ž์˜ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ™๋‹ค.
*
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
*/
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;

int[] charCount = new int[26];

for (int i = 0; i < s.length(); i++) {
charCount[s.charAt(i) - 'a']++;
}

for (int i = 0; i < t.length(); i++) {
int index = t.charAt(i) - 'a';
charCount[index]--;
if (charCount[index] < 0) {
return false;
}
}

return true;
}
}

0 comments on commit f02b7bf

Please sign in to comment.