두 정수 left와 right가 매개변수로 주어집니다. left부터 right까지의 모든 수들 중에서, 약수의 개수가 짝수인 수는 더하고, 약수의 개수가 홀수인 수는 뺀 수를 return 하도록 solution 함수를 완성해주세요.
나의 풀이
public int solution(int left, int right) {
int answer = 0;
for(int i=left; i<=right; i++){
if(Math.ceil(Math.sqrt(i)) == Math.floor(Math.sqrt(i))){
answer -= i;
}else{
answer += i;
}
}
return answer;
}
접근
(수학적으로 생각해보면) 약수의 개수가 홀수라면 소수이다.
따라서 해당 숫자의 제곱근이 자연수라면, 약수의 개수가 홀수개 이고, 그럴 경우에 값을 빼주고
나머지 숫자는 더하도록 구상
다른 사람의 풀이
public int solution(int left, int right) {
int sum = 0;
for (int i = left; i <= right; i++) {
sum += i * ((countDenominators(i) % 2 == 0) ? 1 : -1);
}
return sum;
}
private int countDenominators(int num) {
int count = 0;
for (int i = 1; i <= num; i++) if (num % i == 0) count++;
return count;
}
오랜만에 나의 풀이가 꽤 좋은 평가를 받는 축이었다.
대부분은 중첩 for문을 사용해서 직접 해당수의 약수를 직접 구해서 판단하는 로직이었다.
느낀점
도메인에 대한 지식의 중요성 : 해당 분야에 대해 알고 있다면, 생각보다 빠르고 쉬운 풀이를 알 수 있다
자연수(정수)를 판단하는게 의외로 잘 생각이 나지 않아 올림/내림을 사용했는데 좋은 방법이 있다 i % Math.sqrt(i) == 0
수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다.
마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요.
나의 풀이
/* 힌트보고 재작성 */
public static String solution(String[] participant, String[] completion) {
String answer = "";
Map<String,Integer> participantMap = new HashMap<>();
for (String partName : participant) {
participantMap.put(partName, participantMap.getOrDefault(partName, 0)+1);
}
for (String compName : completion) {
participantMap.put(compName, participantMap.get(compName)-1);
}
for(String name : participantMap.keySet()){
if(participantMap.get(name) == 1){
answer = name;
}
}
System.out.println("### answer : "+answer);
return answer;
}
/* ArrayList를 활용 -> 라인은 줄었지만, 성능에서 또 걸림 */
public static String solutionSecond(String[] participant, String[] completion) {
String answer = "";
List<String> participantList = new ArrayList<>();
for (String person : participant) {
participantList.add(person);
}
for (String comp : completion) {
participantList.remove(comp);
}
answer=participantList.get(0);
System.out.println("### answer : "+answer);
return answer;
}
접근 방법
처음에 중첩 loop를 사용해서 하나씩 제거하는 방법을 생각했지만, 시간이 오래걸려서 채점에서 CUT 힌트를 보고서야 이 문제가 해시를 이용한 문제라는걸 알았다... 처음엔 Hash Set 을 이용했지만, 중복이 없어지는 Hash의 특성으로 계속 막혀서 결국 힌트를 봤다.
다른 사람의 풀이를 슬쩍 봤는데, 성능을 위해 해시를 쓰되, 중복처리를 위해 맵을 이용해 count를 하는게 인상깊었다
다른 사람의 풀이
HashMap 사용
import java.util.HashMap;
class Solution {
public String solution(String[] participant, String[] completion) {
String answer = "";
HashMap<String, Integer> hm = new HashMap<>();
for (String player : participant) hm.put(player, hm.getOrDefault(player, 0) + 1);
for (String player : completion) hm.put(player, hm.get(player) - 1);
for (String key : hm.keySet()) {
if (hm.get(key) != 0){
answer = key;
}
}
return answer;
}
}
Sorting 사용
import java.util.*;
class Solution {
public String solution(String[] participant, String[] completion) {
Arrays.sort(participant);
Arrays.sort(completion);
int i;
for ( i=0; i<completion.length; i++){
if (!participant[i].equals(completion[i])){
return participant[i];
}
}
return participant[i];
}
}
비록 해시 문제이지만, 개인적으론 풀이법은 아래가 좀 더 멋져보인다
느낀점
일단, 어떤 문제든 일단 중첩 loop로 접근하는 나를 보며, 아쉬웠다
이번에 적절한 자료구조를 찾는게 힘들었다. Hash 까지는 문제를 보며 찾아갔지만, 풀이를 보고서야 HashMap을 쓰는 이유를 알게 되다니..
성능을 위해서 Hash를 사용했고
중복 체크를 위해 Map을 사용했다
getOrDefault() : map에서 get을 하되 없을때 default 값 지정가능
그외의 방법으로는 Array로도 Sorting을 사용하면 시간안에 해결이 가능했지만,
해시에 집착하다보니 Sorting을 더 생각해보지 못해 아쉽다
여러개의 목록에서 중복/여부 체크시 -> Array라면 Sorting 해서 접근하는 방법도 좋을듯하다
비슷한 유형
[프로그래머스] 완주하지 못한 선수 (해시 Lv. 1) [프로그래머스] 전화번호 목록 (해시 Lv. 2) [프로그래머스] 위장 (해시 Lv. 2) [프로그래머스] 베스트 앨범 (해시 Lv. 3)
로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다.1
순위당첨 내용
1
6개 번호가 모두 일치
2
5개 번호가 일치
3
4개 번호가 일치
4
3개 번호가 일치
5
2개 번호가 일치
6(낙첨)
그 외
로또를 구매한 민우는 당첨 번호 발표일을 학수고대하고 있었습니다. 하지만, 민우의 동생이 로또에 낙서를 하여, 일부 번호를 알아볼 수 없게 되었습니다. 당첨 번호 발표 후, 민우는 자신이 구매했던 로또로 당첨이 가능했던 최고 순위와 최저 순위를 알아보고 싶어 졌습니다. 알아볼 수 없는 번호를0으로 표기하기로 하고, 민우가 구매한 로또 번호 6개가44, 1, 0, 0, 31 25라고 가정해보겠습니다. 당첨 번호 6개가31, 10, 45, 1, 6, 19라면, 당첨 가능한 최고 순위와 최저 순위의 한 예는 아래와 같습니다.
당첨 번호3110451619결과
최고 순위 번호
31
0→10
44
1
0→6
25
4개 번호 일치, 3등
최저 순위 번호
31
0→11
44
1
0→7
25
2개 번호 일치, 5등
순서와 상관없이, 구매한 로또에 당첨 번호와 일치하는 번호가 있으면 맞힌 걸로 인정됩니다.
알아볼 수 없는 두 개의 번호를 각각 10, 6이라고 가정하면 3등에 당첨될 수 있습니다.
3등을 만드는 다른 방법들도 존재합니다. 하지만, 2등 이상으로 만드는 것은 불가능합니다.
알아볼 수 없는 두 개의 번호를 각각 11, 7이라고 가정하면 5등에 당첨될 수 있습니다.
5등을 만드는 다른 방법들도 존재합니다. 하지만, 6등(낙첨)으로 만드는 것은 불가능합니다.
민우가 구매한 로또 번호를 담은 배열 lottos, 당첨 번호를 담은 배열 win_nums가 매개변수로 주어집니다. 이때, 당첨 가능한 최고 순위와 최저 순위를 차례대로 배열에 담아서 return 하도록 solution 함수를 완성해주세요.
Group by & order by & 서브쿼리를활용 (key = group by시가장상위의데이터로그룹핑되므로, order by를사용하면내가원하는데이터를위로올릴수있다)
아이디어
그룹핑 (group by yyyymmdd) + 정렬(order by yyyymm), count(데이터수)
서브쿼리2 : 위의내용을월별로그룹핑
그룹핑 (Group by yyyymm)
그룹함수를이용한결과출력
기존쿼리
select
max(bill_dt.readDt) readDt,
max(bill_dt.oneMonthAgo) oneMonthAgo,
max(bill_dt.twoMonthAgo) twoMonthAgo,
max(bill_dt.threeMonthAgo) threeMonthAgo
from (
select
case
when substring(READ_DT,1,6) = date_format(concat( ${read_dt},'01'), '%Y%m') then READ_DT
end as 'readDt',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -1 Month), '%Y%m') then READ_DT
end as 'oneMonthAgo',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -2 Month), '%Y%m') then READ_DT
end as 'twoMonthAgo',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -3 Month), '%Y%m') then READ_DT
end as 'threeMonthAgo'
from gnd_meter_billing gmb
where 조건 1
and 기간 between 기간1 and 기간2
group by substring(READ_DT,1,6), substring(READ_DT,7,2)
order by cnt_readDt desc
) bill_dt
수정쿼리
select
max(bill_dt.readDt) readDt,
max(bill_dt.oneMonthAgo) oneMonthAgo,
max(bill_dt.twoMonthAgo) twoMonthAgo,
max(bill_dt.threeMonthAgo) threeMonthAgo
from (
select *
from (
select
count(READ_DT) cnt_readDt,
substring(READ_DT,1,6) yyyymm,
case
when substring(READ_DT,1,6) = date_format(concat( ${read_dt},'01'), '%Y%m') then READ_DT
end as 'readDt',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -1 Month), '%Y%m') then READ_DT
end as 'oneMonthAgo',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -2 Month), '%Y%m') then READ_DT
end as 'twoMonthAgo',
case
when substring(READ_DT,1,6) = date_format(date_add(concat(${read_dt},'01'),INTERVAL -3 Month), '%Y%m') then READ_DT
end as 'threeMonthAgo'
from gnd_meter_billing gmb
where 조건 1
and 기간 between 기간1 and 기간2
group by substring(READ_DT,1,8)
order by substring(READ_DT,1,6), cnt_readDt desc
) bill_dt_by_yyyymmdd
group by bill_dt_by_yyyymmdd.yyyymm
) bill_dt
select max(bill_dt.readDt) readDt, max(bill_dt.oneMonthAgo) oneMonthAgo, max(bill_dt.twoMonthAgo) twoMonthAgo, max(bill_dt.threeMonthAgo) threeMonthAgo, max(bill_dt.fourMonthAgo) fourMonthAgo, max(bill_dt.twelveMonthAgo) twelveMonthAgo, max(bill_dt.thirteenMonthAgo) thirteenMonthAgo from ( select case when substring(READ_DT,1,6) = '202203' then READ_DT end as 'readDt', case when substring(READ_DT,1,6) = '202202' then READ_DT end as 'oneMonthAgo', case when substring(READ_DT,1,6) = '202201' then READ_DT end as 'twoMonthAgo', case when substring(READ_DT,1,6) = '202112' then READ_DT end as 'threeMonthAgo', case when substring(READ_DT,1,6) = '202111' then READ_DT end as 'fourMonthAgo', case when substring(READ_DT,1,6) = '202103' then READ_DT end as 'twelveMonthAgo', case when substring(READ_DT,1,6) = '202102' then READ_DT end as 'thirteenMonthAgo' from gnd_meter_billing gmb whereREAD_DT between 20210301000000 and 20220322000000 group by substring(READ_DT,1,6), substring(READ_DT,7,2) ) bill_dt ;