Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- ์ธ๋ฑ์ค์คํต์ค์บ
- enum ์์ฑ์ ์ ๊ทผ ์ ํ์
- ์๋ฐ์์์ฐ๋ ๋๋ฅผ ์ฌ์ฉํ๋ ์ด์
- ์์
- builder null
- SVN
- ์ธ๋ฑ์คํ์ค์บ
- ์๋ฐ๋ผ์ด๋ธ์คํฐ๋
- item15
- ์๋ผ์คํฑ์์น๋ฉ์ธ์ง์ฉ๋
- ์๋ฐ์์
- index skip scan
- mysql
- ์์ดํ 15
- ์๋ฐ์ฐ๋ ๋
- Hikari Connection Pool
- mysql์ํคํ ์ฒ
- assert.notnull
- Item6
- ๋ฐ์ฉ๋ ์ค๊ตญ์ด
- ์ํฐ๋ ๋ณ์ข ๊ฐ๋ฐ์
- InnoDB์ํคํ ์ฒ
- ์ดํํฐ๋ธ์๋ฐ
- hikari cp ์ค์
- ๋น์ผ๊ฐ์ฒด์์ฑ
- ์ํฐ๋ ๊ฐ๋ฐ์ํ์ฌ
- index full scan
- ์๋ฐ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฒฉ๋ฆฌ์์ค
- effectiveJava
Archives
- Today
- Total
โ๐ป๊ธฐ๋กํ๋ ๋ธ๋ก๊ทธ
[ํ๋ก๊ทธ๋๋จธ์ค - ํด์] Lv2.์์ฅ ๋ณธ๋ฌธ
728x90
์ข ๋ฅ๋ณ ๊ฐ์ง์๋ฅผ ์ฒดํฌํ๊ธฐ ์ํด Map์ ์ฌ์ฉํ๋ค.
Map์ ํค๊ฐ ๊ณตํต์ผ๋ก ๋ฌถ์์ ์๋ ์ข ๋ฅ๊ฐ ๋๋ค.
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
Map<String, Integer> counts = new HashMap<String, Integer>();
for(String[] s : clothes) {
String type = s[1];
counts.put(type, counts.getOrDefault(type, 0) + 1);
}
int answer = 1;
for(Integer num : counts.values()) {
answer *= num + 1;//์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ ์ถ๊ฐํ์ฌ ๊ณฑํ๋ค.
}
return answer-1 ;//๋ชจ๋๋ค ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ๋ฅผ ๋บ๋ค.
}
}
์๋กญ๊ฒ ์๊ฒ๋๊ฒ
Map.getOrDefault(key๊ฐ, default value)
map์ ๊ฐ์ ๊บผ๋์ ๋ null์ธ ๊ฒฝ์ฐ ์ผํญ ์ฐ์ฐ์๋ฅผ ํตํด ๊ธธ์ด์ก์ง๋ง getOrDefault๋ฉ์๋๋ฅผ ํตํด ์ฝ๋๋ฅผ ์ค์ผ ์ ์๋ค.
stream์ ์ฌ์ฉํ ๋ฆฌํฉํฐ๋ง
import java.util.*;
class Solution {
public int solution(String[][] clothes) {
int answer = Arrays.stream(clothes)
.map(c ->c[1])
.distinct()
.map(type -> (int) Arrays.stream(clothes).filter(c -> c[1].equals(type)).count())
.map(c -> c +1)
.reduce(1,(c,n) -> c * n);
return answer -1;
}
}
ํ๋ก๊ทธ๋๋จธ์ค java ์ปค๋ฎค๋๋ง ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.
728x90
๋ฐ์ํ
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
leetcode - 217. Contains Duplicate (0) | 2022.01.08 |
---|---|
[์ ๋ ฌ] ๋ฒ๋ธ์ ๋ ฌ ( Bubble Sort ) (0) | 2020.10.25 |
ํด์ - Lv1.์์ฃผํ์ง ๋ชปํ ์ ์ (0) | 2020.09.27 |