Skip to content

[jinhyungrhee] WEEK 07 solutions #1456

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

Merged
merged 5 commits into from
May 16, 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
45 changes: 45 additions & 0 deletions longest-substring-without-repeating-characters/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.*;
class Solution {
public int lengthOfLongestSubstring(String s) {

if (s.length() == 0) return 0;
if (s.length() == 1) return 1;

// sliding window
HashSet<Character> table = new HashSet<>();

int left = 0;
int right = 1;
int end = s.length() - 1;

table.add(s.charAt(left));

int maxSize = 0;
while (right <= end) {

if (!table.contains(s.charAt(right))) {
table.add(s.charAt(right));
} else {
/** [중복된 문자면 슬라이딩 윈도우 이동]
1. 중복 문자를 만나면 left를 한칸씩 증가
2. 중복 문자가 Hash 에서 사라질 때까지 왼쪽 값 제거
3. 왼쪽 중복문자가 제거되었을 때 right 이동
*/
while(table.contains(s.charAt(right))) {
table.remove(s.charAt(left));
left++;
}
table.add(s.charAt(right));
}

int tableSize = table.size();
if (tableSize > maxSize) maxSize = tableSize;

right++;
}

return maxSize;
}

}

Copy link
Contributor

Choose a reason for hiding this comment

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

슬라이딩 윈도우와 HashSet으로 효율적으로 푸신거같습니다!

33 changes: 33 additions & 0 deletions number-of-islands/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {

public int numIslands(char[][] grid) {

int[][] dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
boolean[][] visited = new boolean[grid.length][grid[0].length];

int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if ('1' == grid[i][j] && !visited[i][j]) {
dfs(grid, visited, dir , i, j);
count++;
}
}
}
return count;
}

public void dfs(char[][] grid, boolean[][] visited, int[][] dir, int x, int y) {

if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length
&& !visited[x][y] && '1' == grid[x][y]) {

visited[x][y] = true;
for (int i = 0; i < 4; i++) {
int dx = x + dir[i][0];
int dy = y + dir[i][1];
dfs(grid, visited, dir ,dx, dy);
}
}
}
}
16 changes: 16 additions & 0 deletions reverse-linked-list/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public ListNode reverseList(ListNode head) {

ListNode prev = null;
ListNode curr = head;

while(curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}

return prev;
}
}
29 changes: 29 additions & 0 deletions set-matrix-zeroes/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {

static int MARKER = 987654321;
public void setZeroes(int[][] matrix) {

int width = matrix.length;
int height = matrix[0].length;

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if(matrix[i][j] == 0) {
for (int x = 0; x < width; x++) {
if (matrix[x][j] != 0) matrix[x][j] = MARKER;
}
for (int y = 0; y < height; y++) {
if (matrix[i][y] != 0) matrix[i][y] = MARKER;
}
}
}
}

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (matrix[i][j] == MARKER) matrix[i][j] = 0;
}
}

}
}
60 changes: 60 additions & 0 deletions unique-paths/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Solution {
int count;
public int uniquePaths(int m, int n) {
/**
1. dfs + backtrack => Time Limit Exceeded
*/
//boolean[][] visited = new boolean[m][n];
//dfs(visited, 0, 0, m, n);

/**
2. DP
- 저장되는 값은 해당 위치까지 도달할 수 있는 '고유한 경로의 수'
*/
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];
}

/**
1. DFS + backtrack
*/
/**
public void dfs(boolean[][] visited, int x, int y, int m, int n) {

if (x == m - 1 && y == n - 1) {
count++;
return;
}


visited[x][y] = true;

// 오른쪽으로 이동 + backtrack
if (y + 1 < n && !visited[x][y + 1]) {
dfs(visited, x, y + 1, m , n);
}

// 아래쪽으로 이동 + backtrack
if (x + 1 < m && !visited[x + 1][y]) {
dfs(visited, x + 1, y, m, n);
}

visited[x][y] = false;
}
*/
}