[문제]
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
[내 풀이]
import java.util.*;
import java.text.*;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
List<Integer> arr = new ArrayList<Integer>();
HashMap<String,Integer> kind = new HashMap<>();
for(String i : terms){ //종류별 유효기간
String[] a = i.split("\\s");
kind.put(a[0],Integer.parseInt(a[1]));
}
for(int i=0; i<privacies.length;i++){
String[] p = privacies[i].split("\\s"); //스페이스바를 기준으로 가입 날짜와 종류를 분리
if(cmpD(today, p[0] , kind.get(p[1]))) arr.add(i+1);
else continue;
}
return arr.stream().mapToInt(i -> i).toArray();
}
private static boolean cmpD(String today, String date , int month){
int[] today1 = new int[3];
int[] date1 = new int[3];
boolean result = false;
today1[0] = Integer.parseInt(today.substring(0,4)); //현재날짜 분리
today1[1] = Integer.parseInt(today.substring(5,7));
today1[2] = Integer.parseInt(today.substring(8,10));
date1[0] = Integer.parseInt(date.substring(0,4)); //대상날짜 분리
date1[1] = Integer.parseInt(date.substring(5,7));
date1[2] = Integer.parseInt(date.substring(8,10));
int a = today1[0]*12*28+today1[1]*28+today1[2];
if(date1[1]+month>12){
date1[0]++;
date1[1] = date1[1]+month-12;
}
else date1[1]+=month;
int b = date1[0]*12*28+date1[1]*28+date1[2];
if(a>=b) result = true;
return result;
}
}
[풀이 과정]
우선 첫번째 반복문을 통해서 종류별 유효기간 Map을 만들어주고 두번째 반복문을 돌면서 가입일자와 가입종류를 분류해준뒤 cmpD()에 넣어서 해당종류별 기간이 넘었는지 넘지 않았는지를 비교해준다. 이게 코드의 끝이다.
다만 내가 썼지만 정말 더러운 코드다. 문제를 푸는데 시간이 좀 걸려서 그런지 집중력이 흐트러진 탓이라고 생각한다. cmpD에서 불필요한 코드가 많이 보이고 날짜비교도 주먹구구식이다.. 시간나면 코드를 좀더 다듬어야겠다..
'문제풀이 > 프로그래머스' 카테고리의 다른 글
[JAVA] 귤 고르기 (0) | 2023.02.23 |
---|---|
[JAVA] 둘만의 암호 (1) | 2023.02.21 |
[JAVA] [1차] 뉴스 클러스터링 (0) | 2023.02.21 |
[JAVA] 프린터 (0) | 2023.02.21 |
[JAVA] 카드 뭉치 (0) | 2023.02.20 |