Skip to content

Commit 634e804

Browse files
authored
Merge pull request #945 from kdh-92/main
[권동현] Week 7
2 parents 32f1ecc + 7e89b11 commit 634e804

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int lengthOfLongestSubstring(String s) {
3+
// HashSet 풀이
4+
// 시간복잡도 : O(n), 공간복잡도 O(1)
5+
// 풀이
6+
// HashSet에 동일한 문자가 있는지 체크하고 동일한 문자가 있으면 왼쪽 기준점을 하나씩 이동 (동일 문자가 없을 때까지 반복)
7+
// 동일한 문자가 없을 때 현재 기준(right) - 왼쪽 기준(left) + 1 의 최대 길이를 maxLength 저장 후 반환
8+
9+
int left = 0;
10+
int maxLength = 0;
11+
HashSet<Character> charSet = new HashSet<>();
12+
13+
for (int right = 0; right < s.length(); right++) {
14+
while (charSet.contains(s.charAt(right))) {
15+
charSet.remove(s.charAt(left));
16+
left++;
17+
}
18+
19+
charSet.add(s.charAt(right));
20+
maxLength = Math.max(maxLength, right - left + 1);
21+
}
22+
23+
return maxLength;
24+
}
25+
}

number-of-islands/kdh-92.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public int numIslands(char[][] grid) {
3+
// bfs
4+
// 시간복잡도 : O(r * c), 공간복잡도 O(r * c)
5+
// 풀이
6+
// bfs 풀이에 방문을 Set<String>으로 설정하여 체크, directions(상하좌우)를 통해 탐색
7+
// 응용 가능 : 대각선 추가하여 연결된 섬 체크
8+
int islands = 0;
9+
int rows = grid.length;
10+
int cols = grid[0].length;
11+
Set<String> visited = new HashSet<>();
12+
13+
int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
14+
15+
for (int r = 0; r < rows; r++) {
16+
for (int c = 0; c < cols; c++) {
17+
if (grid[r][c] == '1' && !visited.contains(r + "," + c)) {
18+
islands++;
19+
bfs(grid, r, c, visited, directions, rows, cols);
20+
}
21+
}
22+
}
23+
24+
return islands;
25+
}
26+
27+
private void bfs(char[][] grid, int r, int c, Set<String> visited, int[][] directions, int rows, int cols) {
28+
Queue<int[]> q = new LinkedList<>();
29+
visited.add(r + "," + c);
30+
q.add(new int[]{r, c});
31+
32+
while (!q.isEmpty()) {
33+
int[] point = q.poll();
34+
int row = point[0], col = point[1];
35+
36+
for (int[] direction : directions) {
37+
int nr = row + direction[0], nc = col + direction[1];
38+
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' && !visited.contains(nr + "," + nc)) {
39+
q.add(new int[] {nr, nc});
40+
visited.add(nr + "," + nc);
41+
}
42+
}
43+
}
44+
}
45+
}

reverse-linked-list/kdh-92.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public ListNode reverseList(ListNode head) {
3+
// while 풀이
4+
// 시간복잡도 : O(n), 공간복잡도 : O(1)
5+
// 핵심 : node에 순서대로 null <- 1 <- 2 <- 3 담는 과정이 필요
6+
// node = null로 시작하고, 다음 값을 temp에 넣어둔 뒤 순서대로 값 변경
7+
ListNode node = null;
8+
9+
while (head != null) {
10+
ListNode temp = head.next;
11+
head.next = node;
12+
node = head;
13+
head = temp;
14+
}
15+
16+
return node;
17+
}
18+
}

unique-paths/kdh-92.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// 풀이 핵심
2+
// dp나 1차원 배열 2개를 이용하는 방식의 핵심은 결국 현재 값은 왼쪽 값과 상단 값이 더해진 값으로 이루어진다는 것이다.
3+
// dp는 2차원 배열 공간 모두를 사용해야하지만, 1차원 배열 2개의 경우 2n의 공간을 이용해 현재 값을 만들어 나가게 된다.
4+
class Solution {
5+
public int uniquePaths(int m, int n) {
6+
// (1) dp - 2차원 배열 이용
7+
// 시간복잡도 : O(m * n), 공간복잡도 : O(m * n)
8+
int[][] dp = new int[m][n];
9+
10+
for (int i = 0; i < m; i++) {
11+
for (int j = 0; j < n; j++) {
12+
if (i == 0 && j == 0) {
13+
dp[i][j] = 1;
14+
continue;
15+
}
16+
17+
int pathsFromLeft = (j - 1 >= 0) ? dp[i][j - 1] : 0;
18+
int pathsFromUp = (i - 1 >= 0) ? dp[i - 1][j] : 0;
19+
dp[i][j] = pathsFromLeft + pathsFromUp;
20+
}
21+
}
22+
23+
return dp[m - 1][n - 1];
24+
25+
// (2) 1차원 배열 2개 이용
26+
// 시간복잡도 : O(m * n), 공간복잡도 : O(n)
27+
int[] aboveRow = new int[n];
28+
Arrays.fill(aboveRow, 1);
29+
30+
for (int row = 1; row < m; row++) {
31+
int[] currentRow = new int[n];
32+
Arrays.fill(currentRow, 1);
33+
for (int col = 1; col < n; col++) {
34+
currentRow[col] = currentRow[col - 1] + aboveRow[col];
35+
}
36+
aboveRow = currentRow;
37+
}
38+
39+
return aboveRow[n - 1];
40+
}
41+
}

0 commit comments

Comments
 (0)