-
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 #317 from taekwon-dev/main
[์คํ๊ถ] Week1 ๋ฌธ์ ํ์ด
- Loading branch information
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
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,22 @@ | ||
class Solution { | ||
/** | ||
* ์๊ฐ ๋ณต์ก๋: O(N) | ||
* - ์ต์ ์ ๊ฒฝ์ฐ ์ฃผ์ด์ง ๋ฐฐ์ด์ ๋ง์ง๋ง ์์๊น์ง ์ฒดํฌํด์ผ ํจ. | ||
* - ์ค๋ณต ๊ฒ์ฌ๋ฅผ ์ํด Set ์ ํํ ์ด์ | ||
* - O(1) ์ผ๋ก ํ์ ๊ฐ๋ฅ | ||
* - ๋ฐ์ดํฐ ์์ ๋ณด์ฅ ํ์ ์์ | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(N) | ||
* - ์ต์ ์ ๊ฒฝ์ฐ ์ฃผ์ด์ง ๋ฐฐ์ด์ ๋ชจ๋ ์์๊ฐ ์๋ฃ๊ตฌ์กฐ์ ์ ์ฅ๋๋ฏ๋ก O(N) | ||
*/ | ||
public boolean containsDuplicate(int[] nums) { | ||
Set<Integer> set = new HashSet<>(); | ||
for (int num: nums) { | ||
if (set.contains(num)) { | ||
return true; | ||
} | ||
set.add(num); | ||
} | ||
return false; | ||
} | ||
} |
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,58 @@ | ||
/** | ||
* ์ฌ๊ณ ์ ํ๋ฆ: | ||
* - Binary Search Tree & ์์? | ||
* - In-order ๋ฐฉ์์ผ๋ก ์ํ๋ฅผ ํด์ผ๊ฒ ๋ค. | ||
* - 1-indexed ์ด๋ฏ๋ก ๋ฆฌ์คํธ์ ์ฎ๊ธฐ๊ณ k๋ฒ์งธ ์ ๊ทผํ๋ค๋ฉด -1 ํด์ ์ ๊ทผํด์ผ๊ฒ ๋ค. | ||
* - ๊ทผ๋ฐ, In-order ์ํ ์ด๋ป๊ฒ ํ๋๋ผ? ใ ใ (์ฌ๊ท๋ก ํ๋ ๊ฒ ๊ฐ์๋ฐ..) >> ์ฌ๊ธฐ์ ๊ฒ์ (...) | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(N) | ||
* - BST ๋ชจ๋ ๋ ธ๋๋ฅผ ๋ค ์ํํด์ผ ํจ | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(N) | ||
* - ๋ด๊ฐ ํ์ฉํ ์คํ์ ๊ธฐ์ค์ผ๋ก ๋ณด๋ฉด, | ||
* - Stack ์ ์ต๋๋ก ๋ง์ด ๋ค์ด๊ฐ ์ ์๋ ์๋ BST ๋์ด | ||
* - BST ๋ ์ต์ ์ ๊ฒฝ์ฐ ํ ์ชฝ์ผ๋ก ์์ ํ ํธํฅ๋์ด ๋์ด๊ฐ N์ด ๋ ์๋ ์๊ณ , | ||
* - ๊ท ํ์ด ์ ๋ง๋ค๋ฉด (๋ง์น AVL, Red-Black) log N์ด ๋ ์ ์์ | ||
* - ๋ฐ๋ผ์ O(N) ์ด์ง๋ง, ํธ๋ฆฌ์ ๊ท ํ์ด ์ ๋ง๋ค๋ฉด O(log N) | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
public int kthSmallest(TreeNode root, int k) { | ||
Stack<TreeNode> stack = new Stack<>(); | ||
TreeNode current = root; | ||
int count = 0; | ||
|
||
while (current != null || !stack.isEmpty()) { | ||
while (current != null) { | ||
stack.push(current); | ||
current = current.left; | ||
} | ||
|
||
current = stack.pop(); | ||
count++; | ||
|
||
if (count == k) { | ||
return current.val; | ||
} | ||
|
||
current = current.right; | ||
} | ||
|
||
return -1; | ||
} | ||
} |
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,22 @@ | ||
class Solution { | ||
/** | ||
* ์๊ฐ ๋ณต์ก๋: O(log N) | ||
* - ์ฃผ์ด์ง ์๋ฅผ 2๋ก ๋๋๋ ํ์๋ฅผ ๋ฐ๋ณต (์ฃผ์ด์ง ์๊ฐ 0์ด ๋ ๋๊น์ง) | ||
* - ์ด๋ ๋ฐ๋๋ก ๋ณด๋ฉด 2๋ฅผ ๋ช ๋ฒ ๊ณฑํด์ผ N์ด ๋์ค๋์ง๋ฅผ ํ์ธํ๋ ๊ณผ์ ๊ณผ ๊ฐ์ (๋ก๊ทธ์ ์๋ฏธ..ใ ) | ||
* - ๋ ์๊ฐํด๋ณด๋๊น Binary Search Tree ์์ ํน์ ๊ฐ์ ์กฐํํ ๋์ ๊ฐ์ ํ๋ฆ์ด์๋ ๊ฒ ๊ฐ์! | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
* - ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํ์ง ์์ | ||
*/ | ||
public int hammingWeight(int n) { | ||
int result = 0; | ||
while (n >= 1) { | ||
if ((n % 2) == 1) { | ||
result++; | ||
} | ||
n /= 2; | ||
} | ||
|
||
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,37 @@ | ||
class Solution { | ||
/** | ||
* ์ฌ๊ณ ์ ํ๋ฆ: | ||
* - Map ์ด์ฉํด์ ๊ฐ ํค ๊ฐ ๋ณ๋ก ๋ช ๊ฐ๊ฐ ์๋์ง ์ ์ฅํ์. | ||
* - ์ผ๋จ ์ฃผ์ด์ง ๋ชจ๋ ๋ฐฐ์ด์ ํ์ธํด์ผ ํ๋ค. | ||
* - ์ ๋ ฌ์ด ํ์ํ๊ฒ ๋ค. | ||
* - Map์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ์ ํ๋๋ฐ, ํค ๊ฐ๋ ๊ฐ์ด ๋ฐ๋ผ์์ผ๊ฒ ๋ค? | ||
* - Comparable? Comparator? (ํ ... ์ด๊ฑฐ ์ด๋ป๊ฒ ํ๋๋ผ..) | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(NlogN) | ||
* - ArrayList sort() ๋ Merge Sort | ||
* - Merge Sort ๋ O(NlogN) | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(N) | ||
* - ์ต์ ์ ๊ฒฝ์ฐ ์ฃผ์ด์ง ๋ฐฐ์ด์ ๋ชจ๋ ์์๊ฐ ์๋ฃ๊ตฌ์กฐ์ ์ ์ฅ๋๋ฏ๋ก O(N) | ||
*/ | ||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
for (int num: nums) { | ||
map.put(num, map.getOrDefault(num, 0) + 1); | ||
} | ||
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet()); | ||
list.sort(new Comparator<Map.Entry<Integer, Integer>>() { | ||
@Override | ||
public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) { | ||
return e2.getValue().compareTo(e1.getValue()); | ||
} | ||
}); | ||
|
||
int[] result = new int[k]; | ||
for (int i = 0; i < k; i++) { | ||
result[i] = list.get(i).getKey(); | ||
} | ||
|
||
return result; | ||
} | ||
} |