๐ŸŒฑ → ๐ŸŒณ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฒด์œก๋ณต - python ๋ณธ๋ฌธ

Algorithms

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฒด์œก๋ณต - python

BAY 2023. 8. 2. 17:26
728x90

์ฝ”๋“œ

์ฒ˜์Œ ์ฝ”๋“œ

def solution(n, lost, reserve):
    lst = sorted(lost)
    reserve.sort()
    for i in lst1:
        if i-1 in reserve:
            reserve.remove(i-1)
            lost.remove(i)
        elif i in reserve:
            reserve.remove(i)
            lost.remove(i)
        elif i+1 in reserve:
            reserve.remove(i+1)
            lost.remove(i)
    return n-len(lost)

์•„ . .!

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. .. .

 

๋‹ค์‹œ ๋„์ „

def solution(n, lost, reserve):
    lst = sorted(lost)
    reserve.sort()
    for i in lst:
        if i in reserve:
            reserve.remove(i)
            lost.remove(i)
    lst2 = lost[:]
    for i in lst2:
        if i-1 in reserve:
            reserve.remove(i-1)
            lost.remove(i)
        elif i+1 in reserve:
            reserve.remove(i+1)
            lost.remove(i)
    return n-len(lost)

์ด๋žฌ๋”๋‹ˆ ์ด์ œ test 13,14๊ฐ€ ์•ˆ๋จ

;;;; ๋ญ๊ฐ€ ๋ฌธ์ œ์•ผ ๋„๋Œ€์ฒด

์•„์•…

 

์•„ lst = sorted(lost) ์ด๋ ‡๊ฒŒ ํ•ด์ค˜๋„ lst2๋Š” lost๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—

lst2 = lost[:] ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ •๋ ฌ์ด ๋˜์ง€ ์•Š์€ lost๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋จ

 

๐Ÿ—ฏ๏ธ ๋ฐ˜๋ก€ ์ž…๋ ฅ๊ฐ’ ใ€‰ 5, [4, 2], [3, 5] ๊ธฐ๋Œ“๊ฐ’ ใ€‰ 5

 

ํ•ด๊ฒฐ์ฝ”๋“œ

def solution(n, lost, reserve):
    lst = sorted(lost)
    reserve.sort()
    for i in lst:
        if i in reserve:
            reserve.remove(i)
            lost.remove(i)
    lst2 = sorted(lost)
    for i in lst2:
        if i-1 in reserve:
            reserve.remove(i-1)
            lost.remove(i)
        elif i+1 in reserve:
            reserve.remove(i+1)
            lost.remove(i)
    return n-len(lost)

 

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

def solution(n, lost, reserve):
    answer = 0
    students, rent  = [1]*(n+1), [0]*(n+2)
    students[0],rent[0] = 3,3
    
    for lo in lost:
        students[lo] = 0
    for re in reserve:
        rent[re] = 1
        
    for i in range(1,n+1):
        if students[i] == 0:
            if rent[i] == 1:
                rent[i] = 0 
                students[i] =1
            elif rent[i-1] == 1 and students[i-1] != 0: 
                rent[i-1] = 0
                students[i] =1
            elif rent[i+1] == 1 and students[i+1] != 0:
                rent[i+1] = 0
                students[i] =1
                
    for student in students:
        if student == 1:
            answer += 1
    return answer

 

์ฐธ๊ณ ์ž๋ฃŒ 

https://star-ccomputer-go.tistory.com/75

 

(Python) ์ฒด์œก๋ณต(ํ”„๋กœ๊ทธ๋ž˜๋จธ์ŠคLv.1)

์ฒด์œก๋ณต(ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.1) https://school.programmers.co.kr/learn/courses/30/lessons/42862 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ ,

star-ccomputer-go.tistory.com

 

728x90