2024_09_10 Kata

SJ.CHO·2024년 9월 10일

알고리즘 Kata

65.

답안 :

public class Solution {

	public int solution(String s) {
		int answer = 0;
		while (s.length() != 0) {
			answer++;
			char x = s.charAt(0);
			int xcnt = 1;
			int ycnt = 0;
			for (int i = 1; i < s.length(); i++) {
				if (s.charAt(i) == x) {
					xcnt++;
				} else if (s.charAt(i) != x) {
					ycnt++;
				}
				if (xcnt == ycnt) {
					break;
				}
			}
			s = s.substring(xcnt + ycnt);
		}

		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		String s1 = "abracadabra";
		System.out.println(s.solution(s1));
	}
}
  • 알고리즘 설명 : 입력된 문자의 첫번째 문자와 동일문자, 틀린문자를 문자열을 순회하며 카운트 한뒤 두 카운트의 합이 같아지면 for문을 나와서 문자열을 자르고 문자열의 길이가 0이될때까지 다시 반복한다.

  • 오랜만에 나온 쉬운 문제로 처음엔 for문 하나로 제어하려했지만 문자열의 대한 변동이 필요하다 생각해 2중 반복문으로 전환 하였음.

SQL Kata

69.

답안 :

SELECT Month(START_DATE)as 'MONTH',car_id as 'CAR_ID',count(*) as 'RECORDS'
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where car_id in(
select car_id
from CAR_RENTAL_COMPANY_RENTAL_HISTORY where date_format(START_DATE,'%Y-%m') between '2022-08' and '2022-10'
group by 1 having count(*)>=5)
and date_format(START_DATE,'%Y-%m') between '2022-08' and '2022-10'
group by 1,2
order by 1,2 desc
  • 2022-08~2022-10 사이의 데이터를 그룹화하여 '총합' 이 5이상인 데이터들을 다시 검증하고 그룹화하여 풀이.
    생각보다 많이 어려웠다.

틀린답안 :

SELECT Month(START_DATE)as 'MONTH',car_id as 'CAR_ID',count(car_id) as 'RECORDS'
from CAR_RENTAL_COMPANY_RENTAL_HISTORY
where date_format(START_DATE,'%Y-%m') between '2022-08' and '2022-10'
group by Month(START_DATE),car_id
Having count(car_id)>=5
order by 1,2 desc
  • 서브쿼리를 적용을 안하고했기에 RECORDS가 쪼개지지않고 총합으로만 출력. RECORDS가 5이상의 데이터들만 출력이되서 실패
profile
70살까지 개발하고싶은 개발자

0개의 댓글