목차
▸ 바탕화면 정리
▸ 개인정보 수집 유효기간
출처: 프로그래머스 코딩테스트 연습 > 연습문제 > 바탕화면 정리
class Solution {
public int[] solution(String[] wallpaper) {
int a = wallpaper.length;
int b = wallpaper[0].length();
char [][] array = new char[a][b];
int cnt = 0;
//2차원 배열 세팅
for(int i = 0; i < a; i++){
String s = wallpaper[i];
for(int j = 0; j < b; j++){
array[i][j] = s.charAt(j);
if(array[i][j] == '#'){
cnt++;
}
}
}
//'#'인덱스 좌표 저장
int[] xpoints = new int[cnt];
int[] ypoints = new int[cnt];
int zero = 0;
for(int i = 0; i < a; i++){
for(int j = 0; j < b;j++){
if(array[i][j] == '#'){
xpoints[zero] = i;
ypoints[zero] = j;
zero++;
}
}
}
//x 좌표 중 최대, 최소 구하기
int xmin = xpoints[0];
int xmax = xpoints[0];
for(int i = 0; i < xpoints.length; i++){
if(xpoints[i] > xmax){
xmax = xpoints[i];
} else if(xpoints[i] < xmin){
xmin = xpoints[i];
}
}
//y 좌표 중 최대, 최소 구하기
int ymin = ypoints[0];
int ymax = ypoints[0];
for(int i = 0; i < ypoints.length; i++){
if(ypoints[i] > ymax){
ymax = ypoints[i];
} else if(ypoints[i] < ymin){
ymin = ypoints[i];
}
}
//정답 배열 만들기
int[] answer = new int[4];
answer[0] = xmin;
answer[1] = ymin;
answer[2] = xmax + 1;
answer[3] = ymax + 1;
return answer;
}
}
출처: 프로그래머스 코딩테스트 연습 > 2023 KAKAO BLIND RECRUITMENT > 개인정보 수집 유효기간
[오답 코드]
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies)throws ParseException {
Map<String, Integer> map = new HashMap<>();
List<String> list = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
Date date1 = sdf.parse(today);
for(String term : terms){
String[] termsArray = term.split(" ");
map.put(termsArray[0], Integer.valueOf(termsArray[1]));
}
for(String s : privacies){
String[] privacy = s.split(" ");
String[] join = privacy[0].split("\\.");
for(String key : map.keySet()){
if(privacy[1].equals(key)){
int month = map.get(key);
int tempMonth = Integer.valueOf(join[1]) + month;
if(tempMonth > 12){
int tempYear = Integer.valueOf(join[0]);
tempYear++;
join[0] = String.valueOf(tempYear);
}else{
join[1] = String.valueOf(tempMonth);
}
}
}
String date = String.join(".", join);
Date date2 = sdf.parse(date);
int comparisonResult = date1.compareTo(date2);
if(comparisonResult < 0){
list.add("o");
}else{
list.add("x");
}
}
List<Integer> answerList = new ArrayList<>();
for(int i = 0; i < list.size(); i++){
if(list.get(i).equals("x")){
answerList.add(i+1);
}
}
int[] answer = new int[answerList.size()];
for(int i = 0; i < answer.length; i++){
answer[i] = answerList.get(i);
}
return answer;
}
}
[수정 코드]
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies){
Map<String, Integer> map = new HashMap<>();
List<String> list = new ArrayList<>();
for(String term : terms){
String[] termsArray = term.split(" ");
map.put(termsArray[0], Integer.valueOf(termsArray[1]));
}
String[] todaydate = today.split("\\.");
int todaytotal = Integer.parseInt(todaydate[0])*12*28 + Integer.parseInt(todaydate[1])*28 + Integer.parseInt(todaydate[2]);
for(String s : privacies){
String[] privacy = s.split(" ");
String[] join = privacy[0].split("\\.");
int daytotal = Integer.parseInt(join[0])*12*28 + (Integer.parseInt(join[1])+map.get(privacy[1]))*28 + Integer.parseInt(join[2]);
if(todaytotal-daytotal >= 0){
list.add("x");
}else{
list.add("o");
}
}
List<Integer> answerList = new ArrayList<>();
for(int i = 0; i < list.size(); i++){
if(list.get(i).equals("x")){
answerList.add(i+1);
}
}
int[] answer = new int[answerList.size()];
for(int i = 0; i < answer.length; i++){
answer[i] = answerList.get(i);
}
return answer;
}
}