-
Notifications
You must be signed in to change notification settings - Fork 126
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
[gmlwls96] Week7 #913
[gmlwls96] Week7 #913
Changes from all commits
ac890e2
97099af
16fb71d
78c4ea8
2cd028d
17b3360
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,16 @@ | ||
class Solution { | ||
// 시간 : O(n) 공간 : O(n) | ||
fun lengthOfLongestSubstring(s: String): Int { | ||
var max = 0 | ||
val subStr = StringBuffer() | ||
s.forEach { // s를 조회하면서 글자를 subStr에 담는다. | ||
if (subStr.contains(it)) { // 단, 겹치는 글자가 있을경우 subStr의 len을 기록하고, 초기화 한다. | ||
max = max(max, subStr.length) | ||
subStr.delete(0, subStr.length) | ||
} | ||
subStr.append(it) | ||
} | ||
max = max(max, subStr.length) | ||
return max | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class Solution { | ||
// 시간 : O(N), 공간 O(N) | ||
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. 문제 설명에서 주어진 변수가 m, n (2차원 배열의 행, 열)이므로 복잡도 분석의 결과를 m, n을 이용해서 표현해주시거나 별도의 추가적인 설명을 곁들여주세요 |
||
fun numIslands(grid: Array<CharArray>): Int { | ||
val moveMap = arrayOf( | ||
intArrayOf(1, 0), | ||
intArrayOf(0, 1) | ||
) | ||
val queue = mutableListOf<IntArray>() | ||
val visitMap = Array(grid.size) { | ||
BooleanArray(grid[it].size) | ||
} | ||
var count = 0 | ||
// 1. grid를 모두 조회 | ||
for (y in grid.indices) { | ||
for (x in grid[y].indices) { | ||
// 2. grid[y][x]가 1이고 방문한적이 없으면 newLand로 인지하고 count를 올린다. | ||
if (grid[y][x] == '1' && !visitMap[y][x]) { | ||
count++ | ||
queue.add(intArrayOf(y, x)) | ||
} | ||
// 3. 인접해있는 land를 조회하기위해 queue를 이용하여 탐색하고, visitMap을 변경해준다. | ||
while (queue.isNotEmpty()) { | ||
val newInfo = queue.removeFirst() | ||
val newY = newInfo[0] | ||
val newX = newInfo[1] | ||
visitMap[newY][newX] = true | ||
moveMap.forEach { | ||
val nextY = newY + it[0] | ||
val nextX = newX + it[1] | ||
if (nextY < grid.size | ||
&& nextX < grid[nextY].size | ||
&& grid[nextY][nextX] == '1' | ||
&& !visitMap[nextY][nextX] | ||
) { | ||
queue.add(intArrayOf(nextY, nextX)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return count | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
// 시간 : O(N), 공간 : O(1) | ||
// head를 조회하며 새로운 answerRoot에 새로운 ListNode를 생성하고 꼬리에 현재 answerRoot를 넣는다. | ||
fun reverseList(head: ListNode?): ListNode? { | ||
var currentHead = head | ||
var answerRoot: ListNode? = null | ||
|
||
while (currentHead != null) { | ||
answerRoot = ListNode(currentHead.`val`).apply { next = answerRoot } | ||
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. answerRoot 뒤에 새로운 ListNode 인스턴스를 생성해서 추가하고 있으니까 공간 복잡도가 O(1)이 아닐 것 같아요 |
||
currentHead = currentHead.next | ||
} | ||
return answerRoot | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution { | ||
// 시간 : O(mn) 공간 : O(m+n) | ||
// 변경해야되는 position 찾아 yList, xList에 각각 y,x값을 담되 set을 이용하여 중복을 제거한다. | ||
// yList, xList를 조회하며 matrix를 변경해준다. | ||
fun setZeroes(matrix: Array<IntArray>): Unit { | ||
val yList = mutableSetOf<Int>() | ||
val xList = mutableSetOf<Int>() | ||
for (y in 0 until matrix.size) { | ||
for (x in 0 until matrix[y].size) { | ||
if (matrix[y][x] == 0) { | ||
yList.add(y) | ||
xList.add(x) | ||
} | ||
} | ||
} | ||
yList.forEach { y -> | ||
for (x in 0 until matrix[y].size) { | ||
matrix[y][x] = 0 | ||
} | ||
} | ||
xList.forEach { x -> | ||
for (y in 0 until matrix.size) { | ||
matrix[y][x] = 0 | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution { | ||
// 시간 : O(m*n) 공간 : O(m*n) | ||
// dp 알고리즘. pathMap[y][x]로 올수 있는 경로의 수는 | ||
// 위쪽(pathMap[y - 1][x]) + 왼쪽( pathMap[y][x - 1]) 경로의 수의 합이다. | ||
Comment on lines
+3
to
+4
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.
|
||
fun uniquePaths(m: Int, n: Int): Int { | ||
val pathMap = Array(m) { y -> | ||
IntArray(n) { x -> | ||
if (y == 0 || x == 0) { | ||
1 | ||
} else { | ||
0 | ||
} | ||
} | ||
} | ||
for (y in 1 until m) { | ||
for (x in 1 until n) { | ||
pathMap[y][x] = pathMap[y - 1][x] + pathMap[y][x - 1] | ||
} | ||
} | ||
|
||
return pathMap[m - 1][n - 1] | ||
} | ||
} |
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.
이 로직이 오류를 유발합니다
답안 통과를 하지 못하고 있는데, 인지하고 계신가요?