-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #913 from gmlwls96/main
[gmlwls96] Week7
- Loading branch information
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
longest-substring-without-repeating-characters/gmlwls96.kt
This file contains 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,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 | ||
} | ||
} |
This file contains 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,43 @@ | ||
class Solution { | ||
// 시간 : O(N), 공간 O(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 | ||
} | ||
} |
This file contains 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,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 } | ||
currentHead = currentHead.next | ||
} | ||
return answerRoot | ||
} | ||
} |
This file contains 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,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 | ||
} | ||
} | ||
} | ||
} |
This file contains 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,23 @@ | ||
class Solution { | ||
// 시간 : O(m*n) 공간 : O(m*n) | ||
// dp 알고리즘. pathMap[y][x]로 올수 있는 경로의 수는 | ||
// 위쪽(pathMap[y - 1][x]) + 왼쪽( pathMap[y][x - 1]) 경로의 수의 합이다. | ||
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] | ||
} | ||
} |