Skip to content

Commit 5e1bebf

Browse files
authored
Merge pull request #1903 from geegong/main
[geegong] WEEK 08 solutions
2 parents fc87b20 + 941ab12 commit 5e1bebf

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.util.*;
2+
3+
/*
4+
// Definition for a Node.
5+
class Node {
6+
public int val;
7+
public List<Node> neighbors;
8+
public Node() {
9+
val = 0;
10+
neighbors = new ArrayList<Node>();
11+
}
12+
public Node(int _val) {
13+
val = _val;
14+
neighbors = new ArrayList<Node>();
15+
}
16+
public Node(int _val, ArrayList<Node> _neighbors) {
17+
val = _val;
18+
neighbors = _neighbors;
19+
}
20+
}
21+
*/
22+
public class Geegong {
23+
24+
/**
25+
* dfs ๋ฐฉ์‹์œผ๋กœ ํ’€์ด
26+
* visited ๋กœ memoization ํ•˜์—ฌ ๋ฐฉ๋ฌธํ•ด์„œ ๋ณต์‚ฌ๊ฐ€ ๋œ ๋…ธ๋“œ๋“ค์„ ์ €์žฅ
27+
* ํ•œ๋ฒˆ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๋…ธ๋“œ๋ฅผ neighbors ํƒ์ƒ‰์„ ํ†ตํ•ด ๋˜ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋˜๋ฉด visited ์—์„œ ๊บผ๋‚ด์„œ return ํ•˜๋„๋ก ํ•œ๋‹ค.
28+
*
29+
* time complexity : O(2N) -> O(N)
30+
* space complexity : O(N)
31+
* @param node
32+
* @return
33+
*/
34+
public Node cloneGraph(Node node) {
35+
// node์— ์ง„์ž…ํ•œ ์ˆœ๊ฐ„์— ๋ฐ”๋กœ visited ์— ๋„ฃ์–ด์„œ cloned ๋œ node ๋“ค์„ ์ง‘์–ด๋„ฃ๋„๋ก ๊ด€๋ฆฌ
36+
Map<Integer, Node> visited = new HashMap<>();
37+
38+
if (node == null) {
39+
return null;
40+
}
41+
42+
Node result = cloneDeeply(node, visited);
43+
return result;
44+
}
45+
46+
public Node cloneDeeply(Node origin, Map<Integer, Node> visited) {
47+
48+
// visited ์—๋Š” cloned ๋œ node ๋“ค์„ ์ €์žฅํ•˜๊ณ  ์žˆ์–ด ํ•œ๋ฒˆ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๋…ธ๋“œ๋ผ๋ฉด ๋ณต์‚ฌํ•œ ๋…ธ๋“œ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
49+
if (visited.containsKey(origin.val)) {
50+
return visited.get(origin.val);
51+
}
52+
53+
Node clonedTarget = new Node(origin.val);
54+
visited.put(origin.val, clonedTarget);
55+
56+
for (Node neighbor : origin.neighbors) {
57+
Node clonedNeighbor = cloneDeeply(neighbor, visited);
58+
clonedTarget.neighbors.add(clonedNeighbor);
59+
}
60+
61+
return clonedTarget;
62+
}
63+
64+
65+
// ์ด๋ฏธ ๋‹ค๋ฅธ ๋ถ„๋“ค์ด Node ํด๋ž˜์Šค๋ฅผ ๋งŽ์ด ๋งŒ๋“ค์–ด๋†“์œผ์…”์„œ.. ๊ฐœ์ธ ํด๋ž˜์Šค ์•ˆ์— ์ด๋„ˆ ํด๋ž˜์Šค๋กœ Node ๋งŒ๋“ฌ
66+
public static class Node {
67+
public int val;
68+
public List<Node> neighbors;
69+
public Node() {
70+
val = 0;
71+
neighbors = new ArrayList<Node>();
72+
}
73+
public Node(int _val) {
74+
val = _val;
75+
neighbors = new ArrayList<Node>();
76+
}
77+
public Node(int _val, ArrayList<Node> _neighbors) {
78+
val = _val;
79+
neighbors = _neighbors;
80+
}
81+
}
82+
}
83+
84+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.Comparator;
4+
import java.util.List;
5+
6+
public class Geegong {
7+
8+
/**
9+
* LIS, LCS ์™€ ์—ฐ๊ด€์ง€์–ด์„œ ํ’€์ด
10+
* 1. ๋จผ์ € text1์„ ํ›‘์–ด๋ณด๋Š”๋ฐ text1์— ์ค‘๋ณต๋˜๋Š” ์•ŒํŒŒ๋ฒณ์ด ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ์— ๊ฐ ์บ๋ฆญํ„ฐ๋ณ„๋กœ ์ธ๋ฑ์Šค๋“ค์„ ์ €์žฅ (๊ฐ ์›์†Œ๊ฐ€ arrayList๊ฐ€ ์žˆ๋Š” ๋ฐฐ์—ด)
11+
* 2. text2 ๋ฅผ ํ›‘์œผ๋ฉด์„œ ๋งค์นญ๋˜๋Š” ์บ๋ฆญํ„ฐ๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ 1์—์„œ ์ €์žฅ๋œ ์ธ๋ฑ์Šค๋“ค์„ ํ•œ ๋ฐฐ์—ด์•ˆ์— ๋‚˜์—ด
12+
* -> ์ด๋–„ ๋‚˜์—ดํ•ด์„œ ๋„ฃ์„๋•Œ๋งˆ๋‹ค ์—ญ์ˆœ์œผ๋กœ ์ง‘์–ด๋„ฃ๋Š”๊ฒŒ ์ค‘์š”! (์™œ๋ƒ๋ฉด ์ด ๋‚˜์—ด๋œ ์ธ๋ฑ์Šค๋“ค์„ ๊ฐ€์ง€๊ณ  LIS๋ฅผ ๊ตฌํ• ๊ฑฐ๋ผ์„œ)
13+
* 3. ๋‚˜์—ด๋œ ์ธ๋ฑ์Šค๋“ค์˜ ๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  LIS ๋ฅผ ๊ตฌํ•œ๋‹ค, ์ฆ‰ ์ด LIS์˜ ๊ธธ์ด๊ฐ€ LCS ์˜ ๊ธธ์ด๊ฐ€ ๋œ๋‹ค..!! (์™€์šฐ ์‹ ๊ธฐ)
14+
* ๊ทธ๋Ÿฌ๋‚˜ leet code ์— ๋Œ๋ ธ์„ ๋•Œ runtime ์ด ์˜ ์ข‹์ง€๋Š” ์•Š์Œ
15+
*
16+
* time complexity : O(M + N logN) => text2์— ๋Œ€ํ•ด์„œ ๋ฌธ์ž๋งˆ๋‹ค ๋ฐ”์ด๋„ˆ๋ฆฌ์„œ์น˜๊ฐ€ ์ˆ˜ํ–‰๋จ
17+
* space complexity : O(M+N)
18+
* @param text1
19+
* @param text2
20+
* @return
21+
*/
22+
public int longestCommonSubsequence(String text1, String text2) {
23+
char[] chText1 = text1.toCharArray();
24+
char[] chText2 = text2.toCharArray();
25+
26+
// text1๋งŒ ๊ฐ ์›์†Œ ๋ณ„๋กœ char๊ฐ€ ๊ฐ€์ง€๋Š” index๋ฅผ array ๋กœ ๊ฐ–๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑ
27+
List<Integer>[] positionIndices = new List[26];
28+
for (int index = 0; index<chText1.length; index++) {
29+
if (positionIndices[chText1[index] - 'a'] == null) {
30+
positionIndices[chText1[index] - 'a'] = new ArrayList();
31+
positionIndices[chText1[index] - 'a'].add(index);
32+
} else {
33+
positionIndices[chText1[index] - 'a'].add(index);
34+
}
35+
}
36+
37+
38+
// ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ LIS ๋ฅผ ๊ตฌํ• ๊ฒƒ์ž„
39+
List<Integer> indices = new ArrayList<>();
40+
for (int index=0; index<chText2.length; index++) {
41+
42+
char find = chText2[index];
43+
if (positionIndices[find-'a'] != null && positionIndices[find-'a'].size() > 0) {
44+
// ์—ญ์ˆœ (LIS ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ผ๋ถ€๋Ÿฌ ๋’ค์ง‘์–ด ๋†’์Œ, ์ฆ‰ ๊ฐ char ๋ณ„ LIS ๋ฅผ ๊ตฌํ• ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—..)
45+
// positionIndices ์—์„œ ๊ตฌํ–ˆ๋˜ ๊ฐ’๋“ค์„ ๊ทธ๋Œ€๋กœ addAll ํ•œ๋‹ค๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ์ด ๋˜๊ธฐ๋•Œ๋ฌธ์— ์ •ํ™•ํ•œ LIS ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค
46+
47+
indices.addAll(positionIndices[find-'a'].stream().sorted(Comparator.reverseOrder()).toList());
48+
}
49+
}
50+
51+
// find LIS
52+
return findLIS(indices).size();
53+
}
54+
55+
public List<Integer> findLIS(List<Integer> source) {
56+
if (source.size() == 0) {
57+
return source;
58+
}
59+
60+
List<Integer> LISResult = new ArrayList<>();
61+
for (int index=0; index<source.size(); index++) {
62+
int target = source.get(index);
63+
int insertionPosition = Collections.binarySearch(LISResult, target);
64+
//Returns:
65+
//the index of the search key, if it is contained in the list;
66+
// otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list:
67+
if (insertionPosition < 0) {
68+
insertionPosition = -insertionPosition - 1;
69+
}
70+
71+
if (LISResult.size() == insertionPosition) {
72+
LISResult.add(target);
73+
} else {
74+
LISResult.set(insertionPosition, target);
75+
}
76+
}
77+
78+
return LISResult;
79+
}
80+
81+
}
82+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
public class Geegong {
2+
3+
/**
4+
* leftIdx, rightIdx ๋ฅผ ๋‘ฌ์„œ ์™ผ์กฑ ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฅธ์ชฝ ์ธ๋ฑ์Šค๋ฅผ ์›€์ง์—ฌ๊ฐ€๋ฉฐ palindromic string ์„ ์ฐพ์•„๊ฐ„๋‹ค
5+
* time complexity : O(N^2)
6+
* space complexity : O(1)
7+
* @param s
8+
* @return
9+
*/
10+
public int countSubstrings(String s) {
11+
int count = 0;
12+
char[] sArray = s.toCharArray();
13+
14+
15+
for (int leftIdx = 0; leftIdx < sArray.length; leftIdx++) {
16+
int rightIdx = leftIdx + 1;
17+
18+
// ํ•œ๊ธ€์ž์”ฉ ๋”ฐ์กŒ์„ ๋•Œ palindrome
19+
count++;
20+
while(leftIdx <= rightIdx && rightIdx < sArray.length) {
21+
int offset = rightIdx - leftIdx;
22+
boolean isEqual = sArray[leftIdx] == sArray[rightIdx];
23+
24+
if (!isEqual) {
25+
rightIdx++;
26+
continue;
27+
}
28+
29+
if (offset == 1 && isEqual) {
30+
count++;
31+
rightIdx++;
32+
33+
} else { // isEqual ์ด๊ณ  offset > 1์ธ ์ผ€์ด์Šค
34+
// ์„œ๋กœ ์กฐ์—ฌ๊ฐ€๋ฉด์„œ palindrome ์ธ์ง€ ์ฒดํฌ
35+
count += isPalindromic(sArray, leftIdx + 1, rightIdx - 1);
36+
rightIdx++;
37+
}
38+
}
39+
}
40+
41+
return count;
42+
43+
}
44+
45+
public int isPalindromic(char[] origin, int leftIdx, int rightIdx) {
46+
// ์„œ๋กœ ์กฐ์—ฌ๊ฐ€๋ฉด์„œ palindrome ์ธ์ง€ ์ฒดํฌ
47+
while (leftIdx < rightIdx) {
48+
if (origin[leftIdx] == origin[rightIdx]) {
49+
leftIdx++;
50+
rightIdx--;
51+
continue;
52+
}
53+
54+
return 0;
55+
}
56+
57+
return 1;
58+
}
59+
60+
}
61+
62+

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Geegong {
2+
3+
/**
4+
* time complexity : O(1)
5+
* space complexity : O(1)
6+
* @param n
7+
* @return
8+
*/
9+
public int reverseBits(int n) {
10+
11+
// 32bit ๋งŒํผ ์›€์ง์ธ๋‹ค.
12+
// for (int index=32; index > 0; index--) {
13+
// // ๋งจ ์˜ค๋ฅธ์ชฝ 1bit๋งŒ ๊บผ๋‚ธ๋‹ค
14+
// int lastOneBit = n & 1;
15+
// // ๊ทธ๋Ÿฐ๋ฐ!! ์‹œํ”„ํŠธ์—ฐ์‚ฐ์ด ํ•˜์œ„ 5๋น„ํŠธ๊นŒ์ง€ ๋ฐ–์— ์•ˆ๋˜์„œใ… ใ…  << 32 ๊ฐ€ ์•„๋‹ˆ๊ฐ€ << 0 ์œผ๋กœ ๋˜์–ด๋ฒ„๋ฆผ
16+
// int temp = lastOneBit << index;
17+
// result = result | temp;
18+
// n = n >>> 1;
19+
// }
20+
21+
int result = 0;
22+
// ์‹œํ”„ํŠธ ์—ฐ์‚ฐ์„ 1๋น„ํŠธ์”ฉ ์›€์ง์ด๋Š”๊ฑธ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค
23+
for (int index=1; index <= 32; index++) {
24+
// ๋งˆ์ง€๋ง‰ 1๋น„ํŠธ๋ฅผ ์ถ”์ถœํ•˜๊ณ  ๊ทธ๊ฑธ result ์—์„œ ์™ผ์ชฝ์œผ๋กœ 1๋น„ํŠธ ์‹œํ”„ํŠธํ•œ๊ฑธ ํ•ฉ์ณ์•ผ ํ•œ๋‹ค.
25+
// 0001 -> 0010 -> 0100 -> .... ์š”๋ ‡๊ฒŒ
26+
result = (n & 1) | (result << 1);
27+
n = n >>> 1;
28+
}
29+
30+
return result;
31+
}
32+
33+
}
34+
35+
36+

0 commit comments

Comments
ย (0)