Skip to content

[sm9171] Week 4 solutions #1358

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 4 commits into from
Apr 28, 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
12 changes: 12 additions & 0 deletions find-minimum-in-rotated-sorted-array/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public int findMin(int[] nums) {
int min = Integer.MAX_VALUE;

for (int i = 0; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
return min;
}
}
10 changes: 10 additions & 0 deletions maximum-depth-of-binary-tree/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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;
}
}
20 changes: 20 additions & 0 deletions merge-two-sorted-lists/sm9171.java
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요, 작성하신 코드 잘 보았습니다! 더미 노드 사용해서 엣지 케이스까지 잘 구현하신 것 같아요. 그냥 개인적으로 궁금한 점이 있다면 자바에서 LinkedList를 제공하고 있는 걸로 아는데 직접 구현하신 이유가 있는지 여쭤보고 싶습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(-1);
ListNode node = dummy;

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

node.next = list1 != null ? list1 : list2;
return dummy.next;
}
}
42 changes: 42 additions & 0 deletions word-search/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution {
public boolean exist(char[][] board, String word) {
if (board == null || board.length == 0 || word == null || word.length() == 0) {
return false;
}

int rows = board.length;
int cols = board[0].length;
boolean[][] visited = new boolean[rows][cols];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (dfs(board, visited, i, j, word, 0)) {
return true;
}
}
}
return false;
}

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

if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || visited[i][j] || board[i][j] != word.charAt(index)) {
return false;
}


visited[i][j] = true;

boolean found = dfs(board, visited, i + 1, j, word, index + 1) ||
dfs(board, visited, i - 1, j, word, index + 1) ||
dfs(board, visited, i, j + 1, word, index + 1) ||
dfs(board, visited, i, j - 1, word, index + 1);

visited[i][j] = false;

return found;
}
}