관리 메뉴

막내의 막무가내 프로그래밍 & 일상

[알고리즘] 프로그래머스 오픈채팅방 -2019 KAKAO BLIND RECRUITMENT- -해시(Hash)- 본문

알고리즘/해시

[알고리즘] 프로그래머스 오픈채팅방 -2019 KAKAO BLIND RECRUITMENT- -해시(Hash)-

막무가내막내 2020. 5. 21. 15:00
728x90

https://programmers.co.kr/learn/courses/30/lessons/42888?language=kotlin

 

코딩테스트 연습 - 오픈채팅방

오픈채팅방 카카오톡 오픈채팅방에서는 친구가 아닌 사람들과 대화를 할 수 있는데, 본래 닉네임이 아닌 가상의 닉네임을 사용하여 채팅방에 들어갈 수 있다. 신입사원인 김크루는 카카오톡 오

programmers.co.kr

 

 

 

프로그래머스 LEVEL2 문제 오픈채팅방을 풀어봤습니다. ㅎㅎ

문제 풀이법은 HashMap으로 풀어야겠다고 바로 생각나서 쉽게 접근할 수 있었습니다.

 

1. HashMap 으로 마지막으로 추가 혹은 변경된 이름을 저장하게 합니다. uid를 key로 이름을 value로

2. 출력 문장을 만들어 줍니다. Levae는 두 단어인거 주의

3. 리스트를 배열로 변환 해서 리턴 ( 그냥 toArray 하면안됨 주의)

answer = result.toArray(new String[result.size()]);

 

풀이는 다음과 같습니다.

 

[Java]

import java.util.ArrayList;
import java.util.HashMap;

class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.solution(new String[]{"Enter uid1234 Muzi", "Enter uid4567 Prodo", "Leave uid1234", "Enter uid1234 Prodo", "Change uid4567 Ryan"});
    }

    public String[] solution(String[] record) {
        HashMap<String, String> map = new HashMap();
        ArrayList<String> result = new ArrayList<>();
        String[] answer = {};

        // 최종 이름 설정
        for (String str : record) {
            switch (str.split(" ")[0]) {
                case "Enter":
                case "Change":
                    String uid = str.split(" ")[1];
                    String name = str.split(" ")[2];
                    map.put(uid, name);
                    break;
            }
        }
        // 출력문 만들기
        for (String str : record) {
            switch (str.split(" ")[0]) {
                case "Enter":
                    result.add(map.get(str.split(" ")[1] ) + "님이 들어왔습니다.");
                    break;
                case "Leave":
                    result.add(map.get(str.split(" ")[1]) + "님이 나갔습니다.");
                    break;
            }
        }
        answer = result.toArray(new String[result.size()]);
        return answer;
    }
}

 

 

 

[Kotlin]

import java.util.*
class Solution {

    fun solution(record: Array<String>): Array<String> {
        val map = HashMap<String, String>()
        val result = ArrayList<String>()
        val answer: Array<String>

        for (str in record) {
            when (str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0]) {
                "Enter", "Change" -> {
                    val uid = str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1]
                    val name = str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[2]
                    map[uid] = name
                }
            }

        }
        for (str in record) {
            when (str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0]) {
                "Enter" -> result.add(map.get(str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1]) + "님이 들어왔습니다.")
                "Leave" -> result.add(map.get(str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1]) + "님이 나갔습니다.")
            }
        }
        answer = result.toTypedArray()
        return answer
    }

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            val solution = Solution()
            solution.solution(arrayOf("Enter uid1234 Muzi", "Enter uid4567 Prodo", "Leave uid1234", "Enter uid1234 Prodo", "Change uid4567 Ryan"))
        }
    }
}

 

 

https://github.com/mtjin/algorithm_practice/tree/master/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4%20%EC%98%A4%ED%94%88%EC%B1%84%ED%8C%85%EB%B0%A9%20-2019%20KAKAO%20BLIND%20RECRUITMENT-%20-%ED%95%B4%EC%8B%9C(Hash)-

 

mtjin/algorithm_practice

알고리즘 문제풀이 연습. Contribute to mtjin/algorithm_practice development by creating an account on GitHub.

github.com

 

댓글과 공감은 큰 힘이 됩니다. 감사합니다!!

728x90
Comments