Skip to content

Commit 841cfc8

Browse files
authored
Merge pull request #2160 from YuuuuuuYu/main
[YuuuuuuYu] WEEK 05 solutions
2 parents 807a383 + 53620e3 commit 841cfc8

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Runtime: 1ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 94.08MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜
9+
* - ์ฃผ์–ด์ง„ prices ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ์ตœ์†Œ ๊ฐ€๊ฒฉ(min)์„ ๊ฐฑ์‹ 
10+
*/
11+
class Solution {
12+
public int maxProfit(int[] prices) {
13+
int maxProfit = 0;
14+
int min = prices[0];
15+
16+
for (int i=1; i<prices.length; i++) {
17+
if (min >= prices[i]) {
18+
min = prices[i];
19+
} else {
20+
maxProfit = Math.max(maxProfit, prices[i]-min);
21+
}
22+
}
23+
24+
return maxProfit;
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Runtime: 6ms
3+
* Time Complexity: O(n x k log k)
4+
* - n: ๋ฌธ์ž์—ด ๊ฐœ์ˆ˜
5+
* - k: ๋ฌธ์ž์—ด ์ตœ๋Œ€ ๊ธธ์ด
6+
* - k log k: ๊ฐ ๋ฌธ์ž์—ด์„ ์ •๋ ฌํ•˜๋Š” ์‹œ๊ฐ„
7+
*
8+
* Memory: 49.30MB
9+
* Space Complexity: O(n x k)
10+
* - ์ตœ๋Œ€ n๊ฐœ์˜ ํ‚ค-๊ฐ’
11+
*
12+
* Approach: ์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•ด์„œ ๊ฐ™์€ ์• ๋‚˜๊ทธ๋žจ๋ผ๋ฆฌ ๊ทธ๋ฃนํ™”
13+
*/
14+
class Solution {
15+
public List<List<String>> groupAnagrams(String[] strs) {
16+
Map<String, List<String>> anagramMap = new HashMap<>();
17+
18+
for (String str: strs) {
19+
char[] chs = str.toCharArray();
20+
Arrays.sort(chs);
21+
String key = new String(chs);
22+
23+
anagramMap.computeIfAbsent(key, k -> new ArrayList<>())
24+
.add(str);
25+
}
26+
27+
return new ArrayList<>(anagramMap.values());
28+
}
29+
}

0 commit comments

Comments
ย (0)