Skip to content

[forest000014] Week 04 #805

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 6 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 37 additions & 0 deletions merge-two-sorted-lists/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**

(n = list1.length + list2.length)
시간 복잡도: O(n)
- 최악의 경우 전체 노드 순회
공간 복잡도: O(n)
- 최악의 경우 전체 노드만큼 새 노드 생성

*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode head = new ListNode();
ListNode curr = head;

while (true) {
if (list1 == null) {
curr.next = list2;
break;
} else if (list2 == null) {
curr.next = list1;
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

while(true) 대신에 while(list1 != null && list2 != null) 을 조건문으로 사용하고 내부 if ~ else를 while문이 끝난 후에 사용하는 방법도 있을것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

아하... 그렇게 하는 게 좀 더 논리가 명확하겠네요!
사실 처음부터 while(true)로 짰던 것은 아니고, 최대한 불필요한 조건문을 줄이고 줄이다 보니 지금 코드가 나오게 되었는데요,
코드를 다시 읽어보니 이 조건문이 언제 종료가 되는 것인지, 항상 종료가 되긴 하는 것인지가 한 눈에 직관적으로 이해되지는 않을 수 있겠네요
제안해 주신 아이디어대로 수정해보겠습니다 😄


if (list1.val <= list2.val) {
curr.next = new ListNode(list1.val);
curr = curr.next;
list1 = list1.next;
} else {
curr.next = new ListNode(list2.val);
curr = curr.next;
list2 = list2.next;
}
}

return head.next;
}
}
16 changes: 16 additions & 0 deletions missing-number/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
시간 복잡도: O(n)
공간 복잡도: O(1)

1 ~ n의 합이 n * (n + 1) / 2 라는 점을 활용
*/
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int ans = n * (n + 1) / 2;
for (int i = 0; i < n; i++) {
ans -= nums[i];
}
return ans;
}
}
84 changes: 84 additions & 0 deletions word-search/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Time Complexity: O(m * n * 4^(word.length))
Space Complexity: O(m * n)
*/

class Solution {
boolean[][] visited;
int m, n;
int len;
int[] dr = {-1, 0, 1, 0}; // clockwise traversal
int[] dc = {0, 1, 0, -1};
char board2[][];
String word2;

public boolean exist(char[][] board, String word) {
int[] cnt = new int[52];
board2 = board;
word2 = word;
m = board.length;
n = board[0].length;
visited = new boolean[m][n];
len = word.length();

// 1. for pruning, count characters in board and word respectively
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cnt[charToInt(board[i][j])]++;
}
}
for (int i = 0; i < len; i++) {
int idx = charToInt(word.charAt(i));
if (--cnt[idx] < 0) {
return false;
}
}

// 2. DFS
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board2[i][j] != word.charAt(0)) {
continue;
}
if (dfs(i, j, 1)) {
return true;
}
}
}
return false;
}

private boolean dfs(int row, int col, int idx) {
if (idx == len) { // end of word
return true;
}
visited[row][col] = true;

for (int i = 0; i < 4; i++) {
int nr = row + dr[i];
int nc = col + dc[i];
if (nr < 0 || nr >= m || nc < 0 || nc >= n) { // check boundary of the board
continue;
}
if (visited[nr][nc]) { // check visited
continue;
}
if (board2[nr][nc] == word2.charAt(idx)) {
if (dfs(nr, nc, idx + 1)) {
return true;
}
}
}

visited[row][col] = false;
return false;
}

private int charToInt(char ch) {
if (ch <= 'Z') {
return ch - 'A';
} else {
return ch - 'a' + 26;
}
}
}
Loading