Skip to content

[jinhyungrhee] Week 04 Solutions #1362

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
22 changes: 22 additions & 0 deletions coin-change/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.*;
class Solution {
int INF = 987654321;
public int coinChange(int[] coins, int amount) {
if (amount == 0) return 0;

int[] dp = new int[amount + 1];
Arrays.fill(dp, INF);

dp[0] = 0;
for (int coin : coins) {
if (coin <= amount) dp[coin] = 1;
}

for (int i = 1; i <= amount; i++) {
for (int coin : coins) {
if ((i - coin) >= 0) dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
return (dp[amount] == INF) ? -1 : dp[amount];
}
}
17 changes: 17 additions & 0 deletions find-minimum-in-rotated-sorted-array/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int findMin(int[] nums) {
int left = 0, 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];
}
}
17 changes: 17 additions & 0 deletions maximum-depth-of-binary-tree/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int maxVal;
public int maxDepth(TreeNode root) {
dfs(root, 0);
return maxVal;
}
public void dfs(TreeNode node, int step) {

if (node == null) {
if (maxVal < step) maxVal = step;
return;
}

dfs(node.left, step + 1);
dfs(node.right, step + 1);
}
}
29 changes: 29 additions & 0 deletions merge-two-sorted-lists/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

ListNode dummy = new ListNode(0);
ListNode current = dummy; // ***실제로 노드를 이어나갈 포인터***

while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
current.next = list1;
list1 = list1.next;
}
else {
current.next = list2;
list2 = list2.next;
}
current = current.next;
}

// 남아있는 노드들 이어붙이기
if (list1 != null) {
current.next = list1;
}
if (list2 != null) {
current.next = list2;
}

return dummy.next;
}
}
40 changes: 40 additions & 0 deletions word-search/jinhyungrhee.java
Copy link
Contributor

Choose a reason for hiding this comment

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

visited와 dir 모두 전역에서 사용되는 변수들이라 클래스 변수 혹은 파라미터를 통해 전달하는 방식으로 사용해도 괜찮을것 같네요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public boolean exist(char[][] board, String word) {

boolean[][] visited = new boolean[board.length][board[0].length];

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

public boolean dfs(char[][] board, String word, boolean[][] visited, int x, int y, int index) {

// [성공] : 모든 글자 매칭 성공
if (index == word.length()) return true;

// [실패] : 범위 초과, 방문함, 글자 불일치
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length ||
visited[x][y] || board[x][y] != word.charAt(index)) {
return false;
}

visited[x][y] = true;
int[][] dir = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
for (int i = 0; i < 4; i++) {
int newX = x + dir[i][0], newY = y + dir[i][1];
if (dfs(board, word, visited, newX, newY, index + 1)) {
return true; // 찾았으면 바로 리턴
}
}
visited[x][y] = false; // backtrack

return false; // 4방향 다 실패하면 false

}
}