Skip to content

Commit 3083f36

Browse files
authored
Merge pull request #1926 from jinvicky/main
[jinvicky] Week 11 Solutions
2 parents 8d25cb7 + 83cb69d commit 3083f36

File tree

10 files changed

+368
-0
lines changed

10 files changed

+368
-0
lines changed

โ€Žclone-graph/jinvicky.javaโ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class Solution {
5+
public Node cloneGraph(Node node) {
6+
if (node == null) return null;
7+
8+
Map<Node, Node> map = new HashMap<>();
9+
return clone(node, map);
10+
}
11+
12+
private Node clone(Node node, Map<Node, Node> map) {
13+
if (map.containsKey(node)) {
14+
return map.get(node);
15+
}
16+
17+
Node clonedNode = new Node(node.val);
18+
map.put(node, clonedNode);
19+
20+
for (Node neighbor : node.neighbors) {
21+
clonedNode.neighbors.add(clone(neighbor, map));
22+
}
23+
24+
return clonedNode;
25+
}
26+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
class Solution {
20+
public TreeNode invertTree(TreeNode root) {
21+
if (root == null) return null;
22+
23+
TreeNode temp = root.left;
24+
root.left = root.right;
25+
root.right = temp;
26+
27+
System.out.println(root.val);
28+
29+
invertTree(root.left);
30+
invertTree(root.right);
31+
32+
return root;
33+
}
34+
35+
public TreeNode invertTreeByQueue(TreeNode root) {
36+
if (root == null) {
37+
return null;
38+
}
39+
40+
// BFS๋ฅผ ์œ„ํ•œ ํ ์ƒ์„ฑ
41+
Queue<TreeNode> queue = new LinkedList<>();
42+
queue.add(root);
43+
44+
// ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
45+
while (!queue.isEmpty()) {
46+
TreeNode current = queue.poll();
47+
48+
// ํ˜„์žฌ ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์ž์‹๊ณผ ์˜ค๋ฅธ์ชฝ ์ž์‹ ๊ตํ™˜
49+
TreeNode temp = current.left;
50+
current.left = current.right;
51+
current.right = temp;
52+
53+
// ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ํ์— ์ถ”๊ฐ€
54+
if (current.left != null) {
55+
queue.add(current.left);
56+
}
57+
if (current.right != null) {
58+
queue.add(current.right);
59+
}
60+
}
61+
62+
return root;
63+
}
64+
}
65+
/**
66+
* public TreeNode invertTreeByQueue(TreeNode root) {
67+
* // root๊ฐ€ ๋„์ด๋ฉด ๋ฆฌํ„ด
68+
* // ํ ์„ ์–ธํ•˜๊ณ  ๋ฃจํŠธ๋ฅผ ์‚ฝ์ž…
69+
*
70+
* // ํ๊ฐ€ ๋น„๊ธฐ ์ „๊นŒ์ง€ ์—ฐ์‚ฐ ๋ฐ˜๋ณต
71+
* // temp ๋ณ€์ˆ˜๋กœ left์™€ right์„ ๊ต์ฒด
72+
* // ์ž์‹ left ๋…ธ๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€ (์žˆ๋‹ค๋ฉด)
73+
* // ์ž์‹ right ๋…ธ๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€ (์žˆ๋‹ค๋ฉด)
74+
* // ๋ฃจํŠธ๋ฅผ ๋ฐ˜ํ™˜
75+
* }
76+
*/

โ€Žlongest-common-subsequence/jinvicky.javaโ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
class Solution {
2+
// dp ํ‘œ๋ฅผ ๋ด์•ผ ์ดํ•ด๊ฐ€ ๋” ์‰ฝ๋‹ค. https://twinw.tistory.com/126
3+
// dp, ์ฆ‰ ๋ฉ”๋ชจ์ด์ œ์ด์…˜์˜ ๋ฒ”์œ„๋Š” ์ด์ „ row์ธ ๊ฒƒ์ด๋‹ค. (๊ธฐ์กด cell ์œ„์ฃผ์˜ ๋ฌธ์ œ๋ณด๋‹ค ๋„“์€ ๋ฒ”์œ„)
24
public int longestCommonSubsequence(String text1, String text2) {
5+
// ์™ผ์ชฝ๊ณผ ์œ„๋กœ 1์ค„์”ฉ ๋ฒ„ํผ(์—ฌ์œ ) ๋ฐฐ์—ด์ด ์žˆ๋‹ค. ์ฆ‰, dp[0][..], dp[..][0]์€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ ์ƒํƒœ
36
int m = text1.length();
47
int n = text2.length();
58

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class Solution {
5+
public int characterReplacement(String s, int k) {
6+
Map<Character, Integer> charCount = new HashMap<>();
7+
8+
int left = 0;
9+
int maxFrequency = 0;
10+
int maxLength = 0;
11+
12+
for(int right = 0; right < s.length(); right++) {
13+
char rightChar = s.charAt(right);
14+
15+
charCount.put(rightChar, charCount.getOrDefault(rightChar, 0) + 1);
16+
17+
maxFrequency = Math.max(maxFrequency, charCount.get(rightChar));
18+
19+
if((right - left + 1) - maxFrequency > k) {
20+
char leftChar = s.charAt(left);
21+
22+
charCount.put(leftChar, charCount.get(leftChar) - 1);
23+
left++;
24+
}
25+
26+
maxLength = Math.max(maxLength, right - left + 1);
27+
}
28+
return maxLength;
29+
}
30+
31+
// while๋ฌธ๊ณผ ๋ฐฐ์—ด๋กœ ์„ฑ๋Šฅ์ด ๊ฐœ์„ ๋œ ํ’€์ด
32+
public int characterReplacement2(String s, int k) {
33+
int left = 0, right = 0;
34+
int maxCount = 0, result = 0;
35+
int[] freq = new int[26];
36+
37+
while (right < s.length()) {
38+
freq[s.charAt(right) - 'A']++;
39+
maxCount = Math.max(maxCount, freq[s.charAt(right) - 'A']);
40+
41+
while ((right - left + 1) - maxCount > k) {
42+
freq[s.charAt(left) - 'A']--;
43+
left++;
44+
}
45+
46+
result = Math.max(result, right - left + 1);
47+
right++;
48+
}
49+
return result;
50+
}
51+
}

โ€Žmaximum-product-subarray/jinvicky.javaโ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Solution {
2+
// ์ตœ์†Œ๊ฐ’์€ ์Œ์ˆ˜๊ฐ€ ๊ณฑํ•ด์งˆ ๋•Œ ์ตœ๋Œ€๊ฐ’์ด ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์†Œ, ์ตœ๋Œ€๋ฅผ ๊ฐ๊ฐ dp[]๋กœ ๊ด€๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.
23
public int maxProduct(int[] nums) {
34
if (nums.length == 1)
45
return nums[0];
@@ -15,6 +16,11 @@ public int maxProduct(int[] nums) {
1516

1617
for (int i = 1; i < len; i++) {
1718
// ํ›„๋ณด 3์„ ์ค€๋น„
19+
/**
20+
* ํ˜„์žฌ ์ธ๋ฑ์Šค๊ฐ’ (justNum)
21+
* ์ด์ „ ์ธ๋ฑ์Šค ์ตœ์†Œ๊ฐ’ x ํ˜„์žฌ ์ธ๋ฑ์Šค ๊ฐ’ (reverse)
22+
* ์ด์ „ ์ธ๋ฑ์Šค ์ตœ๋Œ€๊ฐ’ x ํ˜„์žฌ ์ธ๋ฑ์Šค ๊ฐ’ (keep)
23+
*/
1824
int justNum = nums[i];
1925
// ๊ณ„์† ๋”ํ•œ ๊ฐ’
2026
int keep = justNum * max[i-1];
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
class Solution {
6+
public int[][] merge(int[][] intervals) {
7+
// 1. [0]๋ฒˆ์งธ ์œ„์ฃผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
8+
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
9+
10+
// [0], [1] ์„ start, end๋กœ ์„ ์–ธํ•œ๋‹ค.
11+
int start = intervals[0][0];
12+
int end = intervals[0][1];
13+
14+
Map<Integer, Integer> map = new HashMap<>();
15+
16+
for (int i = 0; i < intervals.length; i++) {
17+
int curS = intervals[i][0];
18+
int curE = intervals[i][1];
19+
20+
if(curS <= end) {
21+
end = Math.max(curE, end);
22+
} else {
23+
start = curS;
24+
end = curE;
25+
}
26+
27+
// {1,3} , {1,6}์ด ์ฐจ๋ก€๋กœ ๋“ค์–ด์™”์„ ๋•Œ ๋” ๋‚˜์ค‘์— ๋“ค์–ด์˜จ ๊ฐ’์œผ๋กœ overlapp ๋  ๊ฒƒ์ด๋ผ๋Š” ์ ์„ ์ด์šฉํ–ˆ๋‹ค.
28+
map.put(start, end);
29+
}
30+
31+
int[][] answer = new int[map.size()][2];
32+
33+
int idx = 0;
34+
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
35+
int[] item = new int[]{entry.getKey(), entry.getValue()};
36+
answer[idx] = item;
37+
idx++;
38+
}
39+
40+
return answer;
41+
}
42+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class Solution {
5+
private int[][] heights;
6+
private int rows, cols;
7+
private int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // ์ƒํ•˜์ขŒ์šฐ
8+
9+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
10+
this.heights = heights;
11+
this.rows = heights.length;
12+
this.cols = heights[0].length;
13+
14+
// ํƒœํ‰์–‘๊ณผ ๋Œ€์„œ์–‘์—์„œ ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์ง€์ ์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด
15+
boolean[][] pacific = new boolean[rows][cols];
16+
boolean[][] atlantic = new boolean[rows][cols];
17+
18+
// ํƒœํ‰์–‘๊ณผ ๋Œ€์„œ์–‘ ๊ฒฝ๊ณ„์—์„œ ํƒ์ƒ‰ ์‹œ์ž‘
19+
// ๋งจ ์œ—์ค„๊ณผ ๋งจ ์•„๋žซ์ค„
20+
for (int j = 0; j < cols; j++) {
21+
dfs(0, j, pacific, Integer.MIN_VALUE); // ํƒœํ‰์–‘ (์œ—์ค„)
22+
dfs(rows - 1, j, atlantic, Integer.MIN_VALUE); // ๋Œ€์„œ์–‘ (์•„๋žซ์ค„)
23+
}
24+
25+
// ๋งจ ์™ผ์ชฝ์ค„๊ณผ ๋งจ ์˜ค๋ฅธ์ชฝ์ค„
26+
for (int i = 0; i < rows; i++) {
27+
dfs(i, 0, pacific, Integer.MIN_VALUE); // ํƒœํ‰์–‘ (์™ผ์ชฝ์ค„)
28+
dfs(i, cols - 1, atlantic, Integer.MIN_VALUE); // ๋Œ€์„œ์–‘ (์˜ค๋ฅธ์ชฝ์ค„)
29+
}
30+
31+
// ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
32+
List<List<Integer>> result = new ArrayList<>();
33+
34+
// ์–‘์ชฝ ๋ฐ”๋‹ค๋กœ ๋ชจ๋‘ ํ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ง€์  ์ฐพ๊ธฐ
35+
for (int i = 0; i < rows; i++) {
36+
for (int j = 0; j < cols; j++) {
37+
if (pacific[i][j] && atlantic[i][j]) {
38+
List<Integer> coord = new ArrayList<>();
39+
coord.add(i);
40+
coord.add(j);
41+
result.add(coord);
42+
}
43+
}
44+
}
45+
46+
return result;
47+
}
48+
49+
// ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (DFS) ํ•จ์ˆ˜
50+
private void dfs(int r, int c, boolean[][] visited, int prevHeight) {
51+
// ์œ ํšจํ•˜์ง€ ์•Š์€ ์ง€์  (๋ฒ”์œ„ ๋ฐ–, ์ด๋ฏธ ๋ฐฉ๋ฌธ, ๋†’์ด ์กฐ๊ฑด ๋ถˆ๋งŒ์กฑ)
52+
if (r < 0 || r >= rows || c < 0 || c >= cols || visited[r][c] || heights[r][c] < prevHeight) {
53+
return;
54+
}
55+
56+
// ํ˜„์žฌ ์ง€์  ๋ฐฉ๋ฌธ ํ‘œ์‹œ
57+
visited[r][c] = true;
58+
59+
// ์ƒํ•˜์ขŒ์šฐ๋กœ ํƒ์ƒ‰ ์ง„ํ–‰
60+
for (int[] dir : directions) {
61+
int newR = r + dir[0];
62+
int newC = c + dir[1];
63+
dfs(newR, newC, visited, heights[r][c]);
64+
}
65+
}
66+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
3+
class Solution {
4+
5+
public int countSubstrings(String s) {
6+
int n = s.length();
7+
int count = 0;
8+
9+
// ํ™€์ˆ˜์™€ ์ง์ˆ˜ ํ†ตํ•ฉ ์ฒ˜๋ฆฌ (0๋ถ€ํ„ฐ 2n-1๊นŒ์ง€)
10+
for (int center = 0; center < 2 * n - 1; center++) {
11+
int left = center / 2;
12+
int right = left + center % 2;
13+
14+
while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
15+
count++;
16+
left--;
17+
right++;
18+
}
19+
}
20+
21+
return count;
22+
}
23+
24+
// ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ํ’€์ด
25+
26+
/**
27+
* 1. ํšŒ๋ฌธ์˜ ์ค‘์‹ฌ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ถ„๋ฆฌํ•œ๋‹ค.
28+
* 2. c๋Š” ํšŒ๋ฌธ์˜ ์ค‘์‹ฌํ›„๋ณด๊ฐ€ ๋œ๋‹ค. 1๊ธ€์ž์งœ๋ฆฌ ์ค‘์‹ฌ์ด๊ฑฐ๋‚˜ ํ˜น์€ ๋‘ ๊ธ€์ž์งœ๋ฆฌ ํšŒ๋ฌธ์˜ ์™ผ์ชฝ ์ค‘์‹ฌ์ด๋‹ค.
29+
* 3. c๋ฅผ ํ™€์ˆ˜ ๊ธธ์ด ํšŒ๋ฌธ์˜ ์ค‘์‹ฌ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค -> expand(s,c,c)
30+
* 4. c๋ถ€ํ„ฐ c+1๊นŒ์ง€ ์ง์ˆ˜ ๊ธธ์ด ํšŒ๋ฌธ์˜ ์™ผ์ชฝ ์ค‘์‹ฌ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค -> expand(s,c,c+1)
31+
* <p>
32+
* ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ์ด์œ ๋Š” ABA์™€ ABBA ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ์ปค๋ฒ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค.
33+
*/
34+
public int countSubstrings2(String s) {
35+
int n = s.length();
36+
int count = 0;
37+
for (int c = 0; c < n; c++) {
38+
count += expand(s, c, c); // ํ™€์ˆ˜ ๊ธธ์ด ์ค‘์‹ฌ
39+
count += expand(s, c, c + 1); // ์ง์ˆ˜ ๊ธธ์ด ์ค‘์‹ฌ
40+
}
41+
return count;
42+
}
43+
44+
private int expand(String s, int L, int R) {
45+
int add = 0;
46+
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
47+
add++;
48+
L--;
49+
R++;
50+
}
51+
return add;
52+
}
53+
54+
/**
55+
* ํ”„๋ผ์ด๋น— ๋ฉ”์„œ๋“œ "ํ™•์žฅ" ์„ ์–ธ
56+
* 1. L, R ํฌ์ธํ„ฐ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ›๋Š”๋‹ค.
57+
* 2. ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๋ฌธ์ž์—ด ๋‚ด ์œ ํšจํ•œ ๋ฒ”์œ„์— ์žˆ์–ด์•ผ ํ•œ๋‹ค.
58+
* 3. L๊ณผ R์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž๊ฐ€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
59+
* 2์™€ 3์„ ๋ชจ๋‘ ๋งŒ์กฑํ•œ๋‹ค๋ฉด ํšŒ๋ฌธ
60+
*/
61+
}

โ€Žreverse-bits/jinvicky.javaโ€Ž

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
public class Solution {
4+
public int reverseBits(int n) {
5+
int reversed = 0;
6+
7+
for (int i = 0; i < 32; i++) {
8+
reversed <<= 1;
9+
10+
int lsb_of_n = n & 1; // n์ด ํ™€์ˆ˜๋ฉด 1, ์ง์ˆ˜๋ฉด 0์„ ๋ฐ˜ํ™˜
11+
12+
reversed |= lsb_of_n;
13+
14+
n >>= 1;
15+
}
16+
return reversed;
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int getSum(int a, int b) {
3+
while (b != 0) {
4+
5+
// ๋ณ€์ˆ˜ 2๊ฐœ์™€ ๋น„ํŠธ ์—ฐ์‚ฐ
6+
// 1.
7+
int sum = a ^ b;
8+
9+
// 2.
10+
int carry = (a & b) << 1;
11+
12+
/// 3.
13+
a = sum;
14+
b = carry; // ํ•œ๋ฒˆํ•˜๊ณ  ๋ง๋ฉด b๋Š” ์“ฐ์ผ ์ผ์ด ์—†๋Š”๋ฐ? -> ๋น ๋œจ๋ฆฐ ๋ฐ˜๋ณต๋ฌธ์ด ์—†๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
15+
16+
}
17+
return a;
18+
}
19+
}

0 commit comments

Comments
ย (0)