๋ฌธ์
https://programmers.co.kr/learn/courses/30/lessons/17677
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
์์ค์ฝ๋
import java.util.PriorityQueue;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class Solution {
public int solution(String str1, String str2) {
PriorityQueue<String> q1 = setQueue(str1);
PriorityQueue<String> q2 = setQueue(str2);
if(q1.isEmpty() && q2.isEmpty()) return 65536;
double inter = 0, union = 0;
while(!q1.isEmpty() && !q2.isEmpty()) {
String s1 = q1.poll();
String s2 = q2.poll();
int cmp = s1.compareTo(s2);
if(cmp == 0) inter++;
else if(cmp < 0) q2.offer(s2);
else q1.offer(s1);
union++;
}
union += (q1.size() + q2.size());
return (int)((inter/union)*65536);
}
public PriorityQueue<String> setQueue(String str) {
return IntStream.rangeClosed(0, str.length()-2)
.mapToObj((n) -> str.substring(n, n+2).toUpperCase())
.filter(s -> s.matches("[a-zA-Z]{2}"))
.collect(Collectors.toCollection(PriorityQueue::new));
}
}
ํ๊ณ
- ๋น ๋ฅธ ๋น๊ต๋ฅผ ์ํด ๋ฌธ์์ด ์ ๋ ฌ์ด ํ์ํ์.
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[2019 ์นด์นด์ค ๊ฐ๋ฐ์ ๊ฒจ์ธ ์ธํด์ญ] ์ง๊ฒ๋ค๋ฆฌ ๊ฑด๋๊ธฐ (0) | 2020.04.01 |
---|---|
[2019 ์นด์นด์ค ๊ฐ๋ฐ์ ๊ฒจ์ธ ์ธํด์ญ] ํธํ ๋ฐฉ ๋ฐฐ์ (0) | 2020.04.01 |
[2018 ์นด์นด์ค ๋ธ๋ผ์ธ๋ ์ฑ์ฉ] ์ ํ๋ฒ์ค (0) | 2020.03.16 |
[2018 ์นด์นด์ค ๋ธ๋ผ์ธ๋ ์ฑ์ฉ] ์ถ์ ํธ๋ํฝ (0) | 2020.03.16 |
[2018 ์นด์นด์ค ๋ธ๋ผ์ธ๋ ์ฑ์ฉ] ํ์ผ๋ช ์ ๋ ฌ (0) | 2020.03.05 |