Skip to content
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
26 changes: 26 additions & 0 deletions find-minimum-in-rotated-sorted-array/YuuuuuuYu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Runtime: 0ms
* Time Complexity: O(log n)
*
* Memory: 43.81MB
* Space Complexity: O(1)
*
* Approach: 이진 탐색
*/
class Solution {
public int findMin(int[] nums) {
int left = 0;
int right = nums.length-1;

while (left < right) {
int mid = left + (right-left)/2;
if (nums[mid] <= nums[right]) {
right = mid;
} else {
left = mid + 1;
}
}

return nums[left];
}
}
20 changes: 20 additions & 0 deletions maximum-depth-of-binary-tree/YuuuuuuYu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Runtime: 0ms
* Time Complexity: O(n)
*
* Memory: 44.28MB
* Space Complexity: O(h)
* - h: 트리의 높이 or 최대 깊이
*
* Approach: 재귀를 이용한 DFS 접근법
* - 루트부터 시작하여 재귀가 시작할 때마다 1로 계산
* - 트리의 끝에 도달하면 0을 리턴하여 차례대로 남은 깊이를 계산
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null)
return 0;

return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
34 changes: 34 additions & 0 deletions merge-two-sorted-lists/YuuuuuuYu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Runtime: 0ms
* Time Complexity: O(n)
* - list1.length + list2.length
*
* Memory: 44.28MB
* Space Complexity: O(1)
*
* Approach: 빈 node를 만들고 list1, list2의 현재 값을 비교하여 더 작은 값을 추가
*
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
else if (list2 == null) return list1;

ListNode result = new ListNode();
ListNode currentNode = result;
while (list1 != null && list2 != null) {
if (list1.val > list2.val) {
currentNode.next = list2;
list2 = list2.next;
} else {
currentNode.next = list1;
list1 = list1.next;
}

currentNode = currentNode.next;
}

currentNode.next = list1 != null ? list1 : list2;
return result.next;
}
}
67 changes: 67 additions & 0 deletions word-search/YuuuuuuYu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Runtime: 128ms
* Time Complexity: O(m x n x 4^l)
* - m: board의 행 길이
* - n: board의 열 길이
* - l: word의 길이
*
* Memory: 42.69MB
* Space Complexity: O(l)
*
* Approach: DFS + 백트래킹
* 1) board를 순회하며 word의 첫 글자와 일치하는 글자를 찾음
* 2) 일치하는 글자를 찾으면 DFS를 통해 상하좌우로 다음 글자를 찾음
* 3) 성능 최적화
* - 백트래킹을 위해 방문한 글자는 임시로 다른 문자('#')로 변경 (비트마스크)
* - charAt 캐싱
*/
class Solution {
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};

public boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
char startChar = word.charAt(0);

for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (board[i][j] == startChar) {
if (dfs(board, i, j, 0, word)) {
return true;
}
}
}
}

return false;
}

boolean dfs(char[][] board, int x, int y, int index, String word) {
if (index == word.length()-1) {
return true;
}

char temp = board[x][y];
char nextChar = word.charAt(index+1);
board[x][y] = ' ';

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

if (nx >= 0 && nx < board.length &&
ny >= 0 && ny < board[0].length &&
board[nx][ny] == nextChar) {

if (dfs(board, nx, ny, index+1, word)) {
board[x][y] = temp;
return true;
}
}
}

board[x][y] = temp;
return false;
}
}