-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from taekwon-dev/main
[์คํ๊ถ] Week2 ๋ฌธ์ ํ์ด
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
construct-binary-tree-from-preorder-and-inorder-traversal/taekwon-dev.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |