Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-kjs254 #46

Merged
merged 1 commit into from
Jun 27, 2024
Merged

10-kjs254 #46

merged 1 commit into from
Jun 27, 2024

Conversation

kjs254
Copy link
Member

@kjs254 kjs254 commented Apr 2, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1h

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œํ•ด์„

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

ํ•œ๋งˆ๋””๋กœ ์š”์•ฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ ‘๋‘์‚ฌ๊ฐ€ ๋“ค์–ด์žˆ์œผ๋ฉด False ๋“ค์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ.

์ „ํ™”๋ฒˆํ˜ธ๋ถ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด False๋ฅผ ๋ฑ‰๊ณ , ์ˆœํšŒ๊ฐ€ ๋๋‚˜๋ฉด True๋ฅผ ๋ฑ‰์–ด๋‚ด๋„๋ก ์ฝ”๋“œ๋ฅผ ์งœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.



2. ์ˆ˜๋„์ฝ”๋“œ

์ ‘๋‘์‚ฌ๋ผ๋Š” ์ ์„ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌ์„ ์ด์šฉํ•˜์—ฌ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜๋ฉด ๋น„์Šทํ•œ ์ ‘๋‘์‚ฌ๋ผ๋ฆฌ ๋ชจ์ด๊ฒŒ ๋˜๋ฉฐ ํ˜„์žฌ ์ธ๋ฑ์Šค์™€ ๊ทธ ๋‹ค์Œ ์ธ๋ฑ์Šค๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ ‘๋‘์‚ฌ๋ฅผ ํŒ๋…ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

  1. ์ „ํ˜ธ๋ฒˆํ˜ธ๋ถ€๋ฅผ ์ •๋ ฌํ•œ๋‹ค.

  2. ํ˜„์žฌ ์›์†Œ๊ฐ€ ๋‹ค์Œ ์›์†Œ ๋‚ด์— ๋“ค์–ด์žˆ๋‹ค๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  3. True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ฝ”๋“œ

def solution(phone_book):
    phone_book.sort()
    i=0
    while i<=len(phone_book)-2:
        if phone_book[i] in phone_book[i+1] and phone_book[i][0]==phone_book[i+1][0]:
            return False
        i +=1
    return True

if ์กฐ๊ฑด๋ฌธ ๋‚ด์—์„œ ์‚ฌ์šฉํ•œ in ์€ ๋ฌธ์ž๊ฐ€ ์•ž์— ๋“ค์–ด์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํŒ๋…ํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์—†์–ด์„œ ์›์†Œ์˜ 0๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ๊ฐ™์€์ง€ ํŒ๋…ํ•˜๋Š” ์กฐ๊ฑด๋„ ์ถ”๊ฐ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ’€์ด๋ฅผ ์™„๋ฃŒํ•˜๊ณ  ๋ชจ๋ฒ”๋‹ต์•ˆ์„ ๋ณด๋Š” ์ˆœ๊ฐ„ ๋’คํ†ต์ˆ˜๋ฅผ ๊ฐ•ํƒ€ ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค.

ํŒŒ์ด์ฌ์˜ ๋ฌธ์ž์—ด ๋ฉ”์†Œ๋“œ์—๋Š” startswith() ๋ผ๋Š” ์ ‘๋‘์‚ฌ ํŒ๋… ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด

print('123456'.startswith('123'))

์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋Š” True ์ž…๋‹ˆ๋‹ค.

๋˜ํ•œ while ๋ณด๋‹จ for ๋กœ ๋ฐ˜๋ณตํ•˜๋Š”๊ฒƒ์ด ๋” ๋ณด๊ธฐ ์ข‹๋‹ค๊ณ  ํŒ๋‹จํ•˜์—ฌ ๊ฐœ์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ตœ์ข…์ฝ”๋“œ

def solution(phone_book):
    phone_book.sort()
    for i in range(len(phone_book)-1):
        if phone_book[i+1].startswith(phone_book[i]): 
            return False
    return True

๋‘ ์ฝ”๋“œ์˜ ์—ฐ์‚ฐ์†๋„๋Š” ์œ ์˜๋ฏธํ•˜๊ฒŒ ํฌ๊ฒŒ ๋‚˜์ง„ ์•Š์•˜์Šต๋‹ˆ๋‹ค.


๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํŒŒ์ด์ฌ์˜ startswith() ๋ฉ”์†Œ๋“œ๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

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

์•„๋ž˜๋Š” ํ•ด์‹œ๋กœ ํ‘ผ ์ •์„ํ’€์ด์ž…๋‹ˆ๋‹ค. ์ฐธ๊ณ ํ•˜์‹ค ๋ถ„๋“ค์€ ๋ด์ฃผ์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ ‘๊ธฐ/ํŽผ์น˜๊ธฐ
def solution(phone_book): 

    # 1.Hash map์ƒ์„ฑ
    hash_map = {} 
    for nums in phone_book: 
        hash_map[nums] = 1 
    
    # 2.์ ‘๋‘์–ด๊ฐ€ Hash map์— ์กด์žฌํ•˜๋Š”์ง€ ์ฐพ๊ธฐ 
    for nums in phone_book: 
        arr = "" 
        for num in nums: 
            arr += num
    
            # 3. ๋ณธ์ธ ์ž์ฒด์ผ ๊ฒฝ์šฐ๋Š” ์ œ์™ธ
            if arr in hash_map and arr != nums:       
                return False 
                
    return True

์ถœ์ฒ˜ : [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก _ Python ํ•ด์‹œLv.2

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

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

์ €๋„ ์ฒ˜์Œ์—” ์ •๋ ฌ๊ณผ ํŒŒ์ด์ฌ์˜ startswith() ํ•จ์ˆ˜ ๊ฐ™์ด std::find() ํ•จ์ˆ˜ ์จ์„œ ์ฐพ์œผ๋ ค๋Š” ์ƒ๊ฐ๋งŒ ํ–ˆ์—ˆ๋Š”๋ฐ ํ•ด์‹œ ๋งต์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๊ดœ์ฐฎ๋„ค์š”. ์ด๋ ‡๊ฒŒ๋„ ํ’€ ์ˆ˜ ์žˆ๊ตฌ๋‚˜.. ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. ์•„์ด๋””์–ด ํ•˜๋‚˜ ์ž˜ ์–ป์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

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

startswith, find ๋“ฑ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ํ’€์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ด ํ•จ์ˆ˜๋ฅผ ๋ชจ๋ฅธ๋‹ค๋ฉด ์กฐ๊ธˆ ๋ง‰๋ง‰ํ•œ ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™๊ตฐ์š”.
ํ•ด์‰ฌ๋กœ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์€ ํŠน์ดํ•œ ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์ƒ๊ฐ์„ ๋ชปํ•ด๋ด์„œ ๊ณต๋ถ€ํ•˜๊ธฐ์— ์ข‹์€ ์˜ˆ์‹œ๊ฐ€ ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ˜

@tgyuuAn tgyuuAn removed the request for review from mong3125 June 27, 2024 12:11
@tgyuuAn tgyuuAn merged commit cd9f01b into main Jun 27, 2024
1 check passed
@tgyuuAn tgyuuAn deleted the 10-kjs254 branch June 27, 2024 12:11
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