Skip to content

Commit fddf7c3

Browse files
authored
Merge pull request #882 from imsosleepy/main
[ackku] Week 5
2 parents d688a25 + c026aed commit fddf7c3

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ํˆฌํฌ์ธํ„ฐ๋ฅผ ์“ธ ํ•„์š” ์—†์Œ. ๊ทธ๋ƒฅ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์•„ ๋นผ์ฃผ๋ฉด O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
2+
// ํˆฌํฌ์ธํ„ฐ์™€ ํฐ ์ฐจ์ด๊ฐ€ ์—†๋Š” ์ด์œ ๋Š” ํˆฌํฌ์ธํ„ฐ๋„ O(N)์ด๊ธฐ ๋•Œ๋ฌธ. ์•„์ฃผ ์•ฝ๊ฐ„์˜ ๊ณต๊ฐ„๋ณต์žก๋„ ์ฐจ์ด๋งŒ ์žˆ์„ ๋“ฏ
3+
// ๊ฒฐ๊ณผ๋Š” ํฐ ์ฐจ์ด ์—†์œผ๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๊ฐ€ ๋” ์‰ฝ๋‹ค.
4+
class Solution {
5+
public int maxProfit(int[] prices) {
6+
int minPrice = Integer.MAX_VALUE;
7+
int maxProfit = 0;
8+
9+
for (int price : prices) {
10+
if (price < minPrice) {
11+
minPrice = price;
12+
} else {
13+
maxProfit = Math.max(maxProfit, price - minPrice);
14+
}
15+
}
16+
17+
return maxProfit;
18+
}
19+
}
20+
21+
// ์ฒ˜์Œ ์ƒ๊ฐํ•œ ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹
22+
class Solution {
23+
public int maxProfit(int[] prices) {
24+
int left = 0;
25+
int right = 1;
26+
int maxProfit = 0;
27+
28+
while (right < prices.length) {
29+
if (prices[left] < prices[right]) {
30+
int profit = prices[right] - prices[left];
31+
maxProfit = Math.max(maxProfit, profit);
32+
} else {
33+
left = right;
34+
}
35+
right++;
36+
}
37+
38+
return maxProfit;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// ์•„์ด๋””์–ด๋กœ ํ‘ธ๋Š” ๋ฌธ์ œ๋ผ ์„ ํ˜ธํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ œ...
2+
// ๊ทธ๋ƒฅ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ตฌ๋ถ„์ž๋กœ ๋‘๊ณ  ์Šคํ”Œ๋ฆฟํ•˜๋Š”๊ฒŒ ๊ฐ€์žฅ ํŽธํ•˜๋‹ค. ์•„์˜ˆ ๋‚˜์˜ค์ง€ ์•Š์„ ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‘๋ฉด ๊ธธ์ด๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ
3+
public class Solution {
4+
// ์ธ์ฝ”๋”ฉ ๋ฉ”์„œ๋“œ
5+
public String encode(List<String> strs) {
6+
StringBuilder encodedString = new StringBuilder();
7+
8+
for (String str : strs) {
9+
encodedString.append(str.length()).append("#").append(str);
10+
}
11+
12+
return encodedString.toString();
13+
}
14+
15+
// ๋””์ฝ”๋”ฉ ๋ฉ”์„œ๋“œ
16+
public List<String> decode(String s) {
17+
List<String> decodedList = new ArrayList<>();
18+
int i = 0;
19+
20+
while (i < s.length()) {
21+
22+
int j = i;
23+
while (s.charAt(j) != '#') {
24+
j++;
25+
}
26+
27+
int length = Integer.parseInt(s.substring(i, j));
28+
decodedList.add(s.substring(j + 1, j + 1 + length));
29+
30+
i = j + 1 + length;
31+
}
32+
33+
return decodedList;
34+
}
35+
}
36+
// ๐Ÿš€๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌ
37+
// @!#$@#$ ์ด๋Ÿฐ๊ฑธ ์Šคํ”Œ๋ฆฟ ๋ฌธ์ž๋กœ ๋‘๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.์•„์ด์˜จ
38+
public class Solution {
39+
40+
public String encode(List<String> strs) {
41+
StringBuilder encodedString = new StringBuilder();
42+
43+
for (String str : strs) {
44+
encodedString.append(str).append("๐Ÿš€");
45+
}
46+
47+
return encodedString.toString();
48+
}
49+
50+
public List<String> decode(String s) {
51+
String[] parts = s.split("๐Ÿš€");
52+
List<String> decodedList = new ArrayList<>();
53+
for (String part : parts) {
54+
if (!part.isEmpty()) {
55+
decodedList.add(part);
56+
}
57+
}
58+
59+
return decodedList;
60+
}
61+
}

โ€Žgroup-anagrams/imsosleepy.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ •๋ ฌํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๊ฝค ๋‚˜์˜ค๋Š” ๋ฌธ์ œ
2+
// ํ•˜์ง€๋งŒ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ์ •๋ ฌํ•ด๋„ ๋œ๋‹ค๋Š”๊ฑด ๊ธ€์ž ์ˆ˜๊ฐ€ 100์ž ์ œํ•œ์ด ์žˆ์–ด์„œ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋‹ค.
3+
// O(N) * O(MlogM) N ๋ฐฐ์—ด ๊ธธ์ด, M ๊ธ€์ž์ˆ˜์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋‚˜์˜ด
4+
class Solution {
5+
public List<List<String>> groupAnagrams(String[] strs) {
6+
HashMap<String,List<String>> anagrams = new HashMap<>();
7+
for(String str: strs) {
8+
char[] charArray = str.toCharArray();
9+
Arrays.sort(charArray);
10+
String sortedWord = new String(charArray);
11+
anagrams.computeIfAbsent(sortedWord, k -> new ArrayList<>()).add(str);
12+
}
13+
14+
return new ArrayList<>(anagrams.values());
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// GPT์˜ ํ’€์ด. ํŠธ๋ฆฌ๋ฅผ ์ด์šฉํ•ด O(m)์œผ๋กœ ํ•ด๊ฒฐํ–ˆ๋‹ค.
2+
public class Trie {
3+
private class TrieNode {
4+
TrieNode[] children;
5+
boolean isEndOfWord;
6+
7+
public TrieNode() {
8+
children = new TrieNode[26]; // ์•ŒํŒŒ๋ฒณ a~z (26๊ฐœ์˜ ์ž์‹ ๋…ธ๋“œ)
9+
isEndOfWord = false; // ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ๋‹จ์–ด์˜ ๋์ธ์ง€ ์•„๋‹Œ์ง€ ๋‚˜ํƒ€๋ƒ„
10+
}
11+
}
12+
13+
private TrieNode root;
14+
15+
public Trie() {
16+
root = new TrieNode(); // ๋ฃจํŠธ ๋…ธ๋“œ ์ดˆ๊ธฐํ™”
17+
}
18+
19+
public void insert(String word) {
20+
TrieNode node = root;
21+
for (char c : word.toCharArray()) {
22+
int index = c - 'a'; // ์•ŒํŒŒ๋ฒณ์„ 0-25์˜ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
23+
if (node.children[index] == null) {
24+
node.children[index] = new TrieNode(); // ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์—†์œผ๋ฉด ์ƒˆ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑ
25+
}
26+
node = node.children[index]; // ์ž์‹ ๋…ธ๋“œ๋กœ ์ด๋™
27+
}
28+
node.isEndOfWord = true; // ๋‹จ์–ด์˜ ๋์„ ํ‘œ์‹œ
29+
}
30+
31+
public boolean search(String word) {
32+
TrieNode node = root;
33+
for (char c : word.toCharArray()) {
34+
int index = c - 'a';
35+
if (node.children[index] == null) {
36+
return false; // ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ์—†์œผ๋ฉด false ๋ฐ˜ํ™˜
37+
}
38+
node = node.children[index]; // ์ž์‹ ๋…ธ๋“œ๋กœ ์ด๋™
39+
}
40+
return node.isEndOfWord; // ๋‹จ์–ด์˜ ๋์ธ์ง€๋ฅผ ํ™•์ธ
41+
}
42+
43+
public boolean startsWith(String prefix) {
44+
TrieNode node = root;
45+
for (char c : prefix.toCharArray()) {
46+
int index = c - 'a';
47+
if (node.children[index] == null) {
48+
return false; // ํ•ด๋‹น ์ ‘๋‘์‚ฌ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ์—†์œผ๋ฉด false ๋ฐ˜ํ™˜
49+
}
50+
node = node.children[index]; // ์ž์‹ ๋…ธ๋“œ๋กœ ์ด๋™
51+
}
52+
return true; // ์ ‘๋‘์‚ฌ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฉด true ๋ฐ˜ํ™˜
53+
}
54+
}
55+
56+
// ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ ํ’€์–ด๋ดค๋Š”๋ฐ, ์†๋„๊ฐ€ ๋„ˆ๋ฌด ์•ˆ๋‚˜์™”์Œ
57+
// O(n*m)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋‚˜์˜ด. n์€ ๊ธ€์ž์ˆ˜ m์€ ๊ธ€์ž๊ธธ์ด
58+
class Trie {
59+
60+
private List<String> words;
61+
62+
public Trie() {
63+
words = new ArrayList<>();
64+
}
65+
66+
public void insert(String word) {
67+
words.add(word);
68+
}
69+
70+
public boolean search(String word) {
71+
return backtrack(word, true);
72+
}
73+
74+
public boolean startsWith(String prefix) {
75+
return backtrack(prefix, false);
76+
}
77+
78+
private boolean backtrack(String target, boolean exactMatch) {
79+
for (String word : words) {
80+
if (exactMatch) {
81+
if (word.equals(target)) {
82+
return true;
83+
}
84+
} else {
85+
if (word.startsWith(target)) {
86+
return true;
87+
}
88+
}
89+
}
90+
return false;
91+
}
92+
}

โ€Žword-break/imsosleepy.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// dp ๋ฐฐ์—ด (dp[i] = s[0...i]๊ฐ€ ๋‹จ์–ด๋“ค๋กœ ๋‚˜๋ˆ ์งˆ ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€)
2+
// ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๊ฐ€ ํฌ์ง€ ์•Š์•„์„œ O(N^2)๋„ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ.
3+
class Solution {
4+
public boolean wordBreak(String s, List<String> wordDict) {
5+
Set<String> wordSet = new HashSet<>(wordDict);
6+
boolean[] dp = new boolean[s.length() + 1];
7+
dp[0] = true;
8+
9+
for (int i = 1; i <= s.length(); i++) {
10+
for (int j = 0; j < i; j++) {
11+
if (dp[j] && wordSet.contains(s.substring(j, i))) {
12+
dp[i] = true;
13+
break;
14+
}
15+
}
16+
}
17+
18+
return dp[s.length()];
19+
}
20+
}

0 commit comments

Comments
ย (0)