-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Akku] week 7 #954
Merged
Merged
[Akku] week 7 #954
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
longest-substring-without-repeating-characters/imsosleepy.java
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 @@ | ||
// 각 문자를 한번씩 처리하도록 슬라이딩 윈도우 방식을 채택 | ||
// 시간복잡도 : O(n) - 각 문자를 한 번씩 처리 | ||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
int maxLength = 0; | ||
int startIdx = 0; | ||
String currentSubstring = ""; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
int duplicateIdx = currentSubstring.indexOf(s.charAt(i)); | ||
|
||
// 중복 문자가 발견되면 윈도우의 시작 위치를 조정 | ||
if (duplicateIdx != -1) { | ||
startIdx = startIdx + duplicateIdx + 1; | ||
} | ||
|
||
currentSubstring = s.substring(startIdx, i + 1); | ||
maxLength = Math.max(maxLength, currentSubstring.length()); | ||
} | ||
|
||
return maxLength; | ||
} | ||
} |
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,46 @@ | ||
// DFS를 이용한 풀이도 있는데 개인적으로 BFS를 더 선호함 | ||
class Solution { | ||
public int numIslands(char[][] grid) { | ||
if (grid == null || grid.length == 0) return 0; | ||
|
||
int numIslands = 0; | ||
int rows = grid.length, cols = grid[0].length; | ||
|
||
for (int row = 0; row < rows; row++) { | ||
for (int col = 0; col < cols; col++) { | ||
if (grid[row][col] == '1') { | ||
numIslands++; | ||
bfs(grid, row, col); | ||
} | ||
} | ||
} | ||
|
||
return numIslands; | ||
} | ||
|
||
private void bfs(char[][] grid, int startRow, int startCol) { | ||
int rows = grid.length; | ||
int cols = grid[0].length; | ||
|
||
Queue<int[]> queue = new LinkedList<>(); | ||
queue.offer(new int[]{startRow, startCol}); | ||
|
||
grid[startRow][startCol] = '0'; | ||
|
||
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; | ||
|
||
while (!queue.isEmpty()) { | ||
int[] cell = queue.poll(); | ||
int row = cell[0], col = cell[1]; | ||
|
||
for (int[] dir : directions) { | ||
int newRow = row + dir[0]; | ||
|
||
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && grid[newRow][newCol] == '1') { | ||
queue.offer(new int[]{newRow, newCol}); | ||
grid[newRow][newCol] = '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,17 @@ | ||
// 이전 노드의 헤드와 현재노드 헤드를 하나씩 이동하면서 교환하면 자연스럽게 연결리스트가 뒤집힌다. | ||
// 각 노드를 한번씩 방문하므로 O(N)의 시간복잡도를 갖는다. | ||
class Solution { | ||
public ListNode reverseList(ListNode head) { | ||
ListNode prev = null; | ||
ListNode current = head; | ||
|
||
while (current != null) { | ||
ListNode nextNode = current.next; | ||
current.next = prev; | ||
prev = current; | ||
current = nextNode; | ||
} | ||
|
||
return prev; | ||
} | ||
} |
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,57 @@ | ||
// 주어진 조건을 그대로 시뮬레이션으로 구현 | ||
class Solution { | ||
public void setZeroes(int[][] matrix) { | ||
int rows = matrix.length; | ||
int cols = matrix[0].length; | ||
boolean firstRowZero = false; | ||
boolean firstColZero = false; | ||
|
||
// 1. 첫 행과 첫 열에 0이 있는지 확인 | ||
for (int r = 0; r < rows; r++) { | ||
if (matrix[r][0] == 0) { | ||
firstColZero = true; | ||
break; | ||
} | ||
} | ||
|
||
for (int c = 0; c < cols; c++) { | ||
if (matrix[0][c] == 0) { | ||
firstRowZero = true; | ||
break; | ||
} | ||
} | ||
|
||
// 2. 나머지 셀을 탐색하며 첫 행과 첫 열에 0 기록 | ||
for (int r = 1; r < rows; r++) { | ||
for (int c = 1; c < cols; c++) { | ||
if (matrix[r][c] == 0) { | ||
matrix[r][0] = 0; | ||
matrix[0][c] = 0; | ||
} | ||
} | ||
} | ||
|
||
// 3. 첫 행과 첫 열 정보를 바탕으로 나머지 셀을 0으로 설정 | ||
for (int r = 1; r < rows; r++) { | ||
for (int c = 1; c < cols; c++) { | ||
if (matrix[r][0] == 0 || matrix[0][c] == 0) { | ||
matrix[r][c] = 0; | ||
} | ||
} | ||
} | ||
|
||
// 4. 첫 행 처리 | ||
if (firstRowZero) { | ||
for (int c = 0; c < cols; c++) { | ||
matrix[0][c] = 0; | ||
} | ||
} | ||
|
||
// 5. 첫 열 처리 | ||
if (firstColZero) { | ||
for (int r = 0; r < rows; r++) { | ||
matrix[r][0] = 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,20 @@ | ||
// dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 점화식을 세웠는데 초기 데이터 세팅을 1로 맞추면 되는 것 때문에 진행이 안됐었음 | ||
// 결과적으로 GPT의 도움을 받음 | ||
class Solution { | ||
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]; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
current에 head를 넣어 사용한다면, head를 그대로 사용해도 괜찮지 않을까요?