2024_09_23 Kata

SJ.CHO·2024년 9월 23일

알고리즘 Kata

70.

답안 :

public class Solution {
	public int[] solution(String[] wallpaper) {
		int[] answer = new int[4];
		int maxV = Integer.MIN_VALUE;
		int minV = Integer.MAX_VALUE;
		int maxRows = Integer.MIN_VALUE;
		int minRows = Integer.MAX_VALUE;
		for (int i = 0; i < wallpaper.length; i++) {
			if (minV > wallpaper[i].indexOf("#") && wallpaper[i].indexOf("#") != -1) {
				minV = wallpaper[i].indexOf("#");
			}
			if (maxV < wallpaper[i].lastIndexOf("#") && wallpaper[i].indexOf("#") != -1) {
				maxV = wallpaper[i].lastIndexOf("#");
			}
			if (wallpaper[i].contains("#")) {
				if (minRows > i) {
					minRows = i;
				}
				if (maxRows < i) {
					maxRows = i;
				}
			}
		}
		answer[0] = minRows;
		answer[1] = minV;
		answer[2] = maxRows + 1;
		answer[3] = maxV + 1;
		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		String[] wallpaper = { ".##...##.", "#..#.#..#", "#...#...#", ".#.....#.", "..#...#..", "...#.#...", "....#...." };
		int[] arr = s.solution(wallpaper);
		for (int item : arr) {
			System.out.print(item);
		}
	}
}
  • 알고리즘 설명 : 2차원 배열을 가장한 행렬식 문제로 가장 첫,마지막 #이 들어가는 배열 인덱스와 첫,마지막 자리 #의 위치를 구하면 쉽게 풀리는 문제. #의 인덱스 위치와 #을 가지는 마지막 배열원소의 인덱스를 구해서 문제를 풀었다.

lastIndexOf() : 특정문자가 문자열에서 뒤에서부터 있는지검사 후 반환. 오른쪽에서 세지만 Return 값은 왼쪽부터의 순서를 리턴
참조 : https://mine-it-record.tistory.com/124

SQL Kata

75.

답안 :

select a.HISTORY_ID ,round(daily_fee * (datediff(end_date, start_date) + 1) * (100 - ifnull(discount_rate, 0)) /100, 0) fee
from CAR_RENTAL_COMPANY_CAR c1
join (SELECT *, 
case when DATEDIFF(end_date,start_date)+1 <7 then NULL
when DATEDIFF(end_date,start_date)+1 <30 then '7일 이상'
when DATEDIFF(end_date,start_date)+1<90 then '30일 이상'
else '90일 이상'
end as 'RENTDAY'
from CAR_RENTAL_COMPANY_RENTAL_HISTORY) a
on c1.CAR_ID = a.CAR_ID
left join CAR_RENTAL_COMPANY_DISCOUNT_PLAN c2
on a.RENTDAY = c2.DURATION_TYPE
AND c1.CAR_TYPE = c2.CAR_TYPE
where c1.CAR_TYPE ='트럭'
order by 2 desc, 1 desc
  • DATEDIFF() 함수를 이용해 대여기간을 구하여 CAR_RENTAL_COMPANY_DISCOUNT_PLAN 테이블 과 JOIN 하기 위해 대여기간을 일반화. 이후 조건식과 비용 계산식 round(daily_fee * (datediff(end_date, start_date) + 1) * (100 - ifnull(discount_rate, 0)) /100, 0) 를 이용해 값을 출력.

틀린답안 :

select HISTORY_ID,
case when RentDay = 0 then DAILY_FEE
when RentDay between 1 and 6 then RentDay*DAILY_FEE
when RentDay between 7 and 29 then Round((RentDay*(DAILY_FEE*0.95)),0)
when RentDay between 30 and 89 then Round((RentDay*(DAILY_FEE*0.93)),0)
else Round(RentDay*(DAILY_FEE*0.90),0) end as 'FEE'
from
(SELECT c1.CAR_ID,c1.CAR_TYPE,c1.DAILY_FEE,c2.HISTORY_ID, DATEDIFF(c2.end_date,c2.start_date) as 'RentDay'
from CAR_RENTAL_COMPANY_CAR c1
inner join CAR_RENTAL_COMPANY_RENTAL_HISTORY c2
on c1.CAR_ID = c2.CAR_ID
AND c1.CAR_TYPE = '트럭') a
order by FEE desc, HISTORY_ID desc
  • 유효데이터만 서브쿼리로 뽑아내어 그대로 계산하여 출력하려 했으나 계산식도 오류가 있었고 CAR_RENTAL_COMPANY_DISCOUNT_PLAN 테이블도 사용하지않고 개발자만 알고있단 전제로 쿼리문을 작성했기에 실패.
profile
70살까지 개발하고싶은 개발자

0개의 댓글