230725 달리기 경주

Jongleee·2023년 7월 25일
0

TIL

목록 보기
320/737
public String[] solution(String[] players, String[] callings) {
	HashMap<String, Integer> rank = new HashMap<>();

	for (int i = 0; i < players.length; i++) {
		rank.put(players[i], i);
	}

	for (String calling : callings) {
		int currentRank = rank.get(calling);
		String player = players[currentRank];

		if (currentRank > 0) {
			String frontPlayer = players[currentRank - 1];

			rank.put(player, currentRank - 1);
			rank.put(frontPlayer, currentRank);

			players[currentRank - 1] = player;
			players[currentRank] = frontPlayer;
		}
	}

	return players;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/178871

0개의 댓글