Skip to content

Commit

Permalink
Merge pull request #482 from jaejeong1/main
Browse files Browse the repository at this point in the history
[jaejeong1] WEEK 07 Solutions
  • Loading branch information
jaejeong1 authored Sep 29, 2024
2 parents 7718d7b + 8294a51 commit ec10a0d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
55 changes: 55 additions & 0 deletions longest-substring-without-repeating-characters/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.HashSet;
import java.util.Set;

class SolutionLongestSubstring {

public int lengthOfLongestSubstring(String s) {
// ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ผ

// s.length = 0 ๋˜๋Š” 1์ด๋ฉด return s.length()
// lt = 0, rt = 1
// s[lt] != s[rt] ์ด๋ฉด SubString ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•˜๊ณ  True๋ฉด rt++, count++
// s[lt] == s[rt] ์ด๋ฉด ์Œ“์ธ ์นด์šดํŠธ ์ •๋‹ต์— ์ ์šฉํ•˜๊ณ  lt++, rt=lt+1, count ์ดˆ๊ธฐํ™”
// rt๊ฐ€ ๋์— ๋„๋‹ฌํ•˜๋ฉด ๊ทธ๋•Œ๊นŒ์ง€ ์Œ“์ธ ์ •๋‹ต ๋ฐ˜ํ™˜

// TC: O(N*M), ์ „์ฒด ๋ฌธ์ž์—ด ๊ธธ์ด N * ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๊ธธ์ด M
// SC: O(M), ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ƒ์„ฑ ๊ณต๊ฐ„

if (s.length() <= 1) {
return s.length();
}

int lt = 0;
int rt = lt + 1;
int answer = 0;
int count = 0;
while (rt <= s.length()) {
while (rt <= s.length() && isSubstring(s.substring(lt, rt))) {
count++;
rt++;
}
answer = Math.max(answer, count);

lt++;
rt = lt + 1;
count = 0;
}
return answer;
}

// TC: O(M), ๋ถ€๋ถ„ ๋ฌธ์ž์—ด str์— ์ค‘๋ณต์ด ์—†๋Š” ๊ฒฝ์šฐ str์˜ ๊ธธ์ด
// SC: O(M), ๋ถ€๋ถ„ ๋ฌธ์ž์—ด str์˜ ์ค‘๋ณต์ด ์—†๋Š” ๊ฒฝ์šฐ str์˜ ๊ธธ์ด
private boolean isSubstring(String str) {
Set<Character> set = new HashSet<>();
set.add(str.charAt(0)); // ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๋Š” ๋ฐ”๋กœ add
// ๋‘๋ฒˆ์งธ ๋ฌธ์ž๋ถ€ํ„ฐ ์ค‘๋ณต ๊ฒ€์‚ฌ ๋Œ€์ƒ
for (int i = 1; i < str.length(); i++) {
// ์ค‘๋ณต ๋ฌธ์ž๊ฐ€ ์žˆ๊ฑฐ๋‚˜, ๊ณต๋ฐฑ์ด๋ฉด ๋ฐ”๋กœ false ๋ฆฌํ„ด
if (!set.add(str.charAt(i)) || str.charAt(i) == ' ') {
return false;
}
}

return true;
}
}
41 changes: 41 additions & 0 deletions number-of-islands/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class SolutionNumberOfIslands {
char[][] sharedGrid;
int[] dx = new int[]{0, 0, -1, 1};
int[] dy = new int[]{-1, 1, 0, 0};

public int numIslands(char[][] grid) {
// ํ’€์ด
// ๋„ค ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋ฌผ๋กœ ๋‘˜๋Ÿฌ์Œ“์—ฌ์žˆ์œผ๋ฉด ์•„์ผ๋žœ๋“œ
// ์•„์ผ๋žœ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ผ
// ๋•…์ธ ๊ฒฝ์šฐ DFS ๋Œ๋ ค์„œ ์ˆœํšŒํ•˜์ž
// ์ƒํ•˜์ขŒ์šฐ ํ™•์ธํ•˜๋ฉด์„œ ๋•…์ด๋ฉด ๋ฌผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ ์ˆœํšŒํ•œ๋‹ค
// DFS 1ํšŒ ๋‹น answer += 1
// TC: O(N), N์€ ๋ฐฐ์—ด ์›์†Œ ๊ฐœ์ˆ˜
// SC: O(N)
var answer = 0;

sharedGrid = grid;
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[0].length; j++) {
if (sharedGrid[i][j] == '1') {
dfs(i, j);
answer++;
}
}
}

return answer;
}

private void dfs(int i, int j) {
sharedGrid[i][j] = '0';

for (int k=0; k<4; k++) {
var x = i+dx[k];
var y = j+dy[k];
if (x >= 0 && y >= 0 && x < sharedGrid.length && y < sharedGrid[0].length && sharedGrid[x][y] == '1') {
dfs(x, y);
}
}
}
}
33 changes: 33 additions & 0 deletions reverse-linked-list/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

//Definition for singly-linked list.
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 SolutionReverseLinkedList {
public ListNode reverseList(ListNode head) {
// ํ’€์ด: ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ ๋ฐฉํ–ฅ์„ ํ˜„์žฌ ๊ธฐ์ค€์œผ๋กœ ๋’ค์ง‘๊ณ , ๋…ธ๋“œ๋ฅผ ๋‹ค์Œ์œผ๋กœ ์˜ฎ๊ธฐ๋ฉฐ ๋ฐ˜๋ณตํ•œ๋‹ค
// next = curr.next
// prev > curr
// prev < curr
// prev = curr
// curr = next
// TC: O(N), head ๊ธธ์ด N๋งŒํผ
// SC: O(1), prev/curr 2๊ฐœ๋งŒ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ

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

return prev;
}
}

0 comments on commit ec10a0d

Please sign in to comment.