Day85

강태훈·2026년 5월 1일

nbcamp TIL

목록 보기
85/97

알고리즘 코드카타

Product Sales Analysis III

select a.product_id, a.year as first_year, a.quantity, a.price
from Sales a
left join (
    select *, ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY year) AS rn
    from Sales
) as b
on a.product_id = b.product_id
and a.year = b.year
where b.rn = 1
order by a.sale_id asc
;

달리기 경주

import java.util.Arrays;

class Solution {
    public String[] solution(String[] players, String[] callings) {
        String[] answer = players;

        for (String calling : callings){
            int index = Arrays.asList(answer).indexOf(calling);

            if(index == -1 || index == 0){
                continue;
            }

            String temp = answer[index-1];

            answer[index] = temp;
            answer[index-1] = calling;
        }

        return answer;
    }
}
  • 런타임 에러가 발생해서 아래로 수정
import java.util.HashMap;
import java.util.Map;

class Solution {
    public String[] solution(String[] players, String[] callings) {
        String[] answer = players;

        Map<String, Integer> ranking = new HashMap<>();
        
        for (int i = 0; i < players.length; i++) {
            ranking.put(players[i], i);
        }

        for (String calling : callings) {
            int index = ranking.get(calling);
            String player = answer[index-1];
            
            answer[index-1] = calling;
            answer[index] = player;
            
            ranking.put(calling, index-1);
            ranking.put(player, index);
        }

        return answer;
    }
}

0개의 댓글