diff --git a/longest-substring-without-repeating-characters/EcoFriendlyAppleSu.kt b/longest-substring-without-repeating-characters/EcoFriendlyAppleSu.kt new file mode 100644 index 000000000..f924077f9 --- /dev/null +++ b/longest-substring-without-repeating-characters/EcoFriendlyAppleSu.kt @@ -0,0 +1,24 @@ +package leetcode_study + +/* +* 주어진 문자열에서 반복되는 문자를 포함하지 않은 가장 긴 부분 문자열의 길이를 구하는 문제 +* 이중 반복문을 사용해 바깥 반복문에선 시작 문자를 내부 반복문에선 그 다음 문자를 순회하며 중복된 문자가 발견되었을 때, 반복문 탈출하는 방법으로 문제 해결 +* 시간 복잡도: O(n^2) +* -> 이중 반복 과정 +* 공간 복잡도: O(n) +* -> 중복되지 않은 문자열을 담을 공간 필요 +* */ +fun lengthOfLongestSubstring(s: String): Int { + var maximumLength = 0 + for (start in s.indices) { + val tempList = mutableListOf() + for (end in start until s.length) { + if (tempList.contains(s[end])) { + break // escape loop + } + tempList.add(s[end]) + maximumLength = max(tempList.size, maximumLength) + } + } + return maximumLength +} diff --git a/number-of-islands/EcoFriendlyAppleSu.kt b/number-of-islands/EcoFriendlyAppleSu.kt new file mode 100644 index 000000000..8aa9a55ac --- /dev/null +++ b/number-of-islands/EcoFriendlyAppleSu.kt @@ -0,0 +1,47 @@ +package leetcode_study + + +/* +* 섬의 개수를 구하는 문제 +* bfs를 사용해 문제 해결 +* 시간 복잡도: O(n^2) +* -> 이중 반복문을 통해 모든 배열을 순회 +* -> bfs queue 순회 +* 공간 복잡도: O(n^2) +* -> 방문을 표시하는 board +* -> bfs에 사용되는 queue size +* */ +val dx = listOf(0, 1, -1, 0) +val dy = listOf(1, 0, 0, -1) + +fun numIslands(grid: Array): Int { + val board = Array(grid.size) { BooleanArray(grid[0].size) { false } } + var result = 0 + for (i in grid.indices) { + for (j in grid[0].indices) { + if (grid[i][j] == '1' && board[i][j] == false) { + bfs(grid, board, i ,j) + result += 1 + } + } + } + return result +} + +fun bfs(grid: Array, board: Array, x: Int, y: Int) { + val queue = ArrayDeque>() + queue.add(Pair(x, y)) + board[x][y] = true + + while (queue.isNotEmpty()) { + val (row, col) = queue.removeFirst() + for (i in IntRange(0, 3)) { + val nx = row + dx[i] + val ny = col + dy[i] + if (nx >= 0 && nx < grid.size && ny >= 0 && ny < grid[0].size && !board[nx][ny] && grid[nx][ny] == '1') { + queue.add(Pair(nx, ny)) + board[nx][ny] = true + } + } + } +} diff --git a/reverse-linked-list/EcoFriendlyAppleSu.kt b/reverse-linked-list/EcoFriendlyAppleSu.kt new file mode 100644 index 000000000..f3ff5c925 --- /dev/null +++ b/reverse-linked-list/EcoFriendlyAppleSu.kt @@ -0,0 +1,28 @@ +package leetcode_study + +/* +* Node를 역순으로 만드는 문제 +* 추가 저장 공간을 사용해 노드를 채워 둔 뒤 역행하면서 포인터 값을 변경하는 방법으로 해결 +* 시간 복잡도: O(n) +* -> 새로운 공간에 값을 추가하는 반복문: O(n) +* -> 각 노드에 접근하여 포인터 값을 변경하는 반복문: O(n) +* 공간 복잡도: O(n) +* -> 주어진 노드를 저장할 수 있을만큼 저장 공간 필요: O(n) +* */ +fun reverseList(head: ListNode?): ListNode? { + val stack = mutableListOf() + var currentNode = head + if (head == null) return null + + while (currentNode != null) { + stack.add(currentNode) + currentNode = currentNode.next + } + + + for (i in stack.size - 1 downTo 1) { + stack[i].next = stack[i-1] + } + stack[0].next = null + return stack[stack.size -1] +} diff --git a/set-matrix-zeroes/EcoFriendlyAppleSu.kt b/set-matrix-zeroes/EcoFriendlyAppleSu.kt new file mode 100644 index 000000000..b84e895cb --- /dev/null +++ b/set-matrix-zeroes/EcoFriendlyAppleSu.kt @@ -0,0 +1,43 @@ +package leetcode_study + +/* +* board에서 0이 발견 되었을 때, 기준 row, column을 모두 0으로 변경하는 문제 +* Set 자료구조를 사용해 중복을 없애고 순회하여 문제 해결 +* 시간 복잡도: O(n^2) (정확히는 O(row * col)) +* -> board의 row, col을 순회해 0을 찾는 과정 +* -> set으로 중복을 제거한 row을 순회해 board의 값을 0으로 변경하는 과정 +* -> set으로 중복을 제거한 column을 순회해 board의 값을 0으로 변경하는 과정 +* 공간 복잡도: O(1) +* -> 중복을 제거한 rowSet, columnSet을 저장하는 공간 +* */ +fun setZeroes(matrix: Array): Unit { + val rowSet = mutableSetOf() + val colSet = mutableSetOf() + + val rowSize = matrix.size + val colSize = matrix[0].size + + // find row col + for (row in matrix.indices) { + for (col in matrix[0].indices) { + if (matrix[row][col] == 0) { + rowSet.add(row) + colSet.add(col) + } + } + } + + if (rowSet.size == 0 && colSet.size == 0) return + + for (row in rowSet) { + for (index in IntRange(0, colSize - 1)) { + matrix[row][index] = 0 + } + } + + for (col in colSet) { + for(index in IntRange(0, rowSize - 1)) { + matrix[index][col] = 0 + } + } +} diff --git a/unique-paths/EcoFriendlyAppleSu.kt b/unique-paths/EcoFriendlyAppleSu.kt new file mode 100644 index 000000000..1f956b16d --- /dev/null +++ b/unique-paths/EcoFriendlyAppleSu.kt @@ -0,0 +1,29 @@ +package leetcode_study + +/* +* 주어진 m,n size board 위에서 좌측 위부터 우측 아래까지 도착하는 Unique path의 개수를 구하는 방법 (m = row size, n = col size) +* 움직일 수 있는 방향이 아래와 오른쪽으로 정해저 있는 상황에서 다음 칸으로 갈 수 있는 방법은 아래와 같음 +* board[i][j] = board[i][j-1] + board[i-1][j] +* 시간 복잡도: O(mn) +* -> board를 순회하며 unique path를 구하는 과정 +* 공간 복잡도: O(mn) +* -> m, n을 사용해 board를 구성 +* */ +fun uniquePaths(m: Int, n: Int): Int { + val board = Array(m) { IntArray(n) { 0 } } + + for (i in IntRange(0, m-1)) { + board[i][0] = 1 + } + for (i in IntRange(0, n-1)) { + board[0][i] = 1 + } + + for (i in 1 until m) { + for (j in 1 until n) { + board[i][j] = board[i][j-1] + board[i-1][j] + } + } + + return board[m-1][n-1] +}