Skip to content

Commit 9a7b466

Browse files
authored
Merge pull request #2493 from hyeri0903/main
[hyeri0903] WEEK 05 Solutions
2 parents 45f646b + d212055 commit 9a7b466

File tree

5 files changed

+209
-0
lines changed

5 files changed

+209
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
/**
4+
1. ํ•˜๋ฃจ์— ํŒ”์•„์„œ ๊ฐ€์žฅ ์ตœ๋Œ€ ์ด์ต์„ ๊ตฌํ•˜๋„๋กํ•˜๋Š” max price return
5+
2. ์กฐ๊ฑด
6+
- ๋ฏธ๋ž˜ ๋‹ค๋ฅธ๋‚ ์งœ์— ํŒ๋งค (์ด์ „ ๋‚ ์งœ์— ํŒ๋งค x)
7+
- choosing a single day
8+
- ๋ฐฐ์—ด ๊ธธ์ด min = 1, max = 10^5
9+
- ์›์†Œ๊ฐ’ : min = 0, max = 10^4
10+
3. ํ’€์ด
11+
- 1)brtueforce: time complexity O(n^2), space: O(1)
12+
- 2)ํ˜„์žฌ ๊ฐ’ - ์ด์ „ ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ์ตœ์†Œ๊ฐ’ -> maxProfit , ์ฆ‰ min๊ฐ’์„ ๊ณ„์† ๊ธฐ์–ตํ•˜๋‹ค๊ฐ€ ํ˜„์žฌ ๊ฐ’๊ณผ์˜ ์ฐจ์ด ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•˜๋ฉด๋œ๋‹ค.
13+
- time: O(n)
14+
- space: O(1)
15+
*/
16+
17+
int maxProfit = 0;
18+
int minStock = Integer.MAX_VALUE;
19+
int n = prices.length;
20+
21+
for(int i = 0; i<n; i++) {
22+
if(prices[i] < minStock) {
23+
minStock = prices[i];
24+
}
25+
if(i > 0 && prices[i] - minStock > maxProfit) {
26+
maxProfit = Math.max(maxProfit, prices[i] - minStock);
27+
}
28+
}
29+
return maxProfit;
30+
31+
32+
// for(int i = 0; i < n; i++) {
33+
// int curStock = prices[i];
34+
// for(int j= i + 1; j < n; j++) {
35+
// if(curStock < prices[j]) {
36+
// int curProfit = prices[j] - curStock;
37+
// maxProfit = Math.max(maxProfit, curProfit);
38+
// }
39+
// }
40+
// }
41+
// return maxProfit;
42+
}
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
"""
3+
@param: strs: a list of strings
4+
@return: encodes a list of strings to a single string.
5+
"""
6+
def encode(self, strs: List[str]):
7+
text = ""
8+
for str in strs:
9+
text += f"{len(str)}:{str}"
10+
return text
11+
12+
"""
13+
@param: str: A string
14+
@return: decodes a single string to a list of strings
15+
"""
16+
def decode(self, s: str):
17+
output = []
18+
start = 0
19+
while start < len(s):
20+
mid = s.find(":", start)
21+
length = int(s[start:mid])
22+
output.append(s[mid + 1 : mid + 1 + length])
23+
start = mid + 1 + length
24+
return output
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public List<List<String>> groupAnagrams(String[] strs) {
3+
/**
4+
1.anagram ๋ผ๋ฆฌ ๊ทธ๋ฃนํ™”ํ•ด์„œ return
5+
2.์กฐ๊ฑด
6+
- answer in any order
7+
- strs ๊ธธ์ด ์ตœ์†Œ = 1, ์ตœ๋Œ€ = 10^4
8+
- ์›์†Œ ํ•˜๋‚˜๋‹น ๊ธธ์ด ์ตœ์†Œ = 0, ์ตœ๋Œ€ = 100
9+
- ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ
10+
3.ํ’€์ด
11+
- 1) i๋ฒˆ์งธ ์ดํ›„ ๋‹จ์–ด๋ฅผ ๋น„๊ตํ•ด๊ฐ€๋ฉด์„œ anagram check, time: O(n^2)
12+
- 2) HashMap: string element ๋ฅผ ์ •๋ ฌํ•ด์„œ key ๋กœ ์‚ฌ์šฉ, ์ค‘๋ณต๋˜๋Š” Key ์žˆ์œผ๋ฉด anagram, time: O(n)
13+
*/
14+
15+
int n = strs.length;
16+
//anagram ์ฒดํฌํ•  Map
17+
Map<String, List<String>> map = new HashMap<>();
18+
19+
for(int i = 0; i < n; i++) {
20+
String curStr = strs[i];
21+
char[] tmp = curStr.toCharArray();
22+
Arrays.sort(tmp);
23+
String key = new String(tmp);
24+
// System.out.println("curStr:" + curStr + ", key: " + key);
25+
//Map ์— ์ €์žฅ
26+
map.putIfAbsent(key, new ArrayList<>());
27+
map.get(key).add(curStr);
28+
// if(!map.containsKey(key)) {
29+
// List<String> words = new ArrayList<>();
30+
// words.add(curStr);
31+
// map.put(key, words);
32+
// } else {
33+
// List<String> words = map.get(key);
34+
// words.add(curStr);
35+
// map.put(key, words);
36+
// }
37+
}
38+
39+
40+
return new ArrayList<>(map.values());
41+
}
42+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class Trie {
2+
class TrieNode {
3+
TrieNode[] children;
4+
boolean isEnd;
5+
6+
public TrieNode() {
7+
children = new TrieNode[26]; // a ~ z
8+
isEnd = false;
9+
}
10+
}
11+
12+
private TrieNode root;
13+
14+
public Trie() {
15+
root = new TrieNode();
16+
}
17+
18+
public void insert(String word) {
19+
TrieNode node = root;
20+
21+
for(char c: word.toCharArray()) {
22+
int index = c - 'a';
23+
//์ฒ˜์Œ ๋“ค์–ด์˜ค๋Š” ๋‹จ์–ด๋Š” node ์ƒ์„ฑ
24+
if(node.children[index] == null) {
25+
node.children[index] = new TrieNode();
26+
}
27+
//๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™
28+
node = node.children[index];
29+
}
30+
//๋‹จ์–ด์˜ ๋ ํ‘œ์‹œ
31+
node.isEnd = true;
32+
}
33+
34+
public boolean search(String word) {
35+
TrieNode node = root;
36+
for(char c: word.toCharArray()) {
37+
int index = c - 'a';
38+
//์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด false return
39+
if(node.children[index] == null) {
40+
return false;
41+
}
42+
//๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™
43+
node = node.children[index];
44+
}
45+
return node.isEnd;
46+
}
47+
48+
public boolean startsWith(String prefix) {
49+
//์ค‘๊ฐ„์— null์„ ์•ˆ๋งŒ๋‚˜๋ฉด true ๋ฐ˜ํ™˜
50+
TrieNode node = root;
51+
for(char c: prefix.toCharArray()) {
52+
int index = c - 'a';
53+
if(node.children[index] == null) {
54+
return false;
55+
}
56+
node = node.children[index];
57+
}
58+
return true;
59+
}
60+
}
61+
62+
/**
63+
* Your Trie object will be instantiated and called as such:
64+
* Trie obj = new Trie();
65+
* obj.insert(word);
66+
* boolean param_2 = obj.search(word);
67+
* boolean param_3 = obj.startsWith(prefix);
68+
*/

โ€Žword-break/hyeri0903.javaโ€Ž

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public boolean wordBreak(String s, List<String> wordDict) {
3+
/**
4+
1.๋ฌธ์ œ: s ๊ฐ€ wordDict ๋กœ ์ชผ๊ฐœ์งˆ ์ˆ˜ ์žˆ๋Š”์ง€ true/false return
5+
2.์กฐ๊ฑด
6+
- s ์—๋Š” wordDict ๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Œ.
7+
- s.length() ์ตœ์†Œ = 1, ์ตœ๋Œ€ 300
8+
- wordDict.size() ์ตœ์†Œ 1, ์ตœ๋Œ€ 1000
9+
- ํ•œ ๋‹จ์–ด ๊ธธ์ด ์ตœ์†Œ=1, ์ตœ๋Œ€ = 20
10+
- ๋ชจ๋‘ ์†Œ๋ฌธ์ž. ๋ชจ๋“  wordDict ๊ฐ’์€ unique
11+
3.ํ’€์ด
12+
- i๋ฒˆ์งธ๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ check
13+
*/
14+
15+
int n = s.length();
16+
boolean[] dp = new boolean[n+1];
17+
18+
dp[0] = true; //๋นˆ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ
19+
20+
for(int i = 1; i < n+1; i++) {
21+
for(int j = 0; j < i; j++) {
22+
String subStr = s.substring(j, i);
23+
//์•ž๋‹จ๊ณ„ ๋ชจ๋‘ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ  && wordDict์— ์กด์žฌํ•˜๋Š”์ง€ ์ฒดํฌ
24+
if(dp[j] && wordDict.contains(subStr)) {
25+
dp[i] = true;
26+
break;
27+
}
28+
}
29+
}
30+
return dp[n];
31+
}
32+
}

0 commit comments

Comments
ย (0)