-
-
Notifications
You must be signed in to change notification settings - Fork 361
[tigermint] WEEK 04 Solutions #2748
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
Changes from 4 commits
00a0f20
72e9a27
2bc1c6e
52536d7
43235c9
be688b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution { | ||
| fun findMin(nums: IntArray): Int { | ||
| var left = 0 | ||
| var right = nums.size - 1 | ||
|
|
||
| while (left < right) { | ||
| val mid = (left + right) / 2 | ||
|
|
||
| when { | ||
|
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. 작성하신 로직의 아이디어에서 중간에 해답인 케이스를 알 수 있는데요. 이 떄는 더 이상 진행할 필요가 없어서 최적화 해보셔도 좋을 거 같습니다. |
||
| nums[mid] > nums[right] -> left = mid + 1 // 오른쪽 존재 | ||
| nums[mid] <= nums[right] -> right = mid // 왼쪽 존재 or 나 자신 | ||
| } | ||
| } | ||
|
|
||
| return nums[left] | ||
| } | ||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 루트에서 모든 노드를 방문하며, 재귀 스택의 깊이가 트리의 높이(h) 만큼 사용된다. 개선 제안: 재귀 대신 명시적 스택을 사용한 구현으로 스택 깊이 한계 문제를 피할 수 있다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /** | ||
| * Example: | ||
| * var ti = TreeNode(5) | ||
| * var v = ti.`val` | ||
| * Definition for a binary tree node. | ||
| * class TreeNode(var `val`: Int) { | ||
| * var left: TreeNode? = null | ||
| * var right: TreeNode? = null | ||
| * } | ||
| */ | ||
| class Solution { | ||
| fun maxDepth(root: TreeNode?): Int { | ||
| if (root == null) return 0 | ||
|
|
||
| var maxDepth = 0 | ||
|
|
||
| fun dfs(node: TreeNode, depth: Int) { | ||
| //leaf node | ||
| if (node.left == null && node.right == null) maxDepth = maxOf(maxDepth, depth) | ||
|
|
||
| if (node.left != null) dfs(node.left!!, depth + 1) | ||
| if (node.right != null) dfs(node.right!!, depth + 1) | ||
| } | ||
|
|
||
| dfs(root!!, 1) | ||
|
|
||
| return maxDepth | ||
| } | ||
|
tigermint marked this conversation as resolved.
|
||
|
|
||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 새로운 리스트를 만들어 연결해 나가며, 두 포인터를 이용해 선형 시간에 병합한다. 개선 제안: 리스트 노드 재활용으로 불필요한 새 노드 생성 없이 구현 가능하면 공간을 더 낮출 수 있다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * Example: | ||
| * var li = ListNode(5) | ||
| * var v = li.`val` | ||
| * Definition for singly-linked list. | ||
| * class ListNode(var `val`: Int) { | ||
| * var next: ListNode? = null | ||
| * } | ||
| */ | ||
| class Solution { | ||
| fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? { | ||
| val head = ListNode(0) | ||
| var tail = head | ||
| var l1 = list1 | ||
| var l2 = list2 | ||
|
|
||
| while (l1 != null && l2 != null) { | ||
| if (l1.`val` <= l2.`val`) { | ||
| tail.next = l1 | ||
| l1 = l1.next | ||
| } else { | ||
| tail.next = l2 | ||
| l2 = l2.next | ||
| } | ||
|
|
||
| tail = tail.next!! | ||
| } | ||
|
|
||
| tail.next = l1 ?: l2 | ||
|
|
||
| return head!!.next | ||
|
tigermint marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 한 글자씩 네 방향으로 DFS를 확장하고 방문 여부를 관리하여 모든 시작 위치에서 탐색한다. 개선 제안: 백트래킹 prune 조건을 강화하거나, 보드 크기가 커질 때는 DP/메모이제이션으로 중복 탐색을 줄일 수 있다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| class Solution { | ||
|
|
||
| private val dx = intArrayOf(-1, 1, 0, 0) | ||
| private val dy = intArrayOf(0, 0, -1, 1) | ||
|
|
||
| fun exist(board: Array<CharArray>, word: String): Boolean { | ||
| val rows = board.size | ||
| val cols = board[0].size | ||
| val visited = Array(rows) { BooleanArray(cols) } | ||
|
|
||
|
Comment on lines
+9
to
+10
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. board를 활용해 방문한 셀은 마킹해놓는 식으로 하면 visited 선언 없이도 가능하니 이렇게 풀어보시는 것도 추천드립니다. |
||
| val starts = mutableListOf<Pair<Int, Int>>() | ||
| for (i in 0 until rows) { | ||
| for (j in 0 until cols) { | ||
| if (board[i][j] == word[0]) starts.add(i to j) | ||
| } | ||
| } | ||
|
|
||
| if (word.length == 1) return starts.isNotEmpty() | ||
|
|
||
|
Comment on lines
+18
to
+19
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. 실제로 돌려보니 runtime 이 50% 정도 나오는데요. board, word 인풋을 가지고 미리 리턴할 수 있는 케이스도 고려해서 최적화 해보시면 좋을 거 같습니다. |
||
| var flag = false | ||
|
|
||
| fun dfs(start: Pair<Int, Int>, index: Int) { | ||
| if (flag) return | ||
| if (index == word.length - 1) { | ||
| flag = true | ||
| return | ||
|
Comment on lines
+20
to
+26
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. 전역변수 flag 대신 dfs 함수의 반환 값을 boolean으로 활용해보셔도 좋을 거 같습니다. |
||
| } | ||
|
|
||
| for (i in 0 until 4) { | ||
| val nx = start.first + dx[i] | ||
| val ny = start.second + dy[i] | ||
|
|
||
| if (nx < 0 || nx >= rows) continue | ||
| if (ny < 0 || ny >= cols) continue | ||
| if (visited[nx][ny]) continue | ||
| if (word[index + 1] != board[nx][ny]) continue | ||
|
|
||
| visited[nx][ny] = true | ||
| dfs(Pair(nx, ny), index + 1) | ||
| visited[nx][ny] = false | ||
| } | ||
| } | ||
|
|
||
| for (start in starts) { | ||
| visited[start.first][start.second] = true | ||
| dfs(start, 0) | ||
| visited[start.first][start.second] = false | ||
| if (flag) return true | ||
| } | ||
|
|
||
| return false | ||
| } | ||
| } | ||
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이진 탐색으로 왼쪽/오른쪽의 중간 비교를 통해 최소값 위치를 좁혀 나간다.
개선 제안: 코드가 간결해 보이나, mid와 right 비교 조건을 명확히 주석으로 구분하면 가독성을 높일 수 있다.