Skip to content
Open
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
54 changes: 54 additions & 0 deletions binary-tree-level-order-traversal/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: BFS
  • 설명: 이 코드는 큐를 활용한 너비 우선 탐색(BFS)로 트리의 각 레벨을 순회하며 노드를 방문합니다. 레벨별로 노드를 그룹화하는 방식이 BFS의 대표적 특징입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 큐를 사용하여 각 레벨의 노드를 순차적으로 방문하므로 노드 수에 비례하는 시간과 공간이 소요됩니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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 List<List<Integer>> levelOrder(TreeNode root) {
/**
1.문제: 레벨순으로 왼쪽에서 오른쪽으로 노드 순회
2.constraints: num of nodes min = 0, max = 2000
3.solution: bfs, queue
-time complexity: O(n) - 노드 수
-space complexity: O(n) - 전체 노드를 저장하므로
*/
List<List<Integer>> answer = new ArrayList<>();

if(root == null) {
return new ArrayList<>();
}

Queue<TreeNode> q = new LinkedList<>();
q.offer(root);

while(!q.isEmpty()) {
int size = q.size();
//동일한 레벨 노드 값들을 담을 리스트
List<Integer> currentLevel = new ArrayList<>();

for(int i = 0; i < size; i++) {
TreeNode node = q.poll();
currentLevel.add(node.val);

if(node.left != null) {
q.offer(node.left);
}
if(node.right != null) {
q.offer(node.right);
}
}
answer.add(currentLevel);
}
return answer;
}
}
36 changes: 36 additions & 0 deletions counting-bits/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Bit Manipulation
  • 설명: 이 코드는 2진수 문자열을 생성하고 1의 개수를 세는 방식으로, 비트 조작과 관련된 패턴인 Bit Manipulation에 속합니다. 효율성을 위해 문자열 변환 대신 비트 연산을 활용할 수도 있습니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n log n)
Space O(n)

피드백: 이진수 문자열 변환과 각 문자별 검사로 인해 시간 복잡도가 높아집니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public int[] countBits(int n) {
/**
1.문제: n까지의 수를 2진수로 변경 후 1의 개수를 배열에 저장
2.constraints: 최소 O(n logn) 으로 풀 것
3.solutions:
- 0~n까지 for문 돌면서 2진수로 변환 -> 변환하면서 1개수 count -> 배열에 저장
- time complexity: O(n log n) , space: O(n)
*/

int[] answer = new int[n+1];

for(int i = 0; i <= n; i++) {
if(i == 0 || i == 1) {
answer[i] = i;
continue;
}
int count = getNumOfOne(i);
answer[i] = count;
}
return answer;
}

int getNumOfOne(int n) {
int cnt = 0;
String binary = Integer.toBinaryString(n);

for(char c: binary.toCharArray()) {
if(c == '1') {
cnt++;
}
}
return cnt;
}

}
40 changes: 40 additions & 0 deletions find-median-from-data-stream/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Heap / Priority Queue
  • 설명: 이 코드는 두 개의 힙(우선순위 큐)를 이용하여 데이터 스트림에서 중앙값을 효율적으로 찾는 방법을 구현합니다. 힙 자료구조를 활용하여 삽입과 중앙값 계산을 빠르게 수행합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(log n)
Space O(n)

피드백: 힙 자료구조를 활용하여 삽입과 중앙값 계산을 효율적으로 수행합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class MedianFinder {
PriorityQueue<Integer> left;
PriorityQueue<Integer> right;

public MedianFinder() {
/**
리스트를 중간으로 나눠서 절반은 왼쪽, 오른쪽에 저장
왼쪽 리스트는 항상 큰 수가 가장 왼쪽(앞)에 오도록
오른쪽 리스트는 항상 작은 수가 가장 왼쪽(앞)에 오도록 저장한다.

*/
left = new PriorityQueue<>(Collections.reverseOrder()); //max heap
right = new PriorityQueue<>(); //min heap

}

public void addNum(int num) {
left.add(num);
right.add(left.poll());

//총 원소 수가 홀수개이면 left size 더 크도록 유지
if(right.size() > left.size()) {
left.add(right.poll());
}
}

public double findMedian() {
if(left.size() > right.size()) {
return left.peek();
}
return (left.peek() + right.peek()) / 2.0;
}
}

/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/
41 changes: 41 additions & 0 deletions house-robber-ii/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 인접 집을 털지 않는 조건 하에 최대 금액을 계산하는 문제로, DP 배열을 활용하여 이전 상태를 기반으로 최적의 선택을 반복적으로 구하는 방식입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 두 경우(첫 집 포함/불포함)를 각각 계산 후 최댓값을 선택하는 방식으로 최적 해를 찾습니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public int rob(int[] nums) {
/**
1.문제: 인접한 집을 털지않고 훔칠 수 있는 최대 금액 반환
2.constraints
- nums.length min = 1, max= 100
- value min = 0, max = 1000
- 집들은 circle 형태, 인접한 집을 털면 alerting (0번째, 마지막 집 동시에 털 수 없음)
3.solution
- dp[i] = max(dp[i-1], dp[i-2] + nums[i]) -> 이전값 vs i 번째 털었을 경우 max 값
- case는 2가지. 1)0집 포함, n-1불포함 2)0집 불포함, n-1 포함
- time complexity: O(n), space complexity: O(1)
*/
int currentMax = 0;
int n = nums.length;
int[] dp = new int[n-1];
int[] dp2 = new int[n];

if(n == 1) return nums[0];
if(n == 2) return Math.max(nums[0], nums[1]);

dp[0] = nums[0];
dp[1] = Math.max(dp[0], nums[1]);

//case 1) 0번집 포함, N-1 불포함
for(int i = 2; i < n-1; i++) {
//i번째 털기 or i번째 안 털기
dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]);
}

dp2[0] = 0;
dp2[1] = nums[1];
//case 2) 0번집 불포함, N-1 포함
for(int i = 2; i < n; i++) {
//i번째 털기 or i번째 안 털기
dp2[i] = Math.max(dp2[i-1], dp2[i-2] + nums[i]);
}
int answer = Math.max(dp[n-2], dp2[n-1]);
return answer;
}
}
76 changes: 76 additions & 0 deletions word-search-ii/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Trie, Backtracking
  • 설명: 이 코드는 Trie 자료구조를 이용해 단어 검색을 최적화하고, DFS를 활용한 백트래킹으로 보드 내 단어를 찾는 방식입니다. 두 패턴이 조합되어 효율적인 탐색을 수행합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(m * n * 4^l)
Space O(w * l)

피드백: 트라이 구조와 DFS 백트래킹을 활용하여 중복 탐색을 방지하며 효율적으로 검색합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class Solution {
/**
* 너무 어려워서 풀이 봤어요 ㅠㅠ
*/
class TrieNode {
TrieNode[] children = new TrieNode[26];
String word;
}

List<String> result = new ArrayList<>();
int m, n;
char[][] board;

public List<String> findWords(char[][] board, String[] words) {
this.board = board;
this.m = board.length;
this.n = board[0].length;

TrieNode root = buildTrie(words);

for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
dfs(i, j, root);
}
}
return result;
}
private TrieNode buildTrie(String[] words) {
TrieNode root = new TrieNode();

for(String word : words) {
TrieNode curr = root;

for(char ch : word.toCharArray()) {
int idx = ch - 'a';

if(curr.children[idx] == null) {
curr.children[idx] = new TrieNode();
}

curr = curr.children[idx];
}

curr.word = word;
}

return root;
}
void dfs(int i, int j, TrieNode node) {
//boundary check
if(i < 0 || i >= m || j < 0 || j >= n) return;
//이미 방문했으면 return
if(board[i][j] == '#') return;

char ch = board[i][j];
if(node.children[ch - 'a'] == null) return;

node = node.children[ch - 'a'];

if(node.word != null) {
result.add(node.word);
node.word = null; //중복방지 체크
}

board[i][j] = '#'; //방문 체크

dfs(i+1, j, node);
dfs(i-1, j, node);
dfs(i, j+1, node);
dfs(i, j-1, node);

//backtracking
board[i][j] = ch;
}

}
Loading