Java & Spring/코딩테스트

22일차 - 알고리즘 코드카타

DJ.Kang 2024. 8. 13. 21:27

- 회고

72. 달리기 경주 : https://school.programmers.co.kr/learn/courses/30/lessons/178871

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

- 풀이 과정

import java.util.*;
class Solution {
    static Map<String, Integer> rankMap = new HashMap<>();
    public String[] solution(String[] players, String[] callings) {
        for(int i = 0; i < players.length; i++){
            rankMap.put(players[i], i);
        }

        for (String c : callings) {
            int i = rankMap.get(c);
            swap(players, i);
        }
        return players;
    }

    public static void swap(String[] players, int idx){
        String tmp = players[idx - 1];
        players[idx - 1] = players[idx];
        players[idx] = tmp;

        rankMap.put(players[idx], rankMap.get(players[idx]) + 1);
        rankMap.put(players[idx - 1], rankMap.get(players[idx - 1]) - 1);
    }
}
  1. 현재 등수를 맵으로 저장
  2. 콜을 순회하며 콜이 됐을 때 위치변경하는 메서드 선언
  3. 기존 players배열과 인덱스를 매개변수로 받아
  4. 우선 등수를 변경해준다
  5. 그 다음 맵에 저장된 벨류값(등수)도 변경해준다

- 진행

일자 완료 번호
24.07.16 ~ 24.07.31 1~63
24.08.01 64
24.08.02 65
24.08.05 66
24.08.06 67
24.08.07 68
24.08.08 69
24.08.09 70
24.08.12 71
24.08.13 72