Skip to content

Commit f4393df

Browse files
authored
Merge pull request #954 from imsosleepy/main
[Akku] week 7
2 parents cc778ac + a4e6b74 commit f4393df

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ๊ฐ ๋ฌธ์ž๋ฅผ ํ•œ๋ฒˆ์”ฉ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋ฐฉ์‹์„ ์ฑ„ํƒ
2+
// ์‹œ๊ฐ„๋ณต์žก๋„ : O(n) - ๊ฐ ๋ฌธ์ž๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์ฒ˜๋ฆฌ
3+
class Solution {
4+
public int lengthOfLongestSubstring(String s) {
5+
int maxLength = 0;
6+
int startIdx = 0;
7+
String currentSubstring = "";
8+
9+
for (int i = 0; i < s.length(); i++) {
10+
int duplicateIdx = currentSubstring.indexOf(s.charAt(i));
11+
12+
// ์ค‘๋ณต ๋ฌธ์ž๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด ์œˆ๋„์šฐ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์กฐ์ •
13+
if (duplicateIdx != -1) {
14+
startIdx = startIdx + duplicateIdx + 1;
15+
}
16+
17+
currentSubstring = s.substring(startIdx, i + 1);
18+
maxLength = Math.max(maxLength, currentSubstring.length());
19+
}
20+
21+
return maxLength;
22+
}
23+
}

โ€Žnumber-of-islands/imsosleepy.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// DFS๋ฅผ ์ด์šฉํ•œ ํ’€์ด๋„ ์žˆ๋Š”๋ฐ ๊ฐœ์ธ์ ์œผ๋กœ BFS๋ฅผ ๋” ์„ ํ˜ธํ•จ
2+
class Solution {
3+
public int numIslands(char[][] grid) {
4+
if (grid == null || grid.length == 0) return 0;
5+
6+
int numIslands = 0;
7+
int rows = grid.length, cols = grid[0].length;
8+
9+
for (int row = 0; row < rows; row++) {
10+
for (int col = 0; col < cols; col++) {
11+
if (grid[row][col] == '1') {
12+
numIslands++;
13+
bfs(grid, row, col);
14+
}
15+
}
16+
}
17+
18+
return numIslands;
19+
}
20+
21+
private void bfs(char[][] grid, int startRow, int startCol) {
22+
int rows = grid.length;
23+
int cols = grid[0].length;
24+
25+
Queue<int[]> queue = new LinkedList<>();
26+
queue.offer(new int[]{startRow, startCol});
27+
28+
grid[startRow][startCol] = '0';
29+
30+
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
31+
32+
while (!queue.isEmpty()) {
33+
int[] cell = queue.poll();
34+
int row = cell[0], col = cell[1];
35+
36+
for (int[] dir : directions) {
37+
int newRow = row + dir[0];
38+
39+
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && grid[newRow][newCol] == '1') {
40+
queue.offer(new int[]{newRow, newCol});
41+
grid[newRow][newCol] = '0';
42+
}
43+
}
44+
}
45+
}
46+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ์ด์ „ ๋…ธ๋“œ์˜ ํ—ค๋“œ์™€ ํ˜„์žฌ๋…ธ๋“œ ํ—ค๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ด๋™ํ•˜๋ฉด์„œ ๊ตํ™˜ํ•˜๋ฉด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋’ค์ง‘ํžŒ๋‹ค.
2+
// ๊ฐ ๋…ธ๋“œ๋ฅผ ํ•œ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋ฏ€๋กœ O(N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.
3+
class Solution {
4+
public ListNode reverseList(ListNode head) {
5+
ListNode prev = null;
6+
ListNode current = head;
7+
8+
while (current != null) {
9+
ListNode nextNode = current.next;
10+
current.next = prev;
11+
prev = current;
12+
current = nextNode;
13+
}
14+
15+
return prev;
16+
}
17+
}

โ€Žset-matrix-zeroes/imsosleepy.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๊ทธ๋Œ€๋กœ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์œผ๋กœ ๊ตฌํ˜„
2+
class Solution {
3+
public void setZeroes(int[][] matrix) {
4+
int rows = matrix.length;
5+
int cols = matrix[0].length;
6+
boolean firstRowZero = false;
7+
boolean firstColZero = false;
8+
9+
// 1. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์— 0์ด ์žˆ๋Š”์ง€ ํ™•์ธ
10+
for (int r = 0; r < rows; r++) {
11+
if (matrix[r][0] == 0) {
12+
firstColZero = true;
13+
break;
14+
}
15+
}
16+
17+
for (int c = 0; c < cols; c++) {
18+
if (matrix[0][c] == 0) {
19+
firstRowZero = true;
20+
break;
21+
}
22+
}
23+
24+
// 2. ๋‚˜๋จธ์ง€ ์…€์„ ํƒ์ƒ‰ํ•˜๋ฉฐ ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด์— 0 ๊ธฐ๋ก
25+
for (int r = 1; r < rows; r++) {
26+
for (int c = 1; c < cols; c++) {
27+
if (matrix[r][c] == 0) {
28+
matrix[r][0] = 0;
29+
matrix[0][c] = 0;
30+
}
31+
}
32+
}
33+
34+
// 3. ์ฒซ ํ–‰๊ณผ ์ฒซ ์—ด ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋‚˜๋จธ์ง€ ์…€์„ 0์œผ๋กœ ์„ค์ •
35+
for (int r = 1; r < rows; r++) {
36+
for (int c = 1; c < cols; c++) {
37+
if (matrix[r][0] == 0 || matrix[0][c] == 0) {
38+
matrix[r][c] = 0;
39+
}
40+
}
41+
}
42+
43+
// 4. ์ฒซ ํ–‰ ์ฒ˜๋ฆฌ
44+
if (firstRowZero) {
45+
for (int c = 0; c < cols; c++) {
46+
matrix[0][c] = 0;
47+
}
48+
}
49+
50+
// 5. ์ฒซ ์—ด ์ฒ˜๋ฆฌ
51+
if (firstColZero) {
52+
for (int r = 0; r < rows; r++) {
53+
matrix[r][0] = 0;
54+
}
55+
}
56+
}
57+
}

โ€Žunique-paths/imsosleepy.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// dp[i][j] = dp[i - 1][j] + dp[i][j - 1] ์ ํ™”์‹์„ ์„ธ์› ๋Š”๋ฐ ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ ์„ธํŒ…์„ 1๋กœ ๋งž์ถ”๋ฉด ๋˜๋Š” ๊ฒƒ ๋•Œ๋ฌธ์— ์ง„ํ–‰์ด ์•ˆ๋์—ˆ์Œ
2+
// ๊ฒฐ๊ณผ์ ์œผ๋กœ GPT์˜ ๋„์›€์„ ๋ฐ›์Œ
3+
class Solution {
4+
public int uniquePaths(int m, int n) {
5+
6+
int[][] dp = new int[m][n];
7+
8+
for (int i = 0; i < m; i++) dp[i][0] = 1;
9+
for (int j = 0; j < n; j++) dp[0][j] = 1;
10+
11+
for (int i = 1; i < m; i++) {
12+
for (int j = 1; j < n; j++) {
13+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
14+
}
15+
}
16+
17+
return dp[m - 1][n - 1];
18+
}
19+
20+
}

0 commit comments

Comments
ย (0)