프로그래머스_달리기 경주

LeeYulhee·2023년 8월 24일
0

💻 문제 출처 : 프로그래머스_달리기 경주

👉 내가 작성한 답


import java.util.*;

class Solution {
    public String[] solution(String[] players, String[] callings) {
        
        // key가 이름인 Map과 등수인 Map 생성하고 값 대입
        Map<String, Integer> nameKeyMap = new HashMap<>();
        Map<Integer, String> rankKeyMap = new HashMap<>();
        
        for(int i = 0; i < players.length; i++) {
            nameKeyMap.put(players[i], i);
            rankKeyMap.put(i, players[i]);
        }
        
        // callings에 나오면 순서 변경
        for(String nowCalling : callings) {
            int beforeRanking = nameKeyMap.get(nowCalling);
            String beforePlayer = rankKeyMap.get(beforeRanking - 1);
            
            nameKeyMap.put(nowCalling, beforeRanking - 1);
            nameKeyMap.put(beforePlayer, beforeRanking);
            
            rankKeyMap.put(beforeRanking - 1, nowCalling);
            rankKeyMap.put(beforeRanking, beforePlayer);
        }
        
        // answer 배열 생성하고 Map의 값들 넣기
        String[] answer = new String[players.length];
        
        for(int i = 0; i < players.length; i++) {
            answer[i] = rankKeyMap.get(i);
        }
            
        return answer;
    }
}

📌 문제 풀이 설명

  • String을 key로 Integer를 value로 받는 Map과 Integer를 key로, String을 value로 받는 Map을 생성
  • for문으로 0부터 player의 길이 미만까지 1씩 증가하며 순회
    • nameKeyMap에 player의 이름을 key로, i를 value로 넣음
    • rankKeyMap에 i를 key로 player의 이름을 value로 넣음
  • 향상된 for문으로 callings의 요소를 nowCalling으로 받아서 순회
    • int 변수 beforeRanking에 nameKeyMap에서 nowCalling에 해당하는 value를 대입
    • String 변수 beforePlayer에 rankKeyMap에서 beforeRanking - 1로 받아온 value를 대입
      • nowCalling보다 앞에 있던 사람의 이름
    • nameKeyMap에 nowCalling을 key로, beforeRanking - 1을 value로 넣음
      • 해당 선수의 등수 변경
    • nameKeyMap에 beforePlayer을 key로, beforeRanking을 value로 넣음
      • 앞에 있던 선수를 뒤로 바꿔줌
    • rankKeyMap에 beforeRanking - 1을 key로, nowCalling을 value로 넣음
      • 해당 등수의 선수 변경
    • rankKeyMap에 beforeRanking을 key로, beforePlayer를 value로 넣음
  • for문 종료 후 String 배열인 answer를 players의 길이로 생성
  • for문으로 0부터 players의 길이 미만까지 1씩 증가하며 순회
    • answer[i]에 rankKeyMap에서 i에 해당하는 value를 대입
  • for문 종료 후 return answer



👉 다른 사람이 작성한 답


import java.util.HashMap;

public class Solution {
    public String[] solution(String[] players, String[] callings) {
        int n = players.length;
        HashMap<String, Integer> indexMap = new HashMap<>();

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

        for (String calling : callings) {
            int idx = indexMap.get(calling);
            if (idx > 0) {
                String temp = players[idx - 1];
                players[idx - 1] = players[idx];
                players[idx] = temp;

                indexMap.put(players[idx - 1], idx - 1);
                indexMap.put(players[idx], idx);
            }
        }

        return players;
    }
}

📌 문제 풀이 설명

  • int 변수 n에 players의 길이를 대입
  • String을 key로, Integer를 value로 받는 Map을 생성
  • for문으로 0부터 n 미만까지 1씩 증가하며 순회
    • Map에 players[i]를 key로, i를 value로 넣음
  • 향상된 for문으로 callings를 순회
    • int 변수 idx에 Map에서 callings의 요소에 해당하는 value 값을 대입
    • 만약 idx가 0보다 크다면
      • String temp에 players의 [idx - 1]번째 이름을 대입
      • players[idx - 1]에 players[idx]를 대입
      • players[idx]에 temp 대입
      • Map에 players[idx - 1]를 key로 idx - 1을 value로 넣음
      • Map에 players[idx]를 key로 idx를 value로 넣음
  • for문 종료 후 return players
profile
공부 중인 신입 백엔드 개발자입니다

0개의 댓글