Skip to content

Commit d4640e5

Browse files
authored
Merge pull request #1402 from Yn3-3xh/main
[Yn3-3xh] WEEK 05 Solutions
2 parents 92e54ef + b91d5be commit d4640e5

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
[๋ฌธ์ œํ’€์ด]
3+
- ์ž‘์€์ˆ˜๋ฅผ ๋‘๊ณ , ํฐ์ˆ˜์— ๋บ€ ๊ฐ’์„ ๊ตฌํ•˜์ž.
4+
time: O(N), space: O(1)
5+
6+
[ํšŒ๊ณ ]
7+
์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋‚œ์ด๋„๊ฐ€ easy์ธ ๋•๋ถ„์— ๋ฌด๋ฆฌ์—†์ด ํ’€์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
8+
*/
9+
class Solution {
10+
public int maxProfit(int[] prices) {
11+
int min = prices[0];
12+
int max = 0;
13+
for (int i = 1; i < prices.length; i++) {
14+
min = Math.min(min, prices[i]);
15+
max = Math.max(max, prices[i] - min);
16+
}
17+
return max;
18+
}
19+
}

โ€Žgroup-anagrams/Yn3-3xh.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
[๋ฌธ์ œํ’€์ด]
3+
- ์ด์ „์— ๋ฐฐ์—ด๋กœ ํ’€์—ˆ๋˜ ์•„๋‚˜๊ทธ๋žจ ์†”๋ฃจ์…˜์„ ์ฐธ๊ณ ํ•ด๋ณด์ž.
4+
time: O(N^2 * M), space: O(N * M)
5+
class Solution {
6+
public List<List<String>> groupAnagrams(String[] strs) {
7+
List<List<String>> result = new ArrayList<>();
8+
boolean[] used = new boolean[strs.length];
9+
10+
for (int i = 0; i < strs.length; i++) {
11+
if (used[i]) {
12+
continue;
13+
}
14+
15+
List<String> sub = new ArrayList<>();
16+
String current = strs[i];
17+
sub.add(current);
18+
used[i] = true;
19+
20+
for (int j = i + 1; j < strs.length; j++) {
21+
String next = strs[j];
22+
if (isAnagram(current, next)) {
23+
sub.add(next);
24+
used[j] = true;
25+
}
26+
}
27+
result.add(sub);
28+
}
29+
return result;
30+
}
31+
32+
private boolean isAnagram(String current, String next) {
33+
if (current.length() != next.length()) {
34+
return false;
35+
}
36+
37+
int[] counting = new int[26];
38+
for (char ch : current.toCharArray()) {
39+
counting[ch - 'a']++;
40+
}
41+
for (char ch : next.toCharArray()) {
42+
counting[ch - 'a']--;
43+
}
44+
45+
for (int count : counting) {
46+
if (count != 0) {
47+
return false;
48+
}
49+
}
50+
return true;
51+
}
52+
}
53+
54+
- sorting
55+
time: O(N * M log M), space: O(N * M)
56+
57+
[ํšŒ๊ณ ]
58+
์ค‘์ฒฉ for๋ฌธ์„ ์จ์„œ ์‹œ๊ฐ„๋ณต์žก๋„ ์ปคํŠธ๋ผ์ธ์— ํ„ฑ๊ฑธ์ดํ–ˆ๋‹ค..
59+
60+
์ด์ „์— ํ’€์—ˆ๋˜ ์•„๋‚˜๊ทธ๋žจ์„ ๋ฒ ์ด์Šค๋กœ ํ’€๋‹ค ๋ณด๋‹ˆ ์ตœ์ ํ™”ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™๋‹ค.
61+
๋ฌธ์ œ์— ๋งž๋Š” ํ’€์ด๋ฒ•์„ ์ƒ๊ฐํ•ด๋ณด์ž..
62+
63+
์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ๋ฌธ์ž ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ณ , ๋ฌธ์ž ๋ฐฐ์—ด์„ sortingํ•œ๋‹ค๋ฉด ์•„๋‚˜๊ทธ๋žจ์˜ key๊ฐ€ ๋œ๋‹ค.
64+
key๊ฐ€ ์—†์œผ๋ฉด list๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ์žˆ์œผ๋ฉด ๊ทธ key์— ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ฃผ์ž.
65+
ํ•ด์„ค์„ ๋ณด๊ณ , ์ดํ•ดํ•˜๊ณ  ํ’€๊ณ ๋‚˜๋ฉด ์‰ฌ์šด๋ฐ..
66+
์™œ ์ฒ˜์Œ์— ๋ชป ํ’€๊นŒ..
67+
*/
68+
class Solution {
69+
public List<List<String>> groupAnagrams(String[] strs) {
70+
Map<String, List<String>> result = new HashMap<>();
71+
72+
for (String str : strs) {
73+
char[] tempArray = str.toCharArray();
74+
Arrays.sort(tempArray);
75+
String key = new String(tempArray);
76+
77+
result.putIfAbsent(key, new ArrayList<>());
78+
result.get(key).add(str);
79+
}
80+
return new ArrayList<>(result.values());
81+
}
82+
}
83+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
[๋ฌธ์ œํ’€์ด]
3+
- Node๋ฅผ ๋งŒ๋“ค์ž
4+
time: O(N), space: O(N)
5+
6+
[ํšŒ๊ณ ]
7+
๋…ธ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ์ ‘๊ทผ์€ ๋งž์•˜๋Š”๋ฐ, ๊ทธ ๋’ค๋กœ ์•ˆํ’€๋ ค์„œ ํ•ด์„ค์„ ๋ณด๋‹ˆ ๋…ธ๋“œ์˜ ๊ธธ์ด๋ฅผ 26์œผ๋กœ ํ•œ๋‹ค.
8+
์ด ๋ถ€๋ถ„์—์„œ ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์› ๋Š”๋ฐ ์•„๋ž˜๋ฅผ ๋ณด๊ณ  ์ดํ•ด๋๋‹ค.
9+
(root)
10+
โ”œโ”€โ”€ c
11+
โ”‚ โ”œโ”€โ”€ a
12+
โ”‚ โ”‚ โ”œโ”€โ”€ t (end)
13+
โ”‚ โ”‚ โ””โ”€โ”€ r (end)
14+
โ””โ”€โ”€ d
15+
โ””โ”€โ”€ o
16+
โ””โ”€โ”€ g (end)
17+
18+
์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ํ•ธ๋“ค๋งํ•˜๋Š” ์—ญํ• ์€ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์ง€๋„๋ก ๋…ธ๋“œ ์•ˆ์— ์†”๋ฃจ์…˜์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.
19+
*/
20+
class Trie {
21+
Node root;
22+
23+
public Trie() {
24+
root = new Node();
25+
}
26+
27+
public void insert(String word) {
28+
root.insert(word);
29+
}
30+
31+
public boolean search(String word) {
32+
return root.search(word);
33+
}
34+
35+
public boolean startsWith(String prefix) {
36+
return root.startsWith(prefix);
37+
}
38+
}
39+
40+
class Node {
41+
Node[] nodes;
42+
boolean isEnd;
43+
44+
Node() {
45+
nodes = new Node[26];
46+
}
47+
48+
void insert(String word) {
49+
Node current = this;
50+
for (char ch : word.toCharArray()) {
51+
int index = ch - 'a';
52+
if (current.nodes[index] == null) {
53+
current.nodes[index] = new Node();
54+
}
55+
current = current.nodes[index];
56+
}
57+
current.isEnd = true;
58+
}
59+
60+
boolean search(String word) {
61+
Node current = this;
62+
for (char ch : word.toCharArray()) {
63+
int index = ch - 'a';
64+
if (current.nodes[index] == null) {
65+
return false;
66+
}
67+
current = current.nodes[index];
68+
}
69+
70+
if (current.isEnd) {
71+
return true;
72+
}
73+
return false;
74+
}
75+
76+
boolean startsWith(String prefix) {
77+
Node current = this;
78+
for (char ch : prefix.toCharArray()) {
79+
int index = ch - 'a';
80+
if (current.nodes[index] == null) {
81+
return false;
82+
}
83+
current = current.nodes[index];
84+
}
85+
return true;
86+
}
87+
}
88+
89+
/**
90+
* Your Trie object will be instantiated and called as such:
91+
* Trie obj = new Trie();
92+
* obj.insert(word);
93+
* boolean param_2 = obj.search(word);
94+
* boolean param_3 = obj.startsWith(prefix);
95+
*/
96+

โ€Žword-break/Yn3-3xh.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
[๋ฌธ์ œํ’€์ด]
3+
- startsWith์œผ๋กœ ์žˆ์œผ๋ฉด substringํ•˜๋ฉด์„œ ๋ฐ˜๋ณตํ•ด๋ณด์ž.
4+
- ์ฃผ์–ด์ง„ list๋ฅผ map์œผ๋กœ ํ™œ์šฉ
5+
- failed
6+
s = "cars"
7+
wordDict = ["car","ca","rs"]
8+
---
9+
class Solution {
10+
public boolean wordBreak(String s, List<String> wordDict) {
11+
for (String word: wordDict) {
12+
s = s.replace(word, "");
13+
}
14+
15+
if (s.length() == 0) {
16+
return true;
17+
}
18+
return false;
19+
}
20+
}
21+
22+
- DP
23+
time: O(N^2), space: O(N)
24+
25+
[ํšŒ๊ณ ]
26+
์ฒ˜์Œ์—” ๋„ˆ๋ฌด ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•ด์„œ ์•ˆ๋  ๊ฒƒ ๊ฐ™๊ธดํ–ˆ๋‹ค..
27+
DP๋กœ ํ’€๋ฉด ๋  ๊ฒƒ ๊ฐ™์€๋ฐ..
28+
ํ•ด์„ค์„ ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ๋˜์ง€๋งŒ, ํ•ญ์ƒ DP ์ ‘๊ทผ์ด ์ž˜ ์•ˆ๋œ๋‹ค..
29+
*/
30+
class Solution {
31+
public boolean wordBreak(String s, List<String> wordDict) {
32+
// "applepenapple", ["apple","pen"]
33+
int sLen = s.length();
34+
boolean[] dp = new boolean[sLen + 1];
35+
dp[0] = true;
36+
37+
int maxLen = 0;
38+
for (String word: wordDict) {
39+
maxLen = Math.max(maxLen, word.length());
40+
} // 5
41+
42+
for (int i = 1; i <= sLen; i++) { // 1 ~ 13
43+
// applepenapple > apple > 5
44+
for (int j = i - 1; j >= 0; j--) {
45+
// applepenapple > apple > index:4 > 0
46+
if (dp[j] && wordDict.contains(s.substring(j, i))) {
47+
dp[i] = true;
48+
break;
49+
}
50+
}
51+
}
52+
return dp[sLen];
53+
}
54+
}
55+

0 commit comments

Comments
ย (0)