diff --git a/longest-substring-without-repeating-characters/Tessa1217.java b/longest-substring-without-repeating-characters/Tessa1217.java new file mode 100644 index 000000000..836a6928e --- /dev/null +++ b/longest-substring-without-repeating-characters/Tessa1217.java @@ -0,0 +1,29 @@ +/** + * 문자열 s가 주어질 때 중복 문자가 없는 가장 긴 문자열 길이를 반환하세요. + * */ +class Solution { + // 시간복잡도: O(n) + public int lengthOfLongestSubstring(String s) { + + int maxLength = 0; + + int left = 0; + int right = 0; + + // 알파벳 (대소문자), 숫자, 특수문자, 공백 + boolean[] visited = new boolean[128]; + + while (right < s.length()) { + while (visited[s.charAt(right)]) { + visited[s.charAt(left)] = false; + left++; + } + visited[s.charAt(right)] = true; + maxLength = Math.max(right - left + 1, maxLength); + right++; + } + + return maxLength; + } +} + diff --git a/number-of-islands/Tessa1217.java b/number-of-islands/Tessa1217.java new file mode 100644 index 000000000..422599e51 --- /dev/null +++ b/number-of-islands/Tessa1217.java @@ -0,0 +1,67 @@ +import java.util.LinkedList; +import java.util.Queue; + +/** + * m x n 2D 행렬 grid가 주어질 때 섬의 수를 찾아서 반환하세요. + * 섬(island)는 물(0)로 둘러싸여 있고 가로 또는 세로로 인접한 섬들과 연결되어 형성되어 있다. + */ +class Solution { + + int[] dx = {-1, 1, 0, 0}; + int[] dy = {0, 0, 1, -1}; + + public int numIslands(char[][] grid) { + int cnt = 0; + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + // land라면 + if (grid[i][j] == '1') { + + // bfs로 섬 탐색 + bfs(i, j, grid); + + // dfs로 섬 탐색 + dfs(i, j, grid); + + cnt++; + } + } + } + return cnt; + } + + private void dfs(int x, int y, char[][] grid) { + if (grid[x][y] == '1') { + grid[x][y] = '0'; + } + for (int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0].length && grid[nx][ny] == '1') { + dfs(nx, ny, grid); + } + } + } + + private void bfs(int startX, int startY, char[][] grid) { + + Queue queue = new LinkedList<>(); + queue.offer(new int[]{startX, startY}); + grid[startX][startY] = '0'; + + while (!queue.isEmpty()) { + int[] current = queue.poll(); + for (int i = 0; i < 4; i++) { + int newX = current[0] + dx[i]; + int newY = current[1] + dy[i]; + if (newX >= 0 && newX < grid.length && newY >= 0 && newY < grid[0].length) { + if (grid[newX][newY] == '1') { + queue.offer(new int[]{newX, newY}); + grid[newX][newY] = '0'; + } + } + } + } + } +} + diff --git a/reverse-linked-list/Tessa1217.java b/reverse-linked-list/Tessa1217.java new file mode 100644 index 000000000..4b9e6160a --- /dev/null +++ b/reverse-linked-list/Tessa1217.java @@ -0,0 +1,36 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +/** + * 링크드 리스트 head가 주어질 때 뒤집은 리스트를 반환하세요. + */ +class Solution { + public ListNode reverseList(ListNode head) { + + if (head == null) { + return head; + } + + ListNode prev = head; + ListNode next = prev.next; + head.next = null; + + while (next != null) { + ListNode temp = next.next; + next.next = prev; + prev = next; + next = temp; + } + + return prev; + } + +} + diff --git a/set-matrix-zeroes/Tessa1217.java b/set-matrix-zeroes/Tessa1217.java new file mode 100644 index 000000000..36ed481f7 --- /dev/null +++ b/set-matrix-zeroes/Tessa1217.java @@ -0,0 +1,79 @@ +import java.util.Arrays; + +/** + * m x n 행렬 matrix가 주어질 때, 요소가 0이라면 0을 포함하는 해당 요소를 포함하는 전체 행과 열을 + * 0으로 세팅하세요. + */ +class Solution { + + // Follow Up : 공간 복잡도 개선 필요 + // mark first row and column as marker to set 0's + // 시간복잡도: O(m * n), 공간복잡도: O(1) + public void setZeroes(int[][] matrix) { + + boolean firstZero = false; + + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (matrix[i][j] == 0) { + matrix[i][0] = 0; + if (j == 0) { + firstZero = true; + } else { + matrix[0][j] = 0; + } + } + } + } + + for (int i = 1; i < matrix.length; i++) { + for (int j = 1; j < matrix[0].length; j++) { + if (matrix[i][0] == 0 || matrix[0][j] == 0) { + matrix[i][j] = 0; + } + } + } + + if (matrix[0][0] == 0) { + Arrays.fill(matrix[0], 0); + } + + if (firstZero) { + for (int i = 0; i < matrix.length; i++) { + matrix[i][0] = 0; + } + } + } + + // 시간복잡도: O (m * n), 공간복잡도: O(m + n) + // public void setZeroes(int[][] matrix) { + + // // 0을 포함하는 행과 열의 위치 저장 + // Set rowsContainZeros = new HashSet<>(); + // Set colsContainZeros = new HashSet<>(); + + // for (int i = 0; i < matrix.length; i++) { + // for (int j = 0; j < matrix[0].length; j++) { + // if (matrix[i][j] == 0) { + // rowsContainZeros.add(i); + // colsContainZeros.add(j); + // } + // } + // } + + // for (int row : rowsContainZeros) { + // for (int j = 0; j < matrix[0].length; j++) { + // matrix[row][j] = 0; + // } + // } + + // for (int col : colsContainZeros) { + // for (int i = 0; i < matrix.length; i++) { + // matrix[i][col] = 0; + // } + // } + // } + + +} + diff --git a/unique-paths/Tessa1217.java b/unique-paths/Tessa1217.java new file mode 100644 index 000000000..5e164f6e7 --- /dev/null +++ b/unique-paths/Tessa1217.java @@ -0,0 +1,64 @@ +/** + * m x n 행렬이 있을 때 로봇이 상단-좌측 가장자리에서 하단-우측 가장자리로 갈 수 있는 경우의 수 구하기 + * 로봇은 오른쪽 또는 밑으로만 움직일 수 있음 + */ +class Solution { + + // DFS 시간 초과로 DP로 구현 + // 시간복잡도: O(m * n) + public int uniquePaths(int m, int n) { + int[][] dp = new int[m][n]; + + // 첫째 열 + for (int i = 0; i < m; i++) { + dp[i][0] = 1; + } + + // 첫째 행 + for (int j = 0; j < n; j++) { + dp[0][j] = 1; + } + + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; // 위, 왼쪽 값 + } + } + + return dp[m - 1][n - 1]; + + } + + // DFS 시간 초과 + // private int cnt = 0; + // int[] dx = {1, 0}; + // int[] dy = {0, 1}; + + // public int uniquePaths(int m, int n) { + // int[][] path = new int[m][n]; + // dfs(0, 0, path); + // return cnt; + // } + + // private void dfs(int x, int y, int[][] path) { + + // if (x == path.length - 1 && y == path[0].length - 1) { + // cnt++; + // return; + // } + + // path[x][y] = 1; + + // for (int i = 0; i < 2; i++) { + // int nx = x + dx[i]; + // int ny = y + dy[i]; + // if (nx >= 0 && nx < path.length && ny >= 0 && ny < path[0].length && path[nx][ny] != 1) { + // dfs(nx, ny, path); + // } + // } + + // path[x][y] = 0; + + // } +} +