Skip to content

Commit e792b7e

Browse files
add: week 7 solutions
1 parent ae12971 commit e792b7e

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class Solution {
5+
public int lengthOfLongestSubstring(String s) {
6+
int mx = 0;
7+
int autoIncrement = 0;
8+
Map<Character, Integer> map = new HashMap<>();
9+
Map<Integer, Character> reverseMap = new HashMap<>();
10+
11+
for (char c : s.toCharArray()) {
12+
++autoIncrement;
13+
if (map.containsKey(c)) {
14+
mx = Math.max(mx, map.size());
15+
16+
int start = map.get(c);
17+
// NOTE: 쀑볡 문자λ₯Ό λ§Œλ‚˜λŠ” 경우, μ€‘λ³΅λœ 문자 이전에 Map에 λ“€μ–΄μ˜¨ μ›μ†ŒλŠ” λͺ¨λ‘ 제거, (e.g. "sadvdf" λΌλŠ” μž…λ ₯이
18+
// 듀어왔을 λ•Œ sadκΉŒμ§€λ§Œ μ œκ±°κ°€ 되고 vλŠ” λ‚¨μ•„μžˆμ–΄μ•Ό 함.)
19+
for (int i = start; i >= 1; i--) {
20+
if (reverseMap.containsKey(i)) {
21+
char target = reverseMap.get(i);
22+
reverseMap.remove(i);
23+
map.remove(target);
24+
25+
} else {
26+
break;
27+
}
28+
}
29+
30+
map.put(c, autoIncrement);
31+
reverseMap.put(autoIncrement, c);
32+
} else {
33+
map.put(c, autoIncrement);
34+
reverseMap.put(autoIncrement, c);
35+
}
36+
}
37+
38+
return Math.max(mx, map.size());
39+
}
40+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
class Solution {
5+
6+
public int[] dx = { 1, 0, -1, 0 };
7+
public int[] dy = { 0, 1, 0, -1 };
8+
public int cnt = 0;
9+
public int w = 0;
10+
public int h = 0;
11+
public boolean[][] visit;
12+
public Queue<Node> q = new LinkedList<>();
13+
14+
public int numIslands(char[][] grid) {
15+
w = grid.length;
16+
h = grid[0].length;
17+
visit = new boolean[w][h];
18+
19+
for (int i = 0; i < w; i++) {
20+
for (int j = 0; j < h; j++) {
21+
if (grid[i][j] == '1') {
22+
cnt++;
23+
// dfs(grid, i, j);
24+
bfs(grid, i, j);
25+
}
26+
}
27+
}
28+
29+
return cnt;
30+
}
31+
32+
public void dfs(char[][] grid, int x, int y) {
33+
if (x < 0 || x >= w || y < 0 || y >= h || grid[x][y] == '0' || visit[x][y]) {
34+
return;
35+
}
36+
37+
visit[x][y] = true;
38+
grid[x][y] = '0';
39+
40+
for (int i = 0; i < 4; i++) {
41+
int nx = x + dx[i];
42+
int ny = y + dy[i];
43+
44+
dfs(grid, nx, ny);
45+
}
46+
}
47+
48+
public void bfs(char[][] grid, int x, int y) {
49+
50+
q.add(new Node(x, y));
51+
52+
while (!q.isEmpty()) {
53+
Node p = q.poll();
54+
55+
for (int i = 0; i < 4; i++) {
56+
int nx = p.x + dx[i];
57+
int ny = p.y + dy[i];
58+
59+
if (x < 0 || x >= w || y < 0 || y >= h || grid[x][y] == '0' || visit[x][y]) {
60+
continue;
61+
}
62+
63+
grid[nx][ny] = '0';
64+
q.add(new Node(nx, ny));
65+
}
66+
}
67+
}
68+
}
69+
70+
class Node {
71+
int x;
72+
int y;
73+
74+
public Node(int x, int y) {
75+
this.x = x;
76+
this.y = y;
77+
}
78+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* public class ListNode {
7+
* int val;
8+
* ListNode next;
9+
* ListNode() {}
10+
* ListNode(int val) { this.val = val; }
11+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
12+
* }
13+
*/
14+
class Solution {
15+
public ListNode reverseList(ListNode head) {
16+
int idx = 0;
17+
Map<Integer, Integer> idxMap = new HashMap<>();
18+
19+
if (head == null || head.next == null) {
20+
return null;
21+
}
22+
23+
while (true) {
24+
idxMap.put(idx++, head.val);
25+
26+
if (head.next == null) {
27+
break;
28+
}
29+
30+
head = head.next;
31+
}
32+
33+
ListNode resHead = new ListNode();
34+
ListNode cur = resHead;
35+
36+
for (int i = idx - 1; i >= 0; i--) {
37+
cur.val = idxMap.get(i);
38+
39+
if (i != 0) {
40+
cur.next = new ListNode();
41+
cur = cur.next;
42+
}
43+
}
44+
45+
return resHead;
46+
}
47+
48+
}
49+
50+
// O(n) + λ³€μˆ˜ ν•˜λ‚˜λ‘œ μ§κ΄€μ μœΌλ‘œ 문제λ₯Ό ν•΄κ²°ν•  수 μžˆλ‹€..
51+
class AnotherSolution {
52+
public ListNode reverstList(ListNode head) {
53+
ListNode prev = null;
54+
ListNode cur = head;
55+
56+
while (cur.next != null) {
57+
ListNode next = cur.next;
58+
cur.next = prev;
59+
prev = cur;
60+
cur = next;
61+
}
62+
63+
return prev;
64+
}
65+
}
66+
67+
class ListNode {
68+
int val;
69+
ListNode next;
70+
71+
ListNode() {
72+
}
73+
74+
ListNode(int val) {
75+
this.val = val;
76+
}
77+
78+
ListNode(int val, ListNode next) {
79+
this.val = val;
80+
this.next = next;
81+
}
82+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class Solution {
5+
6+
public List<Node> point = new ArrayList<>();
7+
8+
public void setZeroes(int[][] matrix) {
9+
10+
for (int i = 0; i < matrix.length; i++) {
11+
for (int j = 0; j < matrix[0].length; j++) {
12+
if (matrix[i][j] == 0) {
13+
point.add(new Node(i, j));
14+
}
15+
}
16+
}
17+
18+
for (Node n : point) {
19+
int x = n.x;
20+
int y = n.y;
21+
22+
int nx = n.x;
23+
while (nx > 0) {
24+
matrix[--nx][y] = 0;
25+
}
26+
27+
int ny = n.y;
28+
while (ny > 0) {
29+
matrix[x][--ny] = 0;
30+
}
31+
32+
int nnx = n.x;
33+
while (nnx < matrix.length - 1) {
34+
matrix[++nnx][y] = 0;
35+
}
36+
37+
int nny = n.y;
38+
while (nny < matrix[0].length - 1) {
39+
matrix[x][++nny] = 0;
40+
}
41+
}
42+
}
43+
44+
}
45+
46+
class Node {
47+
int x;
48+
int y;
49+
50+
public Node(int x, int y) {
51+
this.x = x;
52+
this.y = y;
53+
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// NOTE: 2 * X λΆ€ν„° λͺ¨λ“  경우λ₯Ό μ„Έμ–΄λ³΄λ‹ˆ, 3 * 3의 κ²©μžκ°€ λ‚˜μ™”μ„ λ•Œ 2 * 3, 3 * 2 경우λ₯Ό λ”ν•œ 값이 λ‚˜μ˜€λŠ” 것을 μ•Œ 수 μžˆμ—ˆμŒ
2+
// +) 3 * 4 κ²©μžμ— λŒ€ν•œ 닡은 3 * 3 격자의 λ‹΅ + 2 * 4 격자의 닡을 λ”ν•˜λ©΄ λ§Œλ“€μ–΄μ§€λŠ” 것을 μ•Œμˆ˜ μžˆμ—ˆκ³ , μ•„λž˜μ™€ 같은 점화식을 λ§Œλ“€ 수 μžˆμ—ˆμŒ
3+
// memo[m][n] = memo[m][n - 1] + memo[m - 1][n]
4+
class Solution {
5+
public int uniquePaths(int m, int n) {
6+
int[][] memo = new int[101][101];
7+
8+
if (m == 1 || n == 1) {
9+
return 1;
10+
}
11+
12+
memo[2][2] = 2;
13+
for (int i = 2; i < 3; i++) {
14+
for (int j = 3; j < 101; j++) {
15+
memo[i][j] = j;
16+
memo[j][i] = j;
17+
}
18+
}
19+
20+
for (int i = 3; i < 101; i++) {
21+
memo[i][i] = memo[i][i - 1] + memo[i - 1][i];
22+
for (int j = i + 1; j < 101; j++) {
23+
int val = memo[i][j - 1] + memo[i - 1][j];
24+
memo[i][j] = val;
25+
memo[j][i] = val;
26+
}
27+
}
28+
29+
return memo[m][n];
30+
}
31+
}

0 commit comments

Comments
Β (0)