diff --git a/find-minimum-in-rotated-sorted-array/JeonJe.java b/find-minimum-in-rotated-sorted-array/JeonJe.java new file mode 100644 index 0000000000..55071f319e --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/JeonJe.java @@ -0,0 +1,24 @@ +import java.util.*; + +// TC: O(log n) +// SC: O(1) +class Solution { + public int findMin(int[] nums) { + + int lowIndex = 0; + int highIndex = nums.length - 1; + + while(lowIndex < highIndex) { + int midIndex = (lowIndex + highIndex) / 2; + + //오른 부분이 정렬 되지 않음 = 왼쪽 부분은 정렬됨 + if(nums[midIndex] > nums[highIndex]) { + lowIndex = midIndex + 1; + } else { + //오른쪽 부분이 정렬 됨 = 왼쪽 부분을 봐야함 + highIndex = midIndex; + } + } + return nums[lowIndex]; + } +} diff --git a/maximum-depth-of-binary-tree/JeonJe.java b/maximum-depth-of-binary-tree/JeonJe.java new file mode 100644 index 0000000000..e2dd694c60 --- /dev/null +++ b/maximum-depth-of-binary-tree/JeonJe.java @@ -0,0 +1,15 @@ +import java.util.*; + +// TC: O(n) +// SC: O(h) +class Solution { + public int maxDepth(TreeNode root) { + return calMaxDepth(root); + } + + // 현재까지 최대 깊이는 Math.max(왼쪽 서브노드 최대 깊이 , 오른쪽 서브노드 최대깊이) + 1 + private int calMaxDepth(TreeNode node) { + if (node == null) return 0; + return Math.max(calMaxDepth(node.left), calMaxDepth(node.right)) + 1; + } +} diff --git a/merge-two-sorted-lists/JeonJe.java b/merge-two-sorted-lists/JeonJe.java new file mode 100644 index 0000000000..35864654f5 --- /dev/null +++ b/merge-two-sorted-lists/JeonJe.java @@ -0,0 +1,31 @@ +import java.util.*; + +// TC: O(n+m) +// SC: O(n+m) +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode dummy = new ListNode(0); + ListNode tail = dummy; + + while (list1 != null && list2 != null) { + if (list1.val > list2.val) { + tail.next = new ListNode(list2.val); + list2 = list2.next; + } else { + tail.next = new ListNode(list1.val); + list1 = list1.next; + } + tail = tail.next; + } + + if (list1 != null) { + tail.next = list1; + } + + if (list2 != null) { + tail.next = list2; + } + + return dummy.next; + } +} diff --git a/word-search/JeonJe.java b/word-search/JeonJe.java new file mode 100644 index 0000000000..12d05b4cdd --- /dev/null +++ b/word-search/JeonJe.java @@ -0,0 +1,58 @@ +import java.util.*; + +// TC: O(m * n * 4^L) +// SC: O(m * n) +class Solution { + + private char[][] board; + private int m, n; + private boolean[][] visited; + private char[] word; + private static final int[] dx = {-1, 1, 0, 0}; + private static final int[] dy = {0, 0, -1, 1}; + + + public boolean exist(char[][] board, String word) { + this.board = board; + this.word = word.toCharArray(); + this.n = board.length; + this.m = board[0].length; + this.visited = new boolean[n][m]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (dfs(i, j, 0)) { + return true; + } + } + } + return false; + } + + private boolean dfs(int x, int y, int wordIndex) { + if (board[x][y] != word[wordIndex] || visited[x][y]) { + return false; + } + if (wordIndex == word.length - 1) { + return true; + } + + visited[x][y] = true; + + for (int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx < 0 || nx >= n || ny < 0 || ny >= m) { + continue; + } + + if (board[nx][ny] == word[wordIndex + 1] && !visited[nx][ny]) { + if (dfs(nx, ny, wordIndex + 1)) { + return true; + } + } + } + visited[x][y] = false; + return false; + } +}