Skip to content

[YoungSeok-Choi] Week 4 Solutions #1330

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
17 changes: 17 additions & 0 deletions coin-change/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.Arrays;

class Solution {
public int coinChange(int[] coins, int amount) {
int[] memo = new int[amount + 1];
Arrays.fill(memo, amount + 1); // 도달 불가능한 초기값
memo[0] = 0; // 0원을 만들기 위한 동전 수는 0개

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

return memo[amount] > amount ? -1 : memo[amount];
}
}
13 changes: 13 additions & 0 deletions find-minimum-in-rotated-sorted-array/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// NOTE: 문제에서 반드시 log n 복잡도의 알고리즘을 작성하라고 했는데.. 맞았다..
// 이런걸 묻는건지... 다른 풀이 코드 보면서 복기가 필요함..
class Solution {
public int findMin(int[] nums) {
int min = 9876521;

for(int i = 0; i < nums.length; i++) {
min = Math.min(min, nums[i]);
}

return min;
}
}
35 changes: 35 additions & 0 deletions maximum-depth-of-binary-tree/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.LinkedList;
import java.util.Queue;

// 시간복잡도 O(n)
class Solution {
public int depth = 0;
public int maxDepth(TreeNode root) {

if(root == null) {
return depth;
}

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

while(!q.isEmpty()) {
int size = q.size();
depth++;

for(int i = 0; i < size; i++) {
TreeNode p = q.poll();

if(p.right != null) {
q.add(p.right);
}

if(p.left != null) {
q.add(p.left);
}
}
}

return depth;
}
}
44 changes: 44 additions & 0 deletions merge-two-sorted-lists/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 시간복잡도 O(N + M)
class Solution {

public ListNode root = null;
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

while(list1 != null || list2 != null) {
int v1 = 9999;
int v2 = 9999;

if(list1 != null) {
v1 = list1.val;
}

if(list2 != null) {
v2 = list2.val;
}

if(v1 < v2) {
addNode(v1);
list1 = list1.next;
} else {
addNode(v2);
list2 = list2.next;
}
}

return root;
}

public void addNode (int val) {
if(root == null) {
root = new ListNode(val);
return;
}

ListNode now = root;
while(now.next != null) {
now = now.next;
}

now.next = new ListNode(val);
}
}
120 changes: 120 additions & 0 deletions word-search/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// NOTE: Queue를 처음에 써서 탐색하며 꼬여버리는 문제가 있었다..
// NOTE: 원본 문자의 index를 사용해서 해결.

import java.util.LinkedList;
import java.util.Queue;

class Solution {

public boolean[][] visit;
int w = 0;
int h = 0;
int[] dx = {1, 0, -1, 0};
int[] dy = {0, 1, 0, -1};

public boolean exist(char[][] board, String word) {

char[] cArr = word.toCharArray();
w = board.length;
h = board[0].length;
visit = new boolean[w][h];

for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(cArr[0] == board[i][j]) {

if(dfs(board, word, i, j, 0)) {
return true;
}
}
}
}

return false;
}

public boolean dfs(char[][] b, String word, int x, int y, int idx) {
if(idx == word.length()) return true;

if(x < 0 || x >= w || y < 0 || y >= h || b[x][y] != word.charAt(idx) || visit[x][y]) {
return false;
}

visit[x][y] = true;

for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];

if(dfs(b, word, nx, ny, idx + 1)) {
return true;
}
}

visit[x][y] = false;
return false;
}



class WrongSolution {

public boolean[][] visit;
Queue<Character> q = new LinkedList();
int w = 0;
int h = 0;
int[] dx = {1, 0, -1, 0};
int[] dy = {0, 1, 0, -1};

public boolean exist(char[][] board, String word) {

char[] cArr = word.toCharArray();
w = board.length;
h = board[0].length;
visit = new boolean[w][h];

for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(cArr[0] == board[i][j]) {
q = new LinkedList();
visit = new boolean[w][h];
for(char c : word.toCharArray()) {
q.add(c);
}


dfs(board, i, j);
if(q.isEmpty()) {
return true;
}
}
}
}

return false;
}

public void dfs(char[][] b, int x, int y) {
if(x < 0 || x >= w || y < 0 || y >= h || visit[x][y]) {
return;
}

if(q.isEmpty()) {
return;
}

if(b[x][y] != q.peek()) {
return;
}

q.poll();
visit[x][y] = true;

for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];

dfs(b, nx, ny);
}
}
}}