Repository navigation
[seungyeonchoi] WEEK 04 Solutions #2970
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
Open
seungyeonchoi
wants to merge
1
commit into
DaleStudy:main
Choose a base branch
from
seungyeonchoi:week-04
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| class Solution { | ||
| /** | ||
| For each cell in board, start with i = 0: | ||
| At cell (r, c), looking for word[i]: | ||
| 1. out of the board? -> return false | ||
| 2. already visited? -> return false | ||
| 3. not equal to word[i]? -> return false | ||
| 4. word[i] is the last letter -> return true | ||
| 5. mark visited | ||
| 6. explore the 4 adjacent cells for word[i+1] | ||
| -> if any of them succeeds, return true | ||
| 7. unmark visited, so other paths can use this cell | ||
| 8. return false | ||
| If any start cell succeeds -> true | ||
| If every start cell fails -> false | ||
| 한 칸 (r, c)에 들어왔다. 지금 word[i]를 찾는 중이다. | ||
| 1. 격자 밖인지 체크 → 밖이면 실패 | ||
| 2. 이미 방문한 곳인지 체크 → 방문했으면 실패 | ||
| 3. 타겟 글자와 동일한지 체크 → 다르면 실패 | ||
| 4. 마지막 글자였다면 → 성공 | ||
| 5. 방문 표시 | ||
| 6. 상하좌우로 word[i+1]을 찾으러 탐색 → 하나라도 성공하면 성공 | ||
| 7. 방문 표시 해제하고 실패 반환 | ||
|
|
||
| 격자의 모든 칸 (r, c)에서: | ||
| 위 과정을 i = 0으로 시작 → 성공하면 true | ||
| 전부 실패하면 false | ||
| */ | ||
| /** | ||
| Complexity | ||
| - time: O(m*n*3^L), m x n is the board size, L is the word length | ||
| - try every cell as a start (m*n) | ||
| - each step has at most 3 directions, since the previous cell is already visited | ||
| - the path is at most L long, so 3^L paths per start | ||
| - space: O(m*n + L), visited array + recursion stack | ||
| */ | ||
| public boolean exist(char[][] board, String word) { | ||
| boolean [][] visited = new boolean [board.length][board[0].length]; | ||
| int index = 0; | ||
| for (int i=0; i<board.length;i++) { | ||
| for (int j=0;j<board[0].length;j++) { | ||
| if (backTracking(board, word, i, j, visited, index)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private boolean backTracking(char[][] board, String word, int row, int col, boolean[][] visited, int index){ | ||
| if(row<0 || row >= visited.length) { | ||
| return false; | ||
| } | ||
| if(col<0 || col >= visited[0].length) { | ||
| return false; | ||
| } | ||
|
|
||
| if (visited[row][col]) { | ||
| return false; | ||
| } | ||
|
|
||
| if (board[row][col] != word.charAt(index)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (index == word.length()-1) { | ||
| return true; | ||
| } | ||
|
|
||
| visited[row][col] = true; | ||
| if (backTracking(board, word, row-1, col, visited, index+1)) { | ||
| return true; | ||
| } | ||
| if (backTracking(board, word, row+1, col, visited, index+1)) { | ||
| return true; | ||
| } | ||
| if (backTracking(board, word, row, col-1, visited, index+1)) { | ||
| return true; | ||
| } | ||
| if (backTracking(board, word, row, col+1, visited, index+1)) { | ||
| return true; | ||
| } | ||
|
Comment on lines
+71
to
+82
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반복문 활용해서 이런 형태로 개선할 수 있을 거 같아요. |
||
| visited[row][col] = false; | ||
|
Comment on lines
+71
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 하나의 visited를 재사용하면서 방문 기록을 복구해 시간복잡도를 개선할 수 있다는 점을 배웠습니다! |
||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
풀이 과정을 주석으로 남겨주셔서 리뷰 시에 코드를 파악하기 용이한 것 같습니다 !👍