From 80151b38563d97d5a8d8f474aa81ffac64140383 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Mon, 12 May 2025 19:47:42 +0900 Subject: [PATCH 1/6] add number of islands solution --- number-of-islands/Tessa1217.java | 67 ++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 number-of-islands/Tessa1217.java 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'; + } + } + } + } + } +} + From 019027cf8e08183c47e160c83aac794eb1cd2ce5 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Mon, 12 May 2025 19:48:41 +0900 Subject: [PATCH 2/6] add reverse linked list solution --- reverse-linked-list/Tessa1217.java | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 reverse-linked-list/Tessa1217.java 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; + } + +} + From 9612086c95b94898d5b5de25bf1dd002f71b5db4 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Mon, 12 May 2025 20:43:06 +0900 Subject: [PATCH 3/6] add unique paths solution --- unique-paths/Tessa1217.java | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 unique-paths/Tessa1217.java 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; + + // } +} + From c65779b6515146208dc55056b51ab52ca8fdfcd9 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Mon, 12 May 2025 21:01:39 +0900 Subject: [PATCH 4/6] add longest substring without repeating characters solution --- .../Tessa1217.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 longest-substring-without-repeating-characters/Tessa1217.java 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; + } +} + From f330cc10bb4c49e0fe62aa74cf435033ff45de92 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Mon, 12 May 2025 23:00:08 +0900 Subject: [PATCH 5/6] add set matrix zeroes solution --- set-matrix-zeroes/Tessa1217.java | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 set-matrix-zeroes/Tessa1217.java 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; + // } + // } + // } + + +} + From e75a8119f242c3c53d1c971a2cbd879a548b021c Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Fri, 16 May 2025 18:33:51 +0900 Subject: [PATCH 6/6] =?UTF-8?q?bfs=20=EC=A3=BC=EC=84=9D=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- number-of-islands/Tessa1217.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number-of-islands/Tessa1217.java b/number-of-islands/Tessa1217.java index 422599e51..92a4baba6 100644 --- a/number-of-islands/Tessa1217.java +++ b/number-of-islands/Tessa1217.java @@ -18,7 +18,7 @@ public int numIslands(char[][] grid) { if (grid[i][j] == '1') { // bfs로 섬 탐색 - bfs(i, j, grid); + // bfs(i, j, grid); // dfs로 섬 탐색 dfs(i, j, grid);