Post

์ฒด์œก๋ณต lv1

๐ŸŒˆ๋ฌธ์ œ ๋งํฌ

Desktop View

Desktop View

Desktop View

์ด๊ฑธ ํ† ๋Œ€๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๋ ค๋ฉด, ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•ด์•ผ ํ•  ๊ฐ’์ธ ์ฒด์œก์ˆ˜์—…์— ์ตœ๋Œ€ ์ฐธ์—ฌ๊ฐ€๋Šฅ ํ•™์ƒ ์ˆ˜์— ๋Œ€ํ•ด

์ฐธ๊ณ ํ•ด์•ผ ํ•  ๋ถ€๋ถ„์€ case1, case3์ž…๋‹ˆ๋‹ค.

ํŒŒ๋ž€ ์„ , ๋ณด๋ผ์ƒ‰ ์„ ์ด ํ‚ค ํฌ์ธํŠธ!

ํ’€์ด 1

1
2
3
4
5
6
7
8
9
10
11
12
def solution(n, lost, reserve):
    _lost = set(lost)-set(reserve)
    _reserve = set(reserve)-set(lost)
    
    for i in _reserve:
        if i-1 in _lost:
            _lost.remove(i-1)
        elif i+1 in _lost:
            _lost.remove(i+1)
            
    answer = n-len(_lost)
    return answer




ํ’€์ด 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def solution(n, lost, reserve):
    # ์ •๋ ฌ
    lost.sort()
    reserve.sort()
	
    # lost, reserve์— ๊ณตํ†ต์œผ๋กœ ์žˆ๋Š” ์š”์†Œ ์ œ๊ฑฐ
    for i in reserve[:]:
        if i in lost:
            reserve.remove(i)
            lost.remove(i)
	
    # ์ฒด์œก๋ณต ๋นŒ๋ ค์ฃผ๊ธฐ(๋‚˜์˜ ์•ž ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ํ™•์ธ)
    for i in reserve:
        if i-1 in lost:
            lost.remove(i-1)
        elif i+1 in lost:
            lost.remove(i+1)
    
    return n-len(lost)
  • for i in reserve[:]:

    reserve ๋ณต์‚ฌ๋ณธ์„ ์ƒ์„ฑ

*์•Œ๊ฒŒ๋œ ๊ฒƒ

set()

  • ์ค‘๋ณต๋œ ๊ฒƒ ์ œ๊ฑฐ
1
set_a = set(a) - set(b)  




์ฐธ๊ณ 

https://codingkwon.tistory.com/189

https://dduniverse.tistory.com/entry/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-์ฒด์œก๋ณต-ํŒŒ์ด์ฌ-python

This post is licensed under CC BY 4.0 by the author.
3D GIF