2024_09_02 Kata

SJ.CHO·2024년 9월 2일

알고리즘 Kata

59


답안 :

import java.util.Arrays;

public class Solution {

	public int solution(int n, int m, int[] section) {
		int answer = 0;
		boolean[] wall = new boolean[n];
		Arrays.fill(wall, true);
		//칠해야할 벽면 idx false 화.
		for (int i = 0; i < section.length; i++) {
			wall[section[i] - 1] = false;
		}
		for (int i = 0; i < n; i++) {
			// 벽면을 칠해야할때
			if (!wall[i]) {
				//m만큼 페인트칠
				for (int j = 0; j < m; j++) {
					//최댓값 검사(OutOfBound 예방)
					if (i + j < wall.length) {
						wall[i + j] = true;
					}
				}
				//칠한만큼 인덱스이동
				i = i + m - 1;
				answer++;
			}
		}
		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		int n = 8;
		int m = 4;
		int[] section = { 2, 3, 6 };
		System.out.println(s.solution(n, m, section));
	}
}
  • 알고리즘 설명 : 벽과 같은 크기의 bool 배열을 선언하고 칠해야할 idx를 false로 표현 후, 칠해야할 벽면을 만났을시 최댓값을 검사하고 이후에 M(롤러의 크기)만큼 반복하여 true값으로 전환 이후 중복값을 피하기위해 idx값을 m만큼 이동시켜서 재검사.
package answer;

import java.util.ArrayList;

public class Solution {

	public int solution(int n, int m, int[] section) {
		int answer = 0;
		int sumwall = 0;
		ArrayList<Integer> arr1 = new ArrayList<>();
		for (int i = 0; i < section.length; i++) {
			sumwall += section[i];
		}
		for (int i = 1; i <= n; i++) {
			arr1.add(i);
		}
		for (int i = 0; i < sumwall / m; i++) {
			for (int j = 0; j < section.length; j++) {
				if (arr1.contains(section[j]) && arr1.size() > m) {
					if (arr1.get(arr1.size() - 1) < section[j] - 1 + m) {
						arr1.subList(section[j] - 1, arr1.size() - 1).clear();
						answer++;
					}
					arr1.subList(section[j] - 1, section[j] - 1 + m).clear();
					answer++;
				} else if (arr1.contains(section[j]) && arr1.size() <= m) {
					arr1.clear();
					answer++;
				}
			}
		}
		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		int n = 4;
		int m = 1;
		int[] section = { 1,2,3,4 };
		System.out.println(s.solution(n, m, section));
	}
}
  • 해당답안은 처음에 생각한대로 구현은 했는데 결과적으론 틀린답안.
  • 처음생각은 LIST로 칠해야할 벽면을 옮긴후 idx만큼 클리어해가며 지운뒤 최대값이 넘고, 벽면이없으면 종료시켰는데 LIST의 특성상 idx가 지워질시 해당 idx만큼 당겨와진다는 특성을 고려하지 못해 원하는 결과가 나오지않는다.

참조 :
https://hianna.tistory.com/556
https://wtg-study.tistory.com/25

SQL Kata

59

  • 답안 :
select car_id,
case when MAX('2022-10-16' Between Start_date and End_date) then '대여중'
else '대여 가능' end as 'AVAILABILITY'
from CAR_RENTAL_COMPANY_RENTAL_HISTORY 
group by car_id
order by car_id desc
  • MAX()를 사용하는 이유는 group by 를 사용하게되면 DB엔진에따라 무슨값이 오게됄지 모르기 때문에 MAX를 이용해서 최신값중 날짜값을 검산하여 조건문을 받아와 처리한다.
SELECT 
    CAR_ID,
    CASE 
        WHEN EXISTS (
            SELECT 1
            FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY AS sub
            WHERE sub.CAR_ID = main.CAR_ID 
              AND '2022-10-16' BETWEEN sub.START_DATE AND sub.END_DATE
        ) THEN '대여중'
        ELSE '대여 가능'
    END AS AVAILABILITY
FROM 
    CAR_RENTAL_COMPANY_RENTAL_HISTORY AS main
GROUP BY 
    CAR_ID
ORDER BY 
    CAR_ID DESC;
  • 튜터님이 말씀해주신 더 직관적인(문제에 대한 협업에 관련한다면) 답안.
profile
70살까지 개발하고싶은 개발자

0개의 댓글