20220205_알고리즘

Minseok-Choi·2022년 2월 5일
0

알고리즘

목록 보기
13/13
post-thumbnail

프로그래머스_오픈채팅방

  • 아이디와 닉네임을 가지는 User클래스를 만들어서 처리하고 싶었으나, 테스트케이스 몇개에서 시간초과가 났다.
  • 닉네임을 바꾸는 경우(Change 커맨드 또는 나갔다가 새로운 닉네임으로 접속)에서 접속을 했었던 유저인지, 확인하고 닉네임을 바꾸는 과정이 시간이 많이 소요되었다.
  • 다시 단순하게 돌아가서, Map으로 처리하니 통과를 할 수 있었다.
import java.util.*;
class Solution {
    public String[] solution(String[] record) {
        Map<String, String> userMap = new HashMap<>();
	
        for (String s : record) { // 아이디와 닉네임을 담는 과정
            String[] splited = s.split(" ");
            if(splited.length == 3) {
                userMap.put(splited[1],splited[2]);
            }
        }
        
        List<String> answer = new ArrayList<>();

        for (String s : record) { // 메시지를 담는 과정
            String[] splited = s.split(" ");
            if(splited[0].equals("Enter")) {
                answer.add(userMap.get(splited[1])+"님이 들어왔습니다.");
            }
            if(splited[0].equals("Leave")) {
                answer.add(userMap.get(splited[1])+"님이 나갔습니다.");
            }
        }
        
        return answer.toArray(String[]::new);
    }
}
profile
차곡차곡

0개의 댓글