2024_09_26 Kata

SJ.CHO·2024년 9월 26일

알고리즘 Kata

73.

답안 :

package answer;

import java.util.HashMap;
import java.util.Map;

class Solution {
	public int[] solution(String[] park, String[] routes) {
		/*
		 * 0. 명령맵핑 1. 현재위치파악 2. 이동명령확인 3. 명령파악(최대사이즈인가? 장애물이있는가?) 4. 이동 명령시행 or 무시
		 * 5. 현재위치 리턴 및 종료
		 */
		int[] n = { -1, 0 };
		int[] s = { 1, 0 };
		int[] e = { 0, 1 };
		int[] w = { 0, -1 };
		int location[] = new int[2];
		int nextLocation[] = new int[2];
		// 방위별 이동거리 맵핑
		Map<Character, int[]> order = new HashMap<>() {
			{
				put('N', n);
				put('S', s);
				put('E', e);
				put('W', w);
			}
		};
		// 시작지점 찾기
		for (int i = 0; i < routes.length; i++) {
			if (park[i].contains("S")) {
				location[0] = i;
				location[1] = park[i].indexOf("S");
				break;
			}
		}
		for (int i = 0; i < routes.length; i++) {
			nextLocation[0] = location[0];
			nextLocation[1] = location[1];
			// 이동 방향과 거리 찾아옴
			int[] ordersLocation = order.get(routes[i].charAt(0));
			char military = routes[i].charAt(0);
			// 이동 방향 별 거리 이동 및 검증
			switch (military) {
			case 'N':
				for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
					// 공원밖으로 나가거나 자신의 이동 방향에 X 표시일경우 무시
					if (nextLocation[0] - 1 < 0 || park[nextLocation[0] - 1].charAt(nextLocation[1]) == 'X') {
						nextLocation[0] = location[0];
						break;
					} else {
						nextLocation[0] += ordersLocation[0];
					}
				}
				break;
			case 'S':
				for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
					if (park.length <= nextLocation[0] + 1
							|| park[nextLocation[0] + 1].charAt(nextLocation[1]) == 'X') {
						nextLocation[0] = location[0];
						break;
					} else {
						nextLocation[0] += ordersLocation[0];
					}
				}
				break;
			case 'E':
				for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
					if (park[nextLocation[0]].length() <= nextLocation[1] + 1
							|| park[nextLocation[0]].charAt(nextLocation[1] + 1) == 'X') {
						nextLocation[1] = location[1];
						break;
					} else {
						nextLocation[1] += ordersLocation[1];
					}
				}
				break;
			case 'W':
				for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
					if (nextLocation[1] - 1 < 0 || park[nextLocation[0]].charAt(nextLocation[1] - 1) == 'X') {
						nextLocation[1] = location[1];
						break;
					} else {
						nextLocation[1] += ordersLocation[1];
					}
				}
				break;
			}
			location[0] = nextLocation[0];
			location[1] = nextLocation[1];
		}
		return location;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		String[] park = { "SOO", "OOO", "XOO" };
		String[] routes = { "S 1" };
		int[] a = s.solution(park, routes);
		for (int item : a) {
			System.out.println(item);
		}
	}
}
  • 알고리즘 설명 :
    • Map 을 이용해 방위별로 이동거리를 맵핑.
    • 시작지점이 Y : 0 일거라는 보장이 없으니 시작지점 확인 후 저장
    • routes[] 배열을 순회하며 맵핑된 이동거리와 방향을 가져옴
    • switch case 문을 사용하여 방위별 이동 및 조건을 검사.
    • 최종 위치 반환.

틀린답안 :

			for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
				if (nextLocation[0] <= -1 || nextLocation[0] <= -1) {
					break;
			}
				if (park[nextLocation[0]].length() <= nextLocation[1] + 1
					|| park[nextLocation[0]].charAt(nextLocation[1] + 1) == 'X') {
					break;
				} else if (park[nextLocation[0]].length() <= nextLocation[0] + 1
						|| park[nextLocation[0] + 1].charAt(nextLocation[0]) == 'X') {
					break;
				} else {
					nextLocation[0] = nextLocation[0] + ordersLocation[0];
					nextLocation[1] = nextLocation[1] + ordersLocation[1];
				}
				location[0] = nextLocation[0];
				location[1] = nextLocation[1];
			}
  • 시작지점을 y:0 지점이라고 생각하여 코딩하였고, if문을 통해 분기 나누기에 실패함. X축이동엔 문제가 없는데 Y축 이동에 문제가 있다고 판단하여 X축이동 실패 등

SQL Kata

81.

  • Content 컬럼의 길이가 15자 초과 인 id 출력문제

답안 :

select tweet_id
from Tweets
where CHAR_LENGTH(content)>15

CHAR_LENGTH(): 해당 문자열의 길이를 리턴한다.

82.

  • EmployeeUNI 테이블과 Employees 테이블중에서 unique_id 와 name을 출력
    id 가 없을경우 NULL 출력

답안 :

select e2.unique_id, e1.name
from Employees e1
left join EmployeeUNI e2
on e1.id = e2.id
  • left join 사용시 왼쪽테이블의 내용은 전부 가져오기에 자동으로 null 로 치환된다.

83.

  • Sales 테이블과 Product 테이블에서 판매된 상품의 이름,년도,가격 출력

답안 :

select p1.product_name, s1.year, s1.price
from Sales s1
inner join Product p1
on s1.product_id = p1.product_id
profile
70살까지 개발하고싶은 개발자

0개의 댓글