Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- react
- ๊ตญ๋น์ง์
- heapq
- ์ฝ๋ฉํ ์คํธ
- fe
- node.js
- ํ ์ดํ๋ก์ ํธ
- ๋๋ฆผ์ฝ๋ฉ
- ๊ทธ๋ฆฌ๋
- ๊ฐ๋ฐ
- ํ๋ก๊ทธ๋๋จธ์ค
- HTML
- JS
- ๋ชจ๊ฐ์ฝ
- ์๊ณ ๋ฆฌ์ฆ
- ํ๋ก ํธ์๋
- Til
- KDT
- Python
- CSS
- ํฌ๋กค๋ง
- ์ฝ๋ฉ์ ํ
- ํ์ด์ฌ
- mongodb
- ์ฝ๋ฉ
- javascript
- ํ๋ก์ ํธ
- ๋ฐฑ์ค
- ๋ ธ๋ง๋์ฝ๋
- error
Archives
- Today
- Total
๐ฑ → ๐ณ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฒด์ก๋ณต - python ๋ณธ๋ฌธ
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
728x90
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ฉ๋ฆฌ๋ฐ๊ธฐ - Python (0) | 2023.08.14 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] N๊ฐ์ ์ต์๊ณต๋ฐฐ์ - Python (0) | 2023.08.13 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ ๋งต๊ฒ - python (0) | 2023.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ธฐ๋ฅ๊ฐ๋ฐ - python (0) | 2023.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ฃผํ์ง ๋ชปํ ์ ์ - python (0) | 2023.08.02 |