Skip to content
Open
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
28 changes: 28 additions & 0 deletions longest-substring-without-repeating-characters/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Sliding Window
  • 설명: 이 코드는 연속된 문자열 구간을 탐색하며 중복을 제거하는 슬라이딩 윈도우 기법을 사용하여 최장 길이를 찾는다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int lengthOfLongestSubstring(String s) {
/**
1.prob: 중복없는 가장 긴 substring length return
2.constraints:
- alphabet, digit, space 로 구성
- s.length min = 0, max = 50,000
3.solution
- slding window, time: O(n), space: O(n) or O(1)
*/

int maxLen = 0;
int left = 0;
Set<Character> visited = new HashSet<>();

for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
while(visited.contains(ch)) {
visited.remove(s.charAt(left));
left += 1;
}
visited.add(ch);
maxLen = Math.max(maxLen, i - left + 1);
}

return maxLen;
}
}
43 changes: 43 additions & 0 deletions number-of-islands/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 깊이 우선 탐색(DFS)을 활용하여 연결된 섬을 탐색하고 개수를 세는 방식으로 문제를 해결합니다. 재귀 호출을 통해 인접한 땅을 방문하며 섬을 구분합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {

public int numIslands(char[][] grid) {
/**
1.prob:섬 개수 구하기
2.constraints:
- 원소값 0 or 1
- m,n 길이는 최소 = 1, 최대 = 300
3.solution: dfs
*/

//m: 세로, n: 가로 길이
int m = grid.length;
int n = grid[0].length;
int count = 0;

for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(grid[i][j] == '1' && grid[i][j] != '#') {
dfs(i, j, grid, m, n);
count += 1;
}
}
}
return count;
}
void dfs(int i, int j, char[][] grid, int m, int n) {
if(i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') {
return;
}
//이미 visited
if(grid[i][j] == '#') {
return;
}
//방문 체크
grid[i][j] = '#';

dfs(i+1, j, grid, m, n);
dfs(i-1, j, grid, m, n);
dfs(i, j+1, grid, m, n);
dfs(i, j-1, grid, m, n);
}
}
9 changes: 4 additions & 5 deletions number-of-islands/hyeri0903.py
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: DFS
  • 설명: 이 코드는 깊이 우선 탐색(DFS)을 이용하여 연결된 섬을 탐색하고 카운트하는 방식으로 문제를 해결합니다. 재귀 호출을 통해 인접한 땅을 방문하며 섬의 개수를 셉니다.

Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ def numIslands(self, grid: List[List[str]]) -> int:
'''
m = len(grid)
n = len(grid[0])
visited = [ [0] * n for _ in range(m)]
count = 0

def dfs(i, j):
#범위 벗어나면 return
if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] == "0":
return

if visited[i][j] == 1:
if grid[i][j] == "#":
return

visited[i][j] = 1 #방문표시
#방문표시
grid[i][j] = "#"

dfs(i+1, j)
dfs(i-1, j)
Expand All @@ -26,7 +25,7 @@ def dfs(i, j):

for i in range(m):
for j in range(n):
if grid[i][j] == "1" and visited[i][j] == 0:
if grid[i][j] == "1" and grid[i][j] != "#":
dfs(i,j)
count += 1
return count
Expand Down
36 changes: 36 additions & 0 deletions reverse-linked-list/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Stack / Priority Queue
  • 설명: 이 코드는 스택을 이용하여 리스트 값을 역순으로 재구성하는 방식으로, 스택 자료구조를 활용하는 패턴에 속합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) {
return null;
}

Deque<Integer> stack = new ArrayDeque<>();
ListNode node = head;

while(node != null) {
stack.push(node.val);
node = node.next;
}

ListNode dummy = new ListNode();
ListNode cur = dummy;

while(stack.size() > 0) {
int val = stack.pop();
cur.next = new ListNode(val);
cur = cur.next;
}

return dummy.next;
}
}
37 changes: 37 additions & 0 deletions set-matrix-zeroes/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Matrix Manipulation
  • 설명: 이 코드는 2차원 배열에서 특정 조건에 따라 행과 열을 변경하는 문제로, 패턴 목록에 없는 'Matrix Manipulation'에 해당합니다. 주어진 조건에 따라 행과 열을 0으로 만드는 방식으로 문제를 해결합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public void setZeroes(int[][] matrix) {
/**
1.문제: 0이 존재하는 위치의 모든 row, column을 0으로 set
2.constraints:
- m,n min = 1, max = 200
- space: O(mn)으로 풀이하지말 것,
3.solution
- 0의 위치를 확인 -> 0의 위치는 여러개일 수 있음
- row, col 각각 0의 위치 저장
- time: O(mn), space O(m+n)
*/
int m = matrix.length;
int n = matrix[0].length;
int[] row = new int[m]; //0이 존재하는 row 위치이면 1, 아니면 0
int[] col = new int[n]; //0이 존재하는 col 위치이면 1, 아니면 0

int x = 0; int y = 0; //0의 위치

for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(matrix[i][j] == 0) {
row[i] = 1;
col[j] = 1;
}
}
}

for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(row[i] == 1 || col[j] == 1) {
matrix[i][j] = 0;
}
}
}
}
}
33 changes: 33 additions & 0 deletions unique-paths/hyeri0903.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 2차원 DP 배열을 활용하여 각 위치까지의 경로 수를 누적 계산하는 방식으로 문제를 해결합니다. 이전 상태를 기반으로 최적의 해를 구하는 대표적인 DP 패턴입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public int uniquePaths(int m, int n) {
/**
1.문제: finish로 가는 unique path 수
2.constraints:
- m: 세로, n = 가로
- m,n min = 1, max = 100
- right, down 으로만 움직이기 가능
3.solution
- dfs ?? => x, 모든 경로 탐색하지 않음
- dp => [i][j] = [i-1][j] + [i][j-1]
- time: O(mn), space: O(n)
*/

int[][] dp = new int[m][n];

//첫행, 첫열 = 1
for(int i =0; i<m; i++) {
dp[i][0] = 1;
}

for(int i = 0; i < n; i++) {
dp[0][i] = 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];
}
}
Loading