728x90

주어진 today 날짜 기준으로 개인정보 수집 유효기간을 초과하여 파기하여야하는 개인정보를 반환 

public ArrayList<Integer> solution(String today, String[] terms, String[] privacies) {

ArrayList<Integer> answer = new ArrayList<Integer>();

for (int i = 0; i < privacies.length; i++) {

String yearStr = privacies[i].substring(0, 4);

String monthStr = privacies[i].substring(5, 7);

String dayStr = privacies[i].substring(8, 10);

int year = Integer.parseInt(yearStr);

int month = Integer.parseInt(monthStr);

int day = Integer.parseInt(dayStr);

String termStr = privacies[i].substring(11);

int term = 0;

for (int j = 0; j < terms.length; j++) {

if (terms[j].startsWith(termStr)) {

term = Integer.parseInt(terms[j].substring(2));

}

}

month = month + term;

if (month > 12) {

year++;

month -= 12;

}

day--;

if (day == 0) {

month--;

if (month == 0) {

year--;

month = 12;

day = 31;

} else if (month == 1) {

day = 31;

} else if (month == 2) {

if (isLuna(year)) {

day = 29;

} else {

day = 28;

}

} else if (month == 3) {

day = 31;

 

} else if (month == 4) {

day = 30;

 

} else if (month == 5) {

day = 31;

 

} else if (month == 6) {

day = 30;

 

} else if (month == 7) {

day = 31;

 

} else if (month == 8) {

day = 31;

 

} else if (month == 9) {

day = 30;

 

} else if (month == 10) {

day = 31;

 

} else if (month == 11) {

day = 30;

 

}

 

}

Date pd = new Date();

String pDate= year+"."+month+"."+day;

try {

pd=new SimpleDateFormat("yyyy.MM.dd").parse(pDate);

Date t = new SimpleDateFormat("yyyy.MM.dd").parse(today);

if(pd.compareTo(t)<0)

answer.add(i+1);

} catch (ParseException e) {

e.printStackTrace();

}

}

return answer;

}

Date 객체로 날짜들을 변환하여 비교하였다. 

 

월별로 일수가 다르기 때문에 월별로 조건문을 이용해 일수를 다르게 지정해주는 바람에 코드가 길어졌다. 

 

하지만 날짜의 대소를 비교할때는 굳이 날짜 형식을 지켜주지 않아도 일수가 많은지 적은지만 비교하면 된다. 

 

public ArrayList<Integer> solution2(String today, String[] terms, String[] privacies) {

ArrayList<Integer> answer = new ArrayList<Integer>();

int tday =Integer.parseInt(today.substring(0,4))*12*28+Integer.parseInt(today.substring(5,7))*28+Integer.parseInt(today.substring(8,10));

 

for (int i = 0; i < privacies.length; i++) {

String termStr = privacies[i].substring(11);

int term = 0;

for (int j = 0; j < terms.length; j++) {

if (terms[j].startsWith(termStr)) {

term = Integer.parseInt(terms[j].substring(2));

}

}

String yearStr = privacies[i].substring(0, 4);

String monthStr = privacies[i].substring(5, 7);

String dayStr = privacies[i].substring(8, 10);

int year = Integer.parseInt(yearStr);

int month = Integer.parseInt(monthStr);

int day = Integer.parseInt(dayStr);

month = month + term;

int n = day+month*28+year*28*12;

if(n<=tday)

answer.add(i+1);

}

return answer;

}

이렇게 계산하니까 시간도 엄청 단축되었다.

'Java > Coding Test' 카테고리의 다른 글

대충 만든 자판 문제 풀이  (0) 2023.10.01
모의고사 문제 풀이  (0) 2023.09.30
숫자 짝궁 문제 풀이  (1) 2023.09.28
옹알이2 문제 풀이  (0) 2023.09.28
과일 장수 문제 풀이  (0) 2023.09.28

+ Recent posts