2024_09_25 Kata

SJ.CHO·2024년 9월 25일

알고리즘 Kata

72.

답안 :

package answer;

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

public class Solution {
	public String[] solution(String[] players, String[] callings) {
		Map<String, Integer> map = new HashMap<>();
		// HashMap을 이용한 선수이름 및 초기순위 MapPing
		for (int i = 0; i < players.length; i++) {
			map.put(players[i], i);
		}
		// 호출되는 선수 이름만큼 순위 변동
		for (int i = 0; i < callings.length; i++) {
			// 호출된 선수 순위 확인
			int callingPidx = map.get(callings[i]);
			// 호출된 선수와 앞선수의 순위교환
			String swap = players[callingPidx - 1];
			players[callingPidx - 1] = players[callingPidx];
			players[callingPidx] = swap;
			// map 순위 최신화
			map.put(callings[i], map.get(callings[i]) - 1);
			map.put(swap, map.get(swap) + 1);
		}
		return players;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		String[] players = { "mumu", "soe", "poe", "kai", "mine" };
		String[] callings = { "kai", "kai", "mine", "mine" };
		s.solution(players, callings);
	}
}
  • 알고리즘 설명 :
    • 선수들의 이름 및 경기 초반 순위를 <String,Integer> 형채로 맵핑
    • 호출된 callings 배열의 선수이름 기반으로 Map 의 순위를 가져온 후 players 배열 및 Map의 순위 변경 로직 수행.
    • players 배열 리턴.

틀린답안 :

import java.util.Arrays;

public class Solution {
	public String[] solution(String[] players, String[] callings) {
		for (int i = 0; i < callings.length; i++) {
			int callingPidx = Arrays.asList(players).indexOf(callings[i]);
			String swap = players[callingPidx - 1];
			players[callingPidx - 1] = players[callingPidx];
			players[callingPidx] = swap;
		}
		String[] answer = players;
		return answer;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution s = new Solution();
		String[] players = { "mumu", "soe", "poe", "kai", "mine" };
		String[] callings = { "kai", "kai", "mine", "mine" };
		s.solution(players, callings);
	}
}
  • 버블소트 형식으로 불린 플레이어를 앞으로 전진시키는 형태.
  • 배열의 있는 선수의 이름을 얻기위해 Arrays.asList(players).indexOf(callings[i]); 사용 하였는데 List로 변환하면서 시간부담이 꽤 큰모양.

SQL Kata

78.

  • referee_id 가 2가 아닌 고객을 찾는 문제.

답안 :

SELECT name
from Customer
where referee_id !=2 || referee_id is null
  • 2가 아니거나 null값을 고객의 이름을 불러온다.

79.

  • 면적이 3000000^2 m 이상이거나 인구수가 25000000명 이상인 나라 찾기

답안 :

SELECT name,area,population
from World
where area >=3000000 || population >=25000000

80.

  • 자신의 기사를 하나 이상 본 모든 저자를 찾기

답안 :

select DISTINCT viewer_id as 'id'
from Views
where author_id =viewer_id 
order by 1
profile
70살까지 개발하고싶은 개발자

0개의 댓글