Skip to content

Latest commit

ย 

History

History
56 lines (40 loc) ยท 1.74 KB

2019-05-31-AT-phone_book.md

File metadata and controls

56 lines (40 loc) ยท 1.74 KB

2019๋…„ 5์›” 31์ผ

Lv2 - ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก {docsify-ignore-all}

์ถœ์ฒ˜: https://programmers.co.kr/learn/courses/30/lessons/42577

๋ฌธ์ œ

์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ ์ค‘, ํ•œ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ, ๊ตฌ์กฐ๋Œ€ ์ „ํ™”๋ฒˆํ˜ธ๋Š” ์˜์„์ด์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค.

๊ตฌ์กฐ๋Œ€ : 119 ๋ฐ•์ค€์˜ : 97 674 223 ์ง€์˜์„ : 11 9552 4421 ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด phone_book ์ด solution ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์–ด๋–ค ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฉด false๋ฅผ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด true๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • phone_book์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 1,000,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

ํ’€์ด

def cmp(x):
    return len(x)

def phone_book_list(phone_book):
    phone_book.sort(key=cmp)
    for i in range(0,len(phone_book)-1):
        for j in range(i+1,len(phone_book)):
            if phone_book[j].startswith(phone_book[i]):
                return False
    return True

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

def solution(phone_book):
    phone_book.sort()
    for a,b in zip(phone_book, phone_book[1:]):
        if b.startswith(a):
            return False
    return True

๋ฐฐ์šด์ 

  • ๋ฌธ์ž์—ด ์ •๋ ฌํ•˜๋ฉด ASCII ์ฝ”๋“œ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค.
  • ์ •๋ ฌํ•œ ๋’ค์—๋Š” 0~9 ์ˆœ์„œ๋กœ ์ •๋ ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด์ค‘ ํฌ๋ฌธ์„ ์“ธ ํ•„์š”๊ฐ€ ์—†๋‹ค.

๋Š๋‚€์ 

  • ์ •๋ ฌํ•˜๊ณ  ์ด์ค‘ํฌ๋ฌธ์„ ์จ์„œ ๋น„ํšจ์œจ์ ์ธ ์ฝ”๋“œ๋ฅด ์งฐ๋‹ค.ใ… ใ… 
  • ๊ทธ ๋™์•ˆ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€ ๋•Œ zip()์„ ํ•œ ๋ฒˆ๋„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค. ์•ž์œผ๋กœ๋Š” ์ž˜ ํ™œ์šฉํ•ด๋ณด์ž.