๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค LV2 ์š”๊ฒฉ ์‹œ์Šคํ…œ = ๋‹จ์†์นด๋ฉ”๋ผ

๐ŸŽฎinspirer9 2023. 6. 28. 22:39
728x90
๋ฐ˜์‘ํ˜•

์˜ค๋Š˜ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋‚˜๋Š” ๋ฐ”๋ณด์˜€๋‹ค๋Š” ์ƒ๊ฐ์„ ํ–ˆ๋‹ค.

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

def solution(targets):
    answer = 0
    targets.sort()
    head,tail = 0,0
    
    for t in targets:
        s,e = t
        #print(head,tail,"<-",s,e, answer)
        if tail <= s:
            answer += 1
            head,tail = s,e
        else: 
            head = max(head, s)
            tail = min(tail, e)

    return answer

์ด๊ฑฐ๋ž‘ ๋น„์Šทํ•œ ๋ฌธ์ œ๊ฐ€ ๋‹จ์†์นด๋ฉ”๋ผ์ธ๋ฐ, ๊ทธ๋•Œ ๋‚ด๊ฐ€ ํ’€์—ˆ๋˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ใ…ก.ใ…ก;

๊ทธ๋•Œ๊ฐ€ 2023๋…„ 2์›” 27์ผ์ด์—ˆ๋Š”๋ฐ C์–ธ์–ด๋ž‘ C#์–ธ์–ด๋กœ ํ’€๋‹ค๊ฐ€ ํŒŒ์ด์ฌ์œผ๋กœ ์–ธ์–ด๋ฅผ ๋ฐ”๊ฟจ์„ ๋•Œ์˜€๋‹ค.

์ง€๊ธˆ๋ณด๋‹ˆ ํŒŒ์ด์ฌ ์ฝ”๋”ฉํ•˜๋Š” ์Šคํƒ€์ผ์ด ๋งŽ์ด ๋ฐ”๋€Œ์—ˆ๋‹ค.

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

def solution(routes):
    routes.sort(key=lambda x:(x[1],x[0]))
    ์•ˆ์ฐํžŒ์ฐจ๋Ÿ‰ = len(routes)
    ์ฐฐ์นต = [1 for _ in range(len(routes))]
    
    ์นด๋ฉ”๋ผ_์„ค์น˜_์ˆ˜๋Ÿ‰ = 0
    ์นด๋ฉ”๋ผ_index = 0
    ์นด๋ฉ”๋ผ_์œ„์น˜ = routes[์นด๋ฉ”๋ผ_index][1]
    
    while ์•ˆ์ฐํžŒ์ฐจ๋Ÿ‰ > 0:
        for i in range(์นด๋ฉ”๋ผ_index,len(routes)):
            if ์ฐฐ์นต[i] == 1 and routes[i][0] <= ์นด๋ฉ”๋ผ_์œ„์น˜ and ์นด๋ฉ”๋ผ_์œ„์น˜ <= routes[i][1]:
                ์ฐฐ์นต[i] = 0
                ์•ˆ์ฐํžŒ์ฐจ๋Ÿ‰ -= 1
        for i in range(์นด๋ฉ”๋ผ_index,len(routes)):
            if ์ฐฐ์นต[i] == 1:
                ์นด๋ฉ”๋ผ_index = i
                ์นด๋ฉ”๋ผ_์œ„์น˜ = routes[์นด๋ฉ”๋ผ_index][1]
                ์นด๋ฉ”๋ผ_์„ค์น˜_์ˆ˜๋Ÿ‰ += 1
                break
    return ์นด๋ฉ”๋ผ_์„ค์น˜_์ˆ˜๋Ÿ‰ + 1

 

๋ฒŒ์จ 4๊ฐœ์›” ์ „ ์ด์•ผ๊ธฐ๊ตฌ๋‚˜... ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ๋น ๋ฅด๊ฒŒ ํ๋ฅด๋„ค...

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋‹จ์†์นด๋ฉ”๋ผ ... ํƒ์š•๋ฒ•

๋ฌธ์ œ ์„ค๋ช… ๊ณ ์†๋„๋กœ๋ฅผ ์ด๋™ํ•˜๋Š” ๋ชจ๋“  ์ฐจ๋Ÿ‰์ด ๊ณ ์†๋„๋กœ๋ฅผ ์ด์šฉํ•˜๋ฉด์„œ ๋‹จ์†์šฉ ์นด๋ฉ”๋ผ๋ฅผ ํ•œ ๋ฒˆ์€ ๋งŒ๋‚˜๋„๋ก ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ณ ์†๋„๋กœ๋ฅผ ์ด๋™ํ•˜๋Š” ์ฐจ๋Ÿ‰์˜ ๊ฒฝ๋กœ routes๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ

inspirer9.tistory.com

๊ทผ๋ฐ ์‹œ๊ฐ„์ด ๋น ๋ฅด๊ฒŒ ํ๋ฅด๋Š”๊ฒŒ ์ข‹๋‹ค. ํž˜๋“ค๊ณ  ๊ดด๋กœ์šด ์‹œ๊ฐ„์ด ๋น ๋ฅด๊ฒŒ ํ๋ฅด๋Š” ๊ฑฐ๋‹ˆ๊นŒ...

์–ด๋ฆด ๋• ํž˜๋“ค๊ณ  ๊ดด๋กœ์šด ๊ฒƒ๋“ค์„ ๊ธˆ๋ฐฉ ์žŠ์—ˆ๋Š”๋ฐ, ์š”์ฆ˜์€ ์ž˜ ์žŠํ˜€์ง€์ง€ ์•Š์•„์„œ ๋” ํž˜๋“  ๊ฒƒ ๊ฐ™๋‹ค.

728x90
๋ฐ˜์‘ํ˜•