Skip to content

Commit

Permalink
Merge pull request #317 from taekwon-dev/main
Browse files Browse the repository at this point in the history
[์œคํƒœ๊ถŒ] Week1 ๋ฌธ์ œ ํ’€์ด
  • Loading branch information
taekwon-dev authored Aug 18, 2024
2 parents df078a5 + ca2017a commit 0748714
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
22 changes: 22 additions & 0 deletions contains-duplicate/taekwon-dev.java
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;
}
}
58 changes: 58 additions & 0 deletions kth-smallest-element-in-a-bst/taekwon-dev.java
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;
}
}
22 changes: 22 additions & 0 deletions number-of-1-bits/taekwon-dev.java
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;
}
}
37 changes: 37 additions & 0 deletions top-k-frequent-elements/taekwon-dev.java
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;
}
}

0 comments on commit 0748714

Please sign in to comment.