-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #482 from jaejeong1/main
[jaejeong1] WEEK 07 Solutions
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
longest-substring-without-repeating-characters/jaejeong1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |