Skip to content

Commit

Permalink
feat : week 7
Browse files Browse the repository at this point in the history
  • Loading branch information
imsosleepy committed Jan 25, 2025
1 parent 227f7d8 commit a4e6b74
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
23 changes: 23 additions & 0 deletions longest-substring-without-repeating-characters/imsosleepy.java
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;
}
}
46 changes: 46 additions & 0 deletions number-of-islands/imsosleepy.java
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';
}
}
}
}
}
17 changes: 17 additions & 0 deletions reverse-linked-list/imsosleepy.java
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;
}
}
57 changes: 57 additions & 0 deletions set-matrix-zeroes/imsosleepy.java
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;
}
}
}
}
20 changes: 20 additions & 0 deletions unique-paths/imsosleepy.java
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];
}

}

0 comments on commit a4e6b74

Please sign in to comment.