-
-
Notifications
You must be signed in to change notification settings - Fork 245
[권동현] Week 7 #945
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
Merged
Merged
[권동현] Week 7 #945
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ae6f4c1
solve : #223 (Reverse Linked List) with Java
kdh-92 fcea209
solve : #243 (Longest Substring Without Repeating Characters) with Java
kdh-92 ecd461a
solve : #258 (Number of Islands) with Java
kdh-92 7e89b11
solve : #273 (Unique Paths) with Java
kdh-92 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
25 changes: 25 additions & 0 deletions
25
longest-substring-without-repeating-characters/kdh-92.java
This file contains hidden or 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,25 @@ | ||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
// HashSet 풀이 | ||
// 시간복잡도 : O(n), 공간복잡도 O(1) | ||
// 풀이 | ||
// HashSet에 동일한 문자가 있는지 체크하고 동일한 문자가 있으면 왼쪽 기준점을 하나씩 이동 (동일 문자가 없을 때까지 반복) | ||
// 동일한 문자가 없을 때 현재 기준(right) - 왼쪽 기준(left) + 1 의 최대 길이를 maxLength 저장 후 반환 | ||
|
||
int left = 0; | ||
int maxLength = 0; | ||
HashSet<Character> charSet = new HashSet<>(); | ||
|
||
for (int right = 0; right < s.length(); right++) { | ||
while (charSet.contains(s.charAt(right))) { | ||
charSet.remove(s.charAt(left)); | ||
left++; | ||
} | ||
|
||
charSet.add(s.charAt(right)); | ||
maxLength = Math.max(maxLength, right - left + 1); | ||
} | ||
|
||
return maxLength; | ||
} | ||
} |
This file contains hidden or 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,45 @@ | ||
class Solution { | ||
public int numIslands(char[][] grid) { | ||
// bfs | ||
// 시간복잡도 : O(r * c), 공간복잡도 O(r * c) | ||
// 풀이 | ||
// bfs 풀이에 방문을 Set<String>으로 설정하여 체크, directions(상하좌우)를 통해 탐색 | ||
// 응용 가능 : 대각선 추가하여 연결된 섬 체크 | ||
int islands = 0; | ||
int rows = grid.length; | ||
int cols = grid[0].length; | ||
Set<String> visited = new HashSet<>(); | ||
|
||
int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; | ||
|
||
for (int r = 0; r < rows; r++) { | ||
for (int c = 0; c < cols; c++) { | ||
if (grid[r][c] == '1' && !visited.contains(r + "," + c)) { | ||
islands++; | ||
bfs(grid, r, c, visited, directions, rows, cols); | ||
} | ||
} | ||
} | ||
|
||
return islands; | ||
} | ||
|
||
private void bfs(char[][] grid, int r, int c, Set<String> visited, int[][] directions, int rows, int cols) { | ||
Queue<int[]> q = new LinkedList<>(); | ||
visited.add(r + "," + c); | ||
q.add(new int[]{r, c}); | ||
|
||
while (!q.isEmpty()) { | ||
int[] point = q.poll(); | ||
int row = point[0], col = point[1]; | ||
|
||
for (int[] direction : directions) { | ||
int nr = row + direction[0], nc = col + direction[1]; | ||
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' && !visited.contains(nr + "," + nc)) { | ||
q.add(new int[] {nr, nc}); | ||
visited.add(nr + "," + nc); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,18 @@ | ||
class Solution { | ||
public ListNode reverseList(ListNode head) { | ||
// while 풀이 | ||
// 시간복잡도 : O(n), 공간복잡도 : O(1) | ||
// 핵심 : node에 순서대로 null <- 1 <- 2 <- 3 담는 과정이 필요 | ||
// node = null로 시작하고, 다음 값을 temp에 넣어둔 뒤 순서대로 값 변경 | ||
ListNode node = null; | ||
|
||
while (head != null) { | ||
ListNode temp = head.next; | ||
head.next = node; | ||
node = head; | ||
head = temp; | ||
} | ||
|
||
return node; | ||
} | ||
} |
This file contains hidden or 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,41 @@ | ||
// 풀이 핵심 | ||
// dp나 1차원 배열 2개를 이용하는 방식의 핵심은 결국 현재 값은 왼쪽 값과 상단 값이 더해진 값으로 이루어진다는 것이다. | ||
// dp는 2차원 배열 공간 모두를 사용해야하지만, 1차원 배열 2개의 경우 2n의 공간을 이용해 현재 값을 만들어 나가게 된다. | ||
class Solution { | ||
public int uniquePaths(int m, int n) { | ||
// (1) dp - 2차원 배열 이용 | ||
// 시간복잡도 : O(m * n), 공간복잡도 : O(m * n) | ||
int[][] dp = new int[m][n]; | ||
|
||
for (int i = 0; i < m; i++) { | ||
for (int j = 0; j < n; j++) { | ||
if (i == 0 && j == 0) { | ||
dp[i][j] = 1; | ||
continue; | ||
} | ||
|
||
int pathsFromLeft = (j - 1 >= 0) ? dp[i][j - 1] : 0; | ||
int pathsFromUp = (i - 1 >= 0) ? dp[i - 1][j] : 0; | ||
dp[i][j] = pathsFromLeft + pathsFromUp; | ||
} | ||
} | ||
|
||
return dp[m - 1][n - 1]; | ||
|
||
// (2) 1차원 배열 2개 이용 | ||
// 시간복잡도 : O(m * n), 공간복잡도 : O(n) | ||
int[] aboveRow = new int[n]; | ||
Arrays.fill(aboveRow, 1); | ||
|
||
for (int row = 1; row < m; row++) { | ||
int[] currentRow = new int[n]; | ||
Arrays.fill(currentRow, 1); | ||
for (int col = 1; col < n; col++) { | ||
currentRow[col] = currentRow[col - 1] + aboveRow[col]; | ||
} | ||
aboveRow = currentRow; | ||
} | ||
|
||
return aboveRow[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.
최적화까지 해보신 점 너무 보기 좋습니다 :)
노파심에 말씀드리자면 두 방식 모두 dp입니다