답안 :
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);
}
}

틀린답안 :
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로 변환하면서 시간부담이 꽤 큰모양.
- referee_id 가 2가 아닌 고객을 찾는 문제.
답안 :
SELECT name
from Customer
where referee_id !=2 || referee_id is null
- 면적이 3000000^2 m 이상이거나 인구수가 25000000명 이상인 나라 찾기
답안 :
SELECT name,area,population
from World
where area >=3000000 || population >=25000000
- 자신의 기사를 하나 이상 본 모든 저자를 찾기
답안 :
select DISTINCT viewer_id as 'id'
from Views
where author_id =viewer_id
order by 1