Skip to content
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

[eunhwa99] week 7 #916

Merged
merged 9 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions longest-substring-without-repeating-characters/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.HashMap;
import java.util.Map;

class Solution {
// 시간 복잡도: O(N)
// 공간 복잡도: O(N)
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> position = new HashMap<>();
int start = 0; // substring의 시작점
int maxLength = 0;

for (int idx = 0; idx < s.length(); idx++) {
char currentChar = s.charAt(idx);

// 같은 문자가 이미 map 에 있고, 그 문자가 현재 substring에 포함된 문자인지 확인
if (position.containsKey(currentChar) && position.get(currentChar) >= start) {
start = position.get(currentChar) + 1;
// 같은 문자가 포함되지 않게 substring의 시작을 옮긴다.
}

maxLength = Math.max(maxLength, idx - start + 1);

position.put(currentChar, idx);
}

return maxLength;
}
}

67 changes: 67 additions & 0 deletions number-of-islands/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.LinkedList;
import java.util.Queue;

// BFS 사용
// 시간 복잡도 : O(MxN)
// 공간 복잡도: O(MxN)
class Solution {

int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

public int numIslands(char[][] grid) {
int row = grid.length;
int col = grid[0].length;

int total = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == '1') {
total++;
BFS(grid, i, j, row, col);
System.out.println(grid[i][j]);
}
}
}
return total;
}

private void BFS(char[][] grid, int r, int c, int sizeR, int sizeC) {
Queue<Position> queue = new LinkedList<>();
eunhwa99 marked this conversation as resolved.
Show resolved Hide resolved

queue.add(new Position(r, c));
grid[r][c] = '0'; // '0'으로 변경 (방문 체크)

while (!queue.isEmpty()) {
Position current = queue.poll();
int curR = current.r;
int curC = current.c;

for (int i = 0; i < 4; i++) {
int dirR = dir[i][0];
int dirC = dir[i][1];

int nextR = curR + dirR;
int nextC = curC + dirC;

if (nextR < 0 || nextR >= sizeR || nextC < 0 || nextC >= sizeC || grid[nextR][nextC] == '0') {
continue;
}
queue.add(new Position(nextR, nextC));
grid[nextR][nextC] = '0';
}
}

}

static class Position {

int r;
int c;

Position(int r, int c) {
this.r = r;
this.c = c;
}
}
}

29 changes: 29 additions & 0 deletions reverse-linked-list/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/

// 시간 복잡도: O(N)
// 공간복잡도: O(N)
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null) return head;

ListNode pointer = new ListNode(head.val);

ListNode tempPointer;
while(head.next!=null){
tempPointer = new ListNode(head.next.val, pointer);
pointer = tempPointer;
head = head.next;
}
}
return pointer;
}

41 changes: 41 additions & 0 deletions set-matrix-zeroes/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.HashSet;
import java.util.Set;

// 시간 복잡도: O(row x col)
// 공간 복잡도: O(row + col)
class Solution {

public void setZeroes(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;

// 행과 열에 0이 있는지 체크할 배열
Set<Integer> rowZero = new HashSet<>();
Set<Integer> colZero = new HashSet<>();
eunhwa99 marked this conversation as resolved.
Show resolved Hide resolved

for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] == 0) {
rowZero.add(i);
colZero.add(j);
}
}
}

// 행을 0으로 설정
for (int r : rowZero) {
for (int c = 0; c < col; c++) {
matrix[r][c] = 0;
}
}

// 열을 0으로 설정
for (int c : colZero) {
for (int r = 0; r < row; r++) {
matrix[r][c] = 0;
}
}
}
}


23 changes: 23 additions & 0 deletions unique-paths/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {

public int uniquePaths(int m, int n) {
int[][] paths = new int[m][n];

for (int i = 0; i < m; i++) {
paths[i][0] = 1; //가장 왼쪽 줄은 항상 경로가 1개
}

for (int i = 0; i < n; i++) {
paths[0][i] = 1; // 가장 윗줄은 항상 경로가 1개
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {

paths[i][j] = paths[i - 1][j] + paths[i][j - 1];
}
}

return paths[m - 1][n - 1];
}
}

Loading