-
-
Notifications
You must be signed in to change notification settings - Fork 334
[hyeri0903] WEEK 14 Solutions #2624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4c219f5
8409113
7c8cb86
551d7f3
19a3ea3
bd67c11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이진수 문자열 변환과 각 문자별 검사로 인해 시간 복잡도가 높아집니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| 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; | ||
| } | ||
|
|
||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 힙 자료구조를 활용하여 삽입과 중앙값 계산을 효율적으로 수행합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| 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(); | ||
| */ |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 두 경우(첫 집 포함/불포함)를 각각 계산 후 최댓값을 선택하는 방식으로 최적 해를 찾습니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| 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; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 트라이 구조와 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; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 큐를 사용하여 각 레벨의 노드를 순차적으로 방문하므로 노드 수에 비례하는 시간과 공간이 소요됩니다.
개선 제안: 현재 구현이 적절해 보입니다.