본문 바로가기
1-3. 코딩테스트(프로그래머스)/PCCP(Lv2)

[PCCP] Lv2: 오픈 채팅방(42888) 해설

by cogito21_cpp 2024. 12. 24.
반응형

문제

- 문제 링크: 오픈 채팅방

 

코드

(C언어)

solution 1)

더보기
#include<>

solution 2)

더보기
#include<>

solution 3)

더보기
#include<>

 

(C++)

solution 1)

- N: record의 길이

- 첫번째 반복문에서 record의 모든 항목을 순회하므로 시간 복잡도는 O(N)

- 두번째 반복문의 시간 복잡도는 O(N)

- 최종 시간 복잡도: O(N)

더보기
#include <vector>
#include <unordered_map>
#include <sstream>

using namespace std;

vector<string> solution(vector<string> record) {
    vector<string> answer;
    unordered_map<string, string> uid;
    
    for (const auto& line : record) {
        // 각 record마다 cmd에는 명령어를 저장하고 id에는 닉네임을 저장
        stringstream ss(line);
        string cmd, id, nickname;
        ss >> cmd >> id;
        // 명령어가 Enter 혹은 Change면 nickname에 닉네임 저장
        if (cmd != "Leave") {
            ss >> nickname;
            uid[id] = nickname;
        }
    }
    
    for (const auto& line : record) {
        stringstream ss(line);
        string cmd, id;
        ss >> cmd >> id;
        // Enter 및 Leave 명령어는 최종 닉네임과 최종 닉네임과 정해진 문자열을 answer에 추가
        if (cmd == "Enter") {
            answer.push_back(uid[id] + "님이 들어왔습니다.");
        } else if (cmd == "Leave") {
            answer.push_back(uid[id] + "님이 나갔습니다.");
        }
        // change는 메시지 생성에 영향을 주지 않으므로 무시
    }
    return answer;
}

solution 2)

더보기
#include<>

solution 3)

더보기
#include<>

 

(C#)

solution 1)

더보기
#include<>

solution 2)

더보기
#include<>

solution 3)

더보기
#include<>

 

(Java)

solution 1)

- N: record의 길이

- 첫 번째 반복문에서 record의 모든 항목을 순회하므로 O(N)이고 두 번째 반복문은 O(N)

- 최종 시간 복잡도: O(N)

더보기
import java.util.ArrayList;
import java.util.HashMap;

class Solution {

    public String[] solution(String[] record) {
        // Enter/Leave 메세지를 저장할 해시맵 생성
        HashMap<String, String> msg = new HashMap<>();
        msg.put("Enter", "님이 들어왔습니다.");
        msg.put("Leave", "님이 나갔습니다.");

        HashMap<String, String> uid = new HashMap<>();

        // record의 각 줄을 하나씩 처리
        for (String s : record) {
            String[] cmd = s.split(" ");
            if (cmd.length == 3) { // Enter 또는 Change인 경우
                uid.put(cmd[1], cmd[2]);
            }
        }

        // 답을 저장할 answer List 생성
        ArrayList<String> answer = new ArrayList<>();

        // record의 각 줄을 하나씩 처리
        for (String s : record) {
            String[] cmd = s.split(" ");
            // 각 상태에 맞는 메세지를 answer에 저장
            if (msg.containsKey(cmd[0])) {
                answer.add(uid.get(cmd[1]) + msg.get(cmd[0]));
            }
        }

        return answer.toArray(new String[0]);
    }

}

solution 2)

더보기
#include<>

solution 3)

더보기
#include<>

 

(Python)

solution 1)

- N: record의 길이

- 첫 번째 반복문에서 record의 모든 항목을 순회: O(N)

- 두 버째 반복문의 시간 복잡도: O(N)

- 최종 시간 복잡도: O(N)

더보기
def solution(record):
    answer = []
    uid = {}
    for line in record: # record의 각 줄을 하나씩 처리
        cmd = line.split(" ")
        if cmd[0] != "Leave": # Enter 또는 Change인 경우
            uid[cmd[1]] = cmd[2]
    for line in record: # record의 각 줄을 하나씩 처리
        cmd = line.split(" ")
        # 각 상태에 맞는 메시지를 answer에 저장
        if cmd[0] == "Enter":
            answer.append("%s님이 들어왔습니다." %uid[cmd[1]])
        elif cmd[0] == "Change":
            pass
        else:
            answer.append("%s님이 나갔습니다." % uid[cmd[1]])
    return answer

solution 2)

더보기
import

solution 3)

더보기
import

 

(JavaScript)

solution 1)

- N: record의 길이

- 첫 번째 반복문에서 record의 모든 항목을 순회: O(N)

- 두 번째 반복문: O(N)

- 최종 시간 복잡도: O(N)

더보기
function solution(record) {
    answer = [];
    uid = {};
    
    for (line in record) { // record의 각 줄을 하나씩 처리
        cmd = record[line].split(" ");
        if (cmd[0] != "Leave") { // Enter 또는 Change인 경우
            uid[cmd[1]] = cmd[2];
        }
    }
    
    for (line in record) { // record의 각 줄을 하나씩 처리
        cmd = record[line].split(" ");
        
        // 각 상태에 맞는 메시지를 answer에 저장
        if (cmd[0] == "Enter") {
            answer.push(uid[cmd[1]] + "님이 들어왔습니다.");
        } else if (cmd[0] == "Leave") {
            answer.push(uid[cmd[1]] + "님이 나갔습니다.");
        }
    }

    return answer;
}

solution 2)

더보기
import

solution 3)

더보기
import

 

반응형