Skip to content

[Yn3-3xh] WEEK 04 solutions #1365

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

Merged
merged 5 commits into from
Apr 27, 2025
Merged
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
29 changes: 29 additions & 0 deletions coin-change/Yn3-3xh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
[문제풀이]
- 배열에 주어진 수를 더해서 amount 만들기
- 배열은 중복 가능
- amount를 만들 수 없으면 -1
- amount를 만든 동전의 최소 개수 구하기
- DP
time: O(N M), space: O(N)

[회고]
DP로 풀면 되지 않을까 라는 짐작은 가는데,
아직 풀이방법이 부족하다..
해결방법을 보고 겨우 이해는 했다..
*/
class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;

for (int coin : coins) {
for (int i = coin; i <= amount; i++) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
}

49 changes: 49 additions & 0 deletions find-minimum-in-rotated-sorted-array/Yn3-3xh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
[문제풀이]
- 왼쪽의 수 보다 오른쪽의 수가 작은 순간을 찾자.
- 가장 왼쪽의 수 보다 가장 오른쪽의 수가 크면 회전이 일어나지 않은 상태이므로, 첫번째가 최솟값이다.
- 풀이1
time: O(N), space: O(1);
class Solution {
public int findMin(int[] nums) {
if (nums.length == 1) {
return nums[0];
}

for (int i = 1; i < nums.length; i++) {
if (nums[i - 1] > nums[i]) {
return nums[i];
}
}
return nums[0];
}
}
- 풀이2 <이진탐색>
time: O(log N), space: O(1)

[회고]
이진탐색으로 푸는 능력이 부족한 것 같다.
*/

class Solution {
public int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;

if (nums[left] < nums[right]) {
return nums[left];
}

while (left < right) {
int mid = (left + right) / 2;

if (nums[mid] <= nums[right]) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[left];
}
}

55 changes: 55 additions & 0 deletions maximum-depth-of-binary-tree/Yn3-3xh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
[문제풀이]
- 최대 depth 구하기
- left or right의 null일 때 depth의 max 구하기
- DFS 1
time: O(N), space: O(N)
class Solution {
public int maxDepth(TreeNode root) {
return nextNode(root, 0);
}

private int nextNode(TreeNode node, int depth) {
if (node == null) {
return depth;
}

int leftDepth = nextNode(node.left, depth + 1);
int rightDepth = nextNode(node.right, depth + 1);
return Math.max(leftDepth, rightDepth);
}
}
- DFS 2
time: O(N), space: O(N)

[회고]
이전에 풀었던 Merget Two Sorted Lists 문제에서 주어진 메서드를 재사용할 수 있겠다는 생각으로 풀 수 있었다!
*/

/**
* 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 maxDepth(TreeNode root) {
if (root == null) {
return 0;
}

int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return Math.max(leftDepth, rightDepth) + 1;
}
}

37 changes: 37 additions & 0 deletions merge-two-sorted-lists/Yn3-3xh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
[문제풀이]
time: O(N + M), space: O(N + M)
- list1과 list2를 비교해서 list2의 수가 작으면 list1을 list2로 교체하자.
- 이때 list1은 list2의 자리에 들어갈 것이다.

[회고]
주어진 메서드를 dfs로 활용할 수도 있구나!
*/

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null || list2 == null) {
return list1 == null ? list2 : list1;
}

if (list1.val > list2.val) {
ListNode temp = list1;
list1 = list2;
list2 = temp;
}

list1.next = mergeTwoLists(list1.next, list2);
return list1;
}
}

63 changes: 63 additions & 0 deletions word-search/Yn3-3xh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
[문제풀이]
- 이웃되는 알파벳이 이어져야 한다.
- 각 알파벳은 한번만 호출되어야 한다.
- DFS
time: O(N M 4^L), space: O(L)

[회고]
BFS로 풀면 되지 않을까 했지만, 최단경로를 찾는 것이 아니니 안 어울릴 것 같다.
DFS로 풀자는 방법은 맞았지만, 풀이과정이 어려웠다.
다시 풀어보면 좋을 것 같다.
*/

class Solution {
public boolean exist(char[][] board, String word) {
int column = board.length;
int row = board[0].length;
boolean[][] visited = new boolean[column][row];

for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
if (board[i][j] == word.charAt(0)) {
if (dfs(board, word, visited, i, j, 0)) {
return true;
}
}
}
}
return false;
}

private boolean dfs(char[][] board, String word, boolean[][] visited, int i, int j, int wordIndex) {
if (wordIndex == word.length()) {
return true;
}

if (isVisited(board, word, visited, i, j, wordIndex)) {
return false;
}

visited[i][j] = true;
if (dfs(board, word, visited, i + 1, j, wordIndex + 1) ||
dfs(board, word, visited, i - 1, j, wordIndex + 1) ||
dfs(board, word, visited, i, j + 1, wordIndex + 1) ||
dfs(board, word, visited, i, j - 1, wordIndex + 1)
) {
return true;
}

visited[i][j] = false;
return false;
}

private boolean isVisited(char[][] board, String word, boolean[][] visited, int i, int j, int wordIndex) {
return i < 0 ||
j < 0 ||
i >= board.length ||
j >= board[0].length ||
visited[i][j] ||
board[i][j] != word.charAt(wordIndex);
}
}