์ฒด์ก๋ณต lv1
๐๋ฌธ์ ๋งํฌ
์ด๊ฑธ ํ ๋๋ก ๋ฌธ์ ๋ฅผ ํ๋ ค๋ฉด, ์ฐ๋ฆฌ๊ฐ ๊ตฌํด์ผ ํ ๊ฐ์ธ ์ฒด์ก์์
์ ์ต๋ ์ฐธ์ฌ๊ฐ๋ฅ ํ์ ์
์ ๋ํด
์ฐธ๊ณ ํด์ผ ํ ๋ถ๋ถ์ 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.