-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Tessa1217] Week 07 Solutions #1463
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
Changes from all commits
80151b3
019027c
9612086
c65779b
f330cc1
e75a811
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,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; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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로 섬 탐색 | ||
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. dfs 또는 bfs 하나만 선택해서 사용하는 것으로 주석 처리하면 어떨까합니다. 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. 앗 bfs랑 dfs 성능 비교해본다고 올렸다가 ㅎㅎ 주석을 다 풀고 올렸네요. 감사합니다! |
||
// 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<int[]> 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'; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
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. head.next = null 부분이 처음엔 낯설고 직관적으로 와닿지 않아서 한참을 살펴보았습니다. 하지만, 흐름을 따라가며 이해하니 로직이 깔끔하게 구성된 점이 인상 깊었습니다. |
||
|
||
while (next != null) { | ||
ListNode temp = next.next; | ||
next.next = prev; | ||
prev = next; | ||
next = temp; | ||
} | ||
|
||
return prev; | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> rowsContainZeros = new HashSet<>(); | ||
// Set<Integer> 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; | ||
// } | ||
// } | ||
// } | ||
|
||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
// } | ||
} | ||
|
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.
visited를 고정된 크기의 boolean 배열로 처리해서, 공간을 항상 O(1)로 유지하는 점이 특히 인상 깊었습니다.
문자열 길이에 상관없이 일정한 성능을 유지할 수 있어서 좋은 접근인 것 같아요!