Skip to content

Conversation

@mj010504
Copy link
Collaborator

πŸ”— 문제 링크

μ˜€ν”ˆμ±„νŒ…λ°©
https://school.programmers.co.kr/learn/courses/30/lessons/42888

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

  • μ‹€μ œλ‘œ μΉ΄μΉ΄μ˜€ν†‘ μ˜€ν”ˆμ±„νŒ…λ°©κ³Ό μœ μ‚¬ν•˜κ²Œ μ±„νŒ… 내역을 λ§Œλ“œλŠ” λ¬Έμ œμž…λ‹ˆλ‹€.
  • μ±„νŒ… 내역을 λ§Œλ“€ λ•Œ λ‹‰λ„€μž„μ΄ λ³€κ²½λ˜λŠ” 뢀뢄을 κ³ λ €ν•΄μ•Ό ν•©λ‹ˆλ‹€. λ‹‰λ„€μž„μ€ μž…μž₯ν•˜κ³  λ‚˜μ„œ λ³€κ²½, λ‚˜κ°€μ„œ λ³€κ²½ν•˜κ³  λ“€μ–΄μ˜€κΈ° 2κ°€μ§€ 방법이 μžˆλŠ”λ° 사싀상 μž…μž₯ μƒνƒœμ΄λ“  μ•ˆν•œ μƒνƒœμ΄λ“  λ‹‰λ„€μž„μ΄ λ³€κ²½ν•˜λ‹€λŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.
  • λ”°λΌμ„œ μ €λŠ” map<string, string>을 톡해 Enterλ˜λŠ” Change λ™μž‘μ‹œμ— userIdλ³„λ‘œ 이름을 맀번 κ°±μ‹ ν•΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€. Enterμ‹œμ—λŠ” λ‚˜κ°€μ„œ λ‹‰λ„€μž„μ„ λ³€κ²½ν•˜κ³ μ˜¨ 경우λ₯Ό λ°˜μ˜ν•˜κΈ° μœ„ν•΄μ„œ μž…λ‹ˆλ‹€.
  • 이 map을 μ΄μš©ν•΄μ„œ μ±„νŒ…λ°©μ—μ„œ λ‚˜κ°„ μœ μ €μ˜ 이름도 idλ₯Ό 톡해 금방 μ°Ύμ•„λ‚Ό 수 μžˆμŠ΅λ‹ˆλ‹€.
  • λ˜ν•œ μ±„νŒ… ꡬ쑰λ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•΄ ꡬ쑰체 chat을 λ§Œλ“€μ–΄ μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€. ν•΄λ‹Ή chat을 λ¬Έμ œμ—μ„œ μš”κ΅¬ν•˜λŠ” μ±„νŒ…μœΌλ‘œ λ³€ν™˜ν•˜λŠ” toKoreanChatν•¨μˆ˜λ„ λ”°λ‘œ λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

  • κ°„λ‹¨ν•œ κ΅¬ν˜„ λ¬Έμ œμ΄μ§€λ§Œ μš”κ΅¬μ‚¬ν•­λ“€μ„ 잘 νŒŒμ•…ν•΄μ„œ μ–΄λ–»κ²Œ ν’€μ§€ 미리 ꡬ상해놓고 풀이에 λ“€μ–΄κ°€λŠ” 것이 νš¨κ³Όμ μ΄λΌλŠ” 생각이 λ“€μ—ˆμŠ΅λ‹ˆλ‹€.

Copy link
Collaborator

@Seol-Munhyeok Seol-Munhyeok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ €λŠ” recordλ₯Ό 두 번 μˆœνšŒν•΄μ„œ 첫 번째 μˆœνšŒμ—μ„œλŠ” dict μžλ£Œν˜•μ— μ΅œμ’… ν™•μ •λ˜λŠ” user_id와 λ‹‰λ„€μž„μ„ μ €μž₯ν•˜λ„λ‘ ν•˜κ³ , 두 번째 μˆœνšŒν•  λ•ŒλŠ” μ΅œμ’… λ‹‰λ„€μž„μ— λ§žμΆ°μ„œ λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€. λ˜λŠ” λ‚˜κ°”μŠ΅λ‹ˆλ‹€.λ₯Ό 좜λ ₯ν•˜λ„λ‘ ν–ˆμŠ΅λ‹ˆλ‹€.

from collections import defaultdict

def solution(record):
    answer = []
    
    # μ΅œμ’… λ‹‰λ„€μž„ κ²°μ •
    mp = defaultdict(str)
    for rec in record:
        r = rec.split()
        if len(r) == 3:
            mp[r[1]] = r[2]
            
    # λͺ…령어에 λ§žμΆ°μ„œ 좜λ ₯
    for rec in record:
        r = rec.split()
        if r[0] == "Enter":
            answer.append(mp[r[1]] + "λ‹˜μ΄ λ“€μ–΄μ™”μŠ΅λ‹ˆλ‹€.")
        elif r[0] == "Leave":
            answer.append(mp[r[1]] + "λ‹˜μ΄ λ‚˜κ°”μŠ΅λ‹ˆλ‹€.")
            
    return answer

μ²˜μŒμ— μž…λ ₯ 크기가 100000 μ΄λΌμ„œ 효율적으둜 ν•œ 번의 순회둜 ν’€μ–΄μ•Όν•˜λ‚˜? ν–ˆλŠ”λ° κ·Έλƒ₯ 두 번 μˆœνšŒν•΄μ„œ ν‘ΈλŠ”κ²Œ κΉ”λ”ν•œ 풀이인 κ±° κ°™μŠ΅λ‹ˆλ‹€.

μ‹œκ°„λ³΅μž‘λ„λŠ” $O(2N)=O(N)$ μž…λ‹ˆλ‹€.

μˆ˜κ³ ν–ˆμŠ΅λ‹ˆλ‹€!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants