Skip to content

Commit f4c8b76

Browse files
authored
Merge pull request #1878 from jinvicky/main
[jinvicky] Week7 solution
2 parents a2976d0 + 2ec4863 commit f4c8b76

File tree

6 files changed

+381
-0
lines changed

6 files changed

+381
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
public class Solution {
5+
/**
6+
* ๋ฌธ์ œ์—์„œ ๋งํ•˜๋Š” pos๋Š” ์„ค๋ช…์„ ๋•๋Š” ์‚ฌ์ดํด ๋ฐœ์ƒ ์œ„์น˜์ด์ง€ ์ฝ”๋“œ ์ƒ์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
7+
*/
8+
public boolean hasCycle(ListNode head) {
9+
Set<Integer> set = new HashSet<>();
10+
while(head != null) {
11+
// set์—์„œ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ˆซ์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋ฐ”๋กœ return true;
12+
// set.add() ๋ฉ”์„œ๋“œ๋Š” ์ถ”๊ฐ€ ์„ฑ๊ณต ์‹œ true, ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ˆซ์ž๋ฉด ์ถ”๊ฐ€ํ•˜์ง€ ๋ชปํ•˜๊ณ  false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
13+
if(!set.add(head.val)) return true;
14+
head = head.next;
15+
}
16+
return false;
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class Solution {
5+
/**
6+
* ์ดˆ๊ธฐ์— ์ค‘๋ณต์ฒดํฌ๋ฅผ ์œ„ํ•ด HashSet์œผ๋กœ ํ’€์—ˆ๋‹ค๊ฐ€ ์˜ˆ์™ธ ์ผ€์ด์Šค๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ์ด์ „ ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค ์œ„์น˜๋ฅผ ์•Œ์•„์•ผ ํ•จ์„ ๊นจ๋‹ซ๊ณ 
7+
* HashMap์œผ๋กœ ๋ฌธ์ž:์ธ๋ฑ์Šค ์Œ์„ ์ €์žฅํ–ˆ๋‹ค.
8+
* ์ž๋ฃŒ๊ตฌ์กฐ์™€ ๋ฐ˜๋ณต๋ฌธ์˜ ํ๋ฆ„์€ ๋งž์•˜์œผ๋‚˜ ์ค‘๋ณต ๋ฐœ๊ฒฌ ์‹œ left๋ฅผ Math.max()๋กœ 2๊ฐ€์ง€ ์ผ€์ด์Šค๋ฅผ ๋ชจ๋‘ ๋น„๊ตํ•˜์ง€ ์•Š์•„์„œ ๋ฌธ์ œ ๋ฐœ์ƒ
9+
* ๊ธฐ์กด left์™€ ํ•ด๋‹น ๋ฌธ์ž์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค ์ค‘ ๋” ํฐ๊ฐ’์„ ์„ ํƒํ•œ๋‹ค.
10+
*/
11+
public int lengthOfLongestSubstring(String s) {
12+
if (s == null || s.isEmpty()) return 0;
13+
14+
Map<Character, Integer> last = new HashMap<>();
15+
int left = 0, maxLen = 0;
16+
17+
for (int right = 0; right < s.length(); right++) {
18+
char ch = s.charAt(right);
19+
20+
// VIP:: left๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€๊ฑฐ๋‚˜(left+=) left๋ฅผ ์œ ์ง€ํ•œ๋‹ค.
21+
// ์™œ left๋ฅผ ๊ฐ€๋Šฅํ•œ ํ•œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์—…๋ฐ์ดํŠธํ• ๊นŒ? -> ๊ทธ๋ž˜์•ผ ์ค‘๋ณต ์ œ๊ฑฐ๋œ ์˜จ์ „ํ•œ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
22+
// abba์˜ ๊ฒฝ์šฐ ๋‘๋ฒˆ์งธ b, ๋‘๋ฒˆ์งธ a๊ฐ€ ์ด์— ํ•ด๋‹นํ•œ๋‹ค.
23+
// last(b) = max(0, 1+1=2) -> 2
24+
// last(a) = max(2, 0+1=1) -> 2
25+
if (last.containsKey(ch)) {
26+
left = Math.max(left, last.get(ch) + 1);
27+
}
28+
maxLen = Math.max(maxLen, right - left + 1);
29+
last.put(ch, right); // ๋ฐฉ๊ธˆ ๋ฌธ์ž์˜ '๋งˆ์ง€๋ง‰ ์œ„์น˜' ๊ฐฑ์‹ 
30+
}
31+
return maxLen;
32+
}
33+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
public class jinvicky {
2+
}
3+
4+
/**
5+
* ์ผ๊ด€์„ฑ์„ ์œ„ํ•ด ๋ชจ๋“  ๋ณ€์ˆ˜๋“ค์„ ๋ฉ”์„œ๋“œ ๋‚ด lv๋กœ ์„ ์–ธํ•ด์„œ ํ‘ธ๋Š” ๊ฒƒ์œผ๋กœ ํ•œ๋‹ค.
6+
* dfs๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ ํŠนํžˆ ๋ฐ˜ํ™˜ ํƒ€์ž…์— ์•ฝํ•œ๋ฐ void์ธ๊ฐ€ int,booelan ๋“ฑ ๊ธฐํƒ€ ํƒ€์ž…์ธ๊ฐ€๋ฅผ ํ™•์‹คํžˆ ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ„๋„ ์˜ˆ์ œ๋กœ ํ’€์—ˆ๋‹ค.
7+
* ๋˜ํ•œ ์˜๋„์ ์œผ๋กœ ๊ธฐ์กด grid๋ฅผ 1 -> 0์œผ๋กœ ๋ฐ”๊พธ์–ด ์„ฌ์„ ์—†์• ์ง€ ์•Š๊ณ  ๊ณต๊ฐ„๋ณต์žก๋„๊ฐ€ ์ฆ๊ฐ€ํ•˜๋”๋ผ๋„ ํ•™์Šต์„ ์œ„ํ•ด ๋ฐฉ๋ฌธ ๋ฐฐ์—ด์„ ๋ณ„๋„๋กœ ์„ ์–ธํ•ด ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ–ˆ๋‹ค.
8+
*/
9+
class Solution {
10+
public int numIslands1(char[][] grid) {
11+
if (grid == null || grid.length == 0) return 0;
12+
int rows = grid.length, cols = grid[0].length;
13+
boolean[][] visited = new boolean[rows][cols];
14+
15+
int count = 0;
16+
for (int r = 0; r < rows; r++) {
17+
for (int c = 0; c < cols; c++) {
18+
if (grid[r][c] == '1' && !visited[r][c]) {
19+
int size = dfsByInt(grid, visited, r, c, rows, cols);
20+
if (size > 0) count++; // ํฌ๊ธฐ๊ฐ€ ์–‘์ˆ˜๋ฉด ์„ฌ 1๊ฐœ
21+
}
22+
}
23+
}
24+
return count;
25+
}
26+
27+
// dfs๋Š” ํ•ด๋‹น ์„ฌ์˜ ๋„“์ด๋ฅผ ๋ฐ˜ํ™˜
28+
private int dfsByInt(char[][] grid, boolean[][] visited, int r, int c, int rows, int cols) {
29+
if (r < 0 || c < 0 || r >= rows || c >= cols) return 0; // ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ๋‹ค.
30+
if (grid[r][c] == '0' || visited[r][c]) return 0; // ๋ฌธ์ œ์˜ ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๊ฑฐ๋‚˜, ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋‹ค.
31+
32+
visited[r][c] = true; // ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
33+
int area = 1;
34+
35+
area += dfsByInt(grid, visited, r + 1, c, rows, cols);
36+
area += dfsByInt(grid, visited, r - 1, c, rows, cols);
37+
area += dfsByInt(grid, visited, r, c + 1, rows, cols);
38+
area += dfsByInt(grid, visited, r, c - 1, rows, cols);
39+
40+
return area;
41+
}
42+
43+
public int numIslands(char[][] grid) {
44+
if (grid == null || grid.length == 0) return 0;
45+
int rows = grid.length, cols = grid[0].length;
46+
47+
boolean[][] visited = new boolean[rows][cols];
48+
int count = 0;
49+
50+
for (int r = 0; r < rows; r++) {
51+
for (int c = 0; c < cols; c++) {
52+
if (grid[r][c] == '1' && !visited[r][c]) {
53+
dfs(grid, visited, r, c, rows, cols);
54+
count++; // ์„ฌ ํ•˜๋‚˜ ํƒ์ƒ‰ ๋๋‚˜๋ฉด +1
55+
}
56+
}
57+
}
58+
return count;
59+
}
60+
61+
private void dfs(char[][] grid, boolean[][] visited, int r, int c, int rows, int cols) {
62+
if (r < 0 || c < 0 || r >= rows || c >= cols) return; // ๊ทธ๋ฆฌ๋“œ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์ข…๋ฃŒ
63+
if (grid[r][c] == '0' || visited[r][c]) return; // ๋ฌผ์ด๊ฑฐ๋‚˜ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์„ฌ์ด๋ผ๋ฉด ์ข…๋ฃŒ
64+
65+
visited[r][c] = true; // ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
66+
67+
// ์ƒํ•˜์ขŒ์šฐ DFS
68+
dfs(grid, visited, r + 1, c, rows, cols);
69+
dfs(grid, visited, r - 1, c, rows, cols);
70+
dfs(grid, visited, r, c + 1, rows, cols);
71+
dfs(grid, visited, r, c - 1, rows, cols);
72+
}
73+
74+
/**
75+
* void ๋ฐ˜ํ™˜์˜ dfs๋ฉด return;
76+
* int ๋ฐ˜ํ™˜์˜ dfs๋ฉด return 0;
77+
* bool ๋ฐ˜ํ™˜์˜ dfs๋ฉด return false;
78+
*
79+
* 1. ๋ฆฌํ„ด๊ฐ’ ์—†์ด ๋„˜๊ฒจ๋ฐ›์€ ์ƒํƒœ๋งŒ ๊ฐฑ์‹ 
80+
* void dfs(Node node) {
81+
* if (๋ฒ”์œ„ ๋ฐ– || ์กฐ๊ฑด ๋ถˆ๋งŒ์กฑ || ์ด๋ฏธ ๋ฐฉ๋ฌธ) return;
82+
*
83+
* ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ(node);
84+
*
85+
* for (์ด์›ƒ nei : node) {
86+
* dfs(nei);
87+
* }
88+
* }
89+
*
90+
* 2. ์ตœ๋Œ€/์ตœ์†Œ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ฐ˜ํ™˜
91+
* int dfs(Node node) {
92+
* if (๋ฒ”์œ„ ๋ฐ– || ์กฐ๊ฑด ๋ถˆ๋งŒ์กฑ) return 0;
93+
*
94+
* ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ(node);
95+
*
96+
* int result = 1; // ์ž๊ธฐ ์ž์‹  ํฌํ•จ
97+
* for (์ด์›ƒ nei : node) {
98+
* result += dfs(nei);
99+
* }
100+
* return result;
101+
* }
102+
* 3. ์กฐ๊ฑด ๋งŒ์กฑ ์—ฌ๋ถ€
103+
* boolean dfs(Node node) {
104+
* if (๋ชฉํ‘œ ๋„๋‹ฌ) return true;
105+
* if (๋ฒ”์œ„ ๋ฐ– || ์กฐ๊ฑด ๋ถˆ๋งŒ์กฑ) return false;
106+
*
107+
* ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ(node);
108+
*
109+
* for (์ด์›ƒ nei : node) {
110+
* if (dfs(nei)) return true;
111+
* }
112+
* return false;
113+
* }
114+
*
115+
* 4. ๋ฉ”๋ชจ์ด์ œ์ด์…˜/DP ๊ฒฐํ•ฉ
116+
* int dfs(Node node, Map<Node,Integer> memo) {
117+
* if (memo.containsKey(node)) return memo.get(node);
118+
* if (๊ธฐ์ € ์กฐ๊ฑด) return 1;
119+
*
120+
* int best = 0;
121+
* for (์ด์›ƒ nei : node) {
122+
* best = Math.max(best, 1 + dfs(nei, memo));
123+
* }
124+
* memo.put(node, best);
125+
* return best;
126+
* }
127+
*/
128+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.Stack;
2+
3+
class Solution {
4+
/**
5+
* ๋ฆฌ๋ฒ„์Šค์™€ ๋™์ผํ•œ ํ›„์ž…์„ ์ถœ ๊ตฌ์กฐ๋ฅผ ์ƒ๊ฐํ–ˆ๊ณ  ๊ทธ๋ž˜์„œ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์Šคํƒ์„ ๊ฒฐ์ •ํ–ˆ๋‹ค.
6+
* ์‚ฌ์ดํด ๋ฌธ์ œ๋ฅผ ์—ผ๋‘ํ•ด ๋‘๊ณ  ์กฐ๊ฑด์„ ์„ค๊ณ„ํ–ˆ์ง€๋งŒ ์‚ฌ์ดํด์„ ๋Š๋Š” ์œ„์น˜๊ฐ€ ํ‹€๋ ค์„œ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค.
7+
*/
8+
public ListNode reverseList(ListNode head) {
9+
if (head == null) return null;
10+
11+
Stack<ListNode> stack = new Stack<>();
12+
while (head != null) {
13+
stack.push(head);
14+
head = head.next;
15+
}
16+
17+
ListNode newHead = stack.pop(); // ์ •๋‹ต ๋ฐ˜ํ™˜์šฉ
18+
ListNode current = newHead; // ํฌ์ธํ„ฐ๋ฅผ ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ๊ณ„์‚ฐํ•˜๊ธฐ์šฉ (์†Œ์œ„ ๋”๋ฏธ ํฌ์ธํ„ฐ)
19+
20+
while (!stack.isEmpty()) {
21+
current.next = stack.pop();
22+
current = current.next; // โ† ์ˆ˜์ •: ์ž๊ธฐ ์ž์‹  ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์‹  ์•ž์œผ๋กœ ์ด๋™
23+
}
24+
current.next = null; // ๋งˆ์ง€๋ง‰ tail ์ •๋ฆฌ
25+
return newHead;
26+
}
27+
28+
/**
29+
* ํฌ์ธํ„ฐ ๋ฐ˜๋ณต๋ฌธ์„ ์ƒˆ๋กœ ๋ฐฐ์› ์„ ๋•Œ fast/slow์™€ ๊ฐ™์€ ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ํ—ท๊ฐˆ๋ ธ์ง€๋งŒ ๋‘˜์€ ์ „ํ˜€ ๋‹ค๋ฅด๋‹ค.
30+
* ํฌ์ธํ„ฐ ๋ฐ˜๋ณต๋ฌธ
31+
* * ๋งํฌ ๋ฐฉํ–ฅ ๋’ค์ง‘๊ธฐ
32+
* * 3๊ฐœ ํฌ์ธํ„ฐ (prev, cur, next)
33+
* * ๋ฆฌ์ŠคํŠธ ์ž์ฒด ๊ตฌ์กฐ ๋ณ€๊ฒฝ
34+
* <p>
35+
* ํˆฌ ํฌ์ธํ„ฐ
36+
* * ์ค‘๊ฐ„, ์‚ฌ์ดํด, ๊ต์ฐจ์  ๋“ฑ ํƒ์ƒ‰
37+
* * 2๊ฐœ ํฌ์ธํ„ฐ (slow, fast)
38+
* * ๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ ๊ทธ๋Œ€๋กœ, ์œ„์น˜ ์ •๋ณด๋งŒ ์–ป์Œ
39+
* <p>
40+
* https://bcp0109.tistory.com/142
41+
*/
42+
public ListNode reverseListByPointer(ListNode head) {
43+
ListNode prev = null;
44+
ListNode cur = head;
45+
while (cur != null) {
46+
ListNode next = cur.next; // next๋ผ๋Š” temp ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ prev์™€ cur.next ๊ฐ’์„ ๋ฐ”๊พผ๋‹ค.
47+
cur.next = prev;
48+
prev = cur;
49+
cur = next;
50+
// ๋Œ€๊ฐ์„ ์œผ๋กœ / / / / ์œผ๋กœ ๋ณ€์ˆ˜๋ช… ์•”๊ธฐํ•˜๊ธฐ
51+
}
52+
return prev;
53+
}
54+
55+
public ListNode reverseListByRecursion(ListNode head) {
56+
// head.next๊ฐ€ null์ธ์ง€๋„ ํ™•์ธํ•˜๋Š” ๋กœ์ง์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. (nullPointerException ๋ฐฉ์ง€)
57+
if (head == null || head.next == null) return null; // ์žฌ๊ท€์˜ ๋, ์ด์ œ ๊ธฐ์กด ์—ฐ์‚ฐ์„ ์ทจํ•ฉํ•œ๋‹ค.
58+
ListNode newHead = reverseListByRecursion(head.next);
59+
head.next.next = head;
60+
head.next = null;
61+
return newHead;
62+
}
63+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* ๋Œ€ํ‘œ์ ์ธ ์กฐ๊ฑด ๊ธฐ๋ฐ˜ ๋งˆํ‚น ๋ฌธ์ œ: ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด ๋ฐ”๋กœ ๋ฐ”๊พธ์ง€ ์•Š๊ณ , ๋จผ์ € ํ‘œ์‹œ๋งŒ ํ•ด๋‘๊ณ  ๋งˆ์ง€๋ง‰์— ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋ฒ•
3+
* ๋ณด์กฐ ๋ฐฐ์—ด์„ ์“ฐ๋Š” ๋ฐฉ์‹์ด๋ฉด 2๋ฒˆ ์ „์ฒด ํƒ์ƒ‰์ด ๊ธฐ๋ณธ์ด๋‹ค.
4+
*/
5+
6+
/**
7+
* ๋งคํŠธ๋ฆญ์Šค ๋ฌธ์ œ ์œ ํ˜•์„ ์•Œ๊ณ  easy์™€ ์—ฐ๊ด€ ์œ ํ˜•์„ ์„ ํ–‰ํ•œ ๋‹ค์Œ์— ์ •๋‹ต์„ ๋งž์ถœ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
8+
* 1. ์กฐ๊ฑด ๊ธฐ๋ฐ˜ ๊ฐฑ์‹ /๋งˆํ‚น
9+
* 2. ์ด์›ƒ ์ง‘๊ณ„
10+
* 3. ํ–‰๋ ฌ ๋ณ€ํ˜•
11+
* 4. ๋‹จ์ˆœ ์ˆœํšŒ
12+
*/
13+
class Solution {
14+
public void setZeroes(int[][] matrix) {
15+
int[][] assist = new int[matrix.length][matrix[0].length];
16+
for (int i = 0; i < matrix.length; i++) {
17+
for (int j = 0; j < matrix[0].length; j++) {
18+
if (matrix[i][j] == 0) {
19+
for (int ni = 0; ni < matrix.length; ni++) {
20+
assist[ni][j] = -1;
21+
}
22+
23+
for (int nj = 0; nj < matrix[0].length; nj++) { // ๋ฒ”์œ„ ํ™•์ธ ์ž˜ํ•˜๊ธฐ
24+
assist[i][nj] = -1;
25+
}
26+
}
27+
}
28+
}
29+
30+
for (int i = 0; i < matrix.length; i++) {
31+
for (int j = 0; j < matrix[0].length; j++) {
32+
// assist[i][j]๊ฐ€ -1์ด๋ฉด 0์œผ๋กœ ๋ฐ”๊พผ๋‹ค.
33+
if (assist[i][j] == -1) {
34+
matrix[i][j] = 0;
35+
}
36+
}
37+
}
38+
}
39+
40+
/**
41+
* ์„ ํ–‰ ๋ฌธ์ œ๋“ค์„ ๋ชจ๋‘ ํ’€๋ฉด ๋ฐ˜๋ณตํ•  ๋ถ€๋ถ„๊ณผ ์‘์šฉํ•  ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , ๊ทธ ๋ถ€๋ถ„์„ ๊ณ„์† ๋ฐ˜๋ณตํ•™์Šตํ•œ ๋‹ค์Œ์— ๋–ผ์–ด๋‚ด์„œ ๋ณ€ํ˜•์„ ์‹œ๋„ํ–ˆ๋‹ค.
42+
*/
43+
// 2์ฐจ์› ๊ทธ๋ฆฌ๋“œ์˜ ๊ธฐ์ดˆ ๋ฌธ์ œ - ํ–‰ ๋‹จ์œ„ ํ•ฉ์‚ฐ ํ›„ ์ตœ๋Œ€๊ฐ’ ์ฐพ๊ธฐ
44+
// https://leetcode.com/problems/richest-customer-wealth/
45+
public int maximumWealth(int[][] accounts) {
46+
int maxW = 0;
47+
for (int i = 0; i < accounts.length; i++) {
48+
int current = 0;
49+
for (int j = 0; j < accounts[0].length; j++) {
50+
current += accounts[i][j];
51+
}
52+
maxW = Math.max(maxW, current);
53+
}
54+
return maxW;
55+
}
56+
57+
// ์—ฌ๊ธฐ์„œ invert๋ž€ row๋งˆ๋‹ค ์—ญ์ˆœ ์ •๋ ฌ์„ 1๋ฒˆ ํ•˜๊ณ , ๋‹ค์‹œ for๋ฌธ์œผ๋กœ 0์€ 1๋กœ, 1์€ 0์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
58+
// https://leetcode.com/problems/flipping-an-image/
59+
public int[][] flipAndInvertImage(int[][] image) {
60+
for (int i = 0; i < image.length; i++) {
61+
int left = 0;
62+
int right = image[0].length - 1;
63+
64+
while (left < right) {
65+
int temp = image[i][left];
66+
image[i][left] = image[i][right];
67+
image[i][right] = temp;
68+
69+
right--;
70+
left++;
71+
}
72+
}
73+
74+
for (int i = 0; i < image.length; i++) {
75+
for (int j = 0; j < image[0].length; j++) {
76+
image[i][j] = image[i][j] == 0 ? 1 : 0;
77+
}
78+
}
79+
return image;
80+
}
81+
82+
// grid[i][j]๋ฅผ grid[j][i]๋กœ ๋ฐ”๊พธ๋Š” ๋ฌธ์ œ. grid[2][3] -> grid[3][2] ์ด์ค‘ for๋ฌธ์„ ์–ด๋–ป๊ฒŒ ๋ฐ˜์˜ํ• ๊นŒ? ์ฒ˜์Œ์—๋Š” ์ƒ๊ฐ์ด ์•ˆ ๋‚˜์„œ while + for๋กœ ํ–ˆ๋‹ค๊ฐ€ ์ด์ค‘ for๋ฌธ์œผ๋กœ ์ž‘์€ ๋ฆฌํŒฉํ† ๋ง
83+
// https://leetcode.com/problems/transpose-matrix/description/
84+
public int[][] transpose(int[][] matrix) {
85+
int[][] answer = new int[matrix[0].length][matrix.length];
86+
87+
for (int j = 0; j < matrix[0].length; j++) {
88+
for (int i = 0; i < matrix.length; i++) {
89+
answer[j][i] = matrix[i][j];
90+
}
91+
}
92+
return answer;
93+
}
94+
95+
// 8๋ฐฉํ–ฅ์„ ๋„์ „, dir ๋ฐฐ์—ด์ด ํ‹€๋ฆฌ์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ๊ฒŒ ์–ด๋ ค์› ๋‹ค. -> ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ๋Š” ์ง€ ํ™•์ธํ•˜๋Š” ๋กœ์ง์ด ๋งˆ์น˜ dfs์˜ ๊ทธ๊ฒƒ์ด์—ˆ๋‹ค. (0๋ณด๋‹ค ์ž‘์€์ง€, length๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€์ง€ ๊ตญ๋ฃฐ ํ™•์ธ)
96+
// ์—ฌ๊ธฐ์„œ ์•Œ์•„์•ผ ํ•  ์ ์€ ์ž…๋ ฅ๋ฐ›์€ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•˜๋Š” ๊ฒฝ์šฐ์™€ assist ๋ฐฐ์—ด์ด ๊ผญ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋ฅผ ๊ตฌ๋ถ„ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
97+
// ์ด ๋ฌธ์ œ๋Š” [i][j]๋งˆ๋‹ค ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜์ง€๋งŒ ์›๋ณธ ๋ฐฐ์—ด์„ ๋ฐ”๊พธ์ง€ ์•Š๊ณ  assist์— ๊ธฐ๋กํ•œ ๋‹ค์Œ์— ๊ทธ๊ฑธ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. (๊ณ„์‚ฐ์€ ๋งค๋ฒˆ ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค)
98+
// https://leetcode.com/problems/image-smoother/
99+
public int[][] imageSmoother(int[][] img) {
100+
int[][] dir8 = new int[][]{
101+
{-1, 0},
102+
{-1, 1},
103+
{0, 1},
104+
{1, 1},
105+
{1, 0},
106+
{1, -1},
107+
{0, -1},
108+
{-1, -1}
109+
};
110+
111+
int[][] assist = new int[img.length][img[0].length];
112+
113+
for (int i = 0; i < img.length; i++) {
114+
for (int j = 0; j < img[0].length; j++) {
115+
// dir์˜ 8์„ ๋Œ๋ฆฌ๋Š”๋ฐ ์ค‘์š”ํ•œ ๊ฑด sum, cnt๊ฐ€ ์นธ๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ํ‰๊ท ์„ /8์ด ์•„๋‹ˆ๋ผ /cnt๋กœ ํ•˜๊ณ  sum๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์˜€๋‹ค.
116+
int sum = img[i][j];
117+
int cnt = 1;
118+
for (int[] d : dir8) { // 3์ค‘ for๋ฌธ ์•ˆ์—์„œ ni, nj ๊ฐœ๋…์„ setMatrixZeros์—์„œ ์ผ๋‹ค.
119+
int ni = i + d[0];
120+
int nj = j + d[1];
121+
122+
if (ni < 0 || ni >= img.length || nj < 0 || nj >= img[0].length) { // ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ๋„˜์–ด๊ฐ€๋Š” ๋กœ์ง์„ ์ฐธ๊ณ ํ–ˆ๋‹ค.
123+
continue;
124+
}
125+
126+
sum += img[ni][nj];
127+
cnt++;
128+
}
129+
assist[i][j] = sum / cnt;
130+
}
131+
}
132+
return assist;
133+
}
134+
135+
//
136+
// https://leetcode.com/problems/game-of-life/
137+
}

โ€Žunique-paths/jinvicky.javaโ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public int uniquePaths(int m, int n) {
44
* ์—ฌ๊ธฐ์„œ ์ด๋™๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋Š” right, down ๋‘๊ฐ€์ง€ ๊ฒฝ์šฐ์ด๋‹ค.
55
* ๋ชจ๋“  ๋ธ”๋ก์€ ๋‚ด ์™ผ์ชฝ ๋ธ”๋ก์—์„œ ๋‚˜๋กœ ์˜จ ๊ฒฝ์šฐ, ๋‚ด ์œ„ ๋ธ”๋ก์—์„œ ๋‚˜๋กœ ์˜จ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•ด์„œ [i-1][j] + [i][j-1]๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
66
* ๋‹จ ๊ฐ€๋กœ ์ฒซ๋ฒˆ์งธ ์ค„๊ณผ ์„ธ๋กœ ์ฒซ๋ฒˆ์งธ ์ค„์€ 1๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค˜์•ผ ํ•œ๋‹ค. (์™œ๋ƒํ•˜๋ฉด ๊ฐ๊ฐ down, right์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๋ธ”๋ก๋“ค์€ 1๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ๋ฐ–์— ๋„๋‹ฌํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.)
7+
*
8+
*
79
*/
810
int[][] dp = new int[m][n];
911
for (int i = 0; i < m; i++) {

0 commit comments

Comments
ย (0)