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
- error
- KDT
- HTML
- ๋ชจ๊ฐ์ฝ
- javascript
- ๊ฐ๋ฐ
- react
- node.js
- ํ ์ดํ๋ก์ ํธ
- JS
- ์ฝ๋ฉํ ์คํธ
- CSS
- heapq
- ํ์ด์ฌ
- ๋ ธ๋ง๋์ฝ๋
- ๋๋ฆผ์ฝ๋ฉ
- mongodb
- ๋ฐฑ์ค
- Til
- Python
- ํฌ๋กค๋ง
- ๊ตญ๋น์ง์
- ์ฝ๋ฉ์ ํ
- ํ๋ก ํธ์๋
- ์ฝ๋ฉ
- fe
- ๊ทธ๋ฆฌ๋
- ํ๋ก๊ทธ๋๋จธ์ค
- ํ๋ก์ ํธ
- ์๊ณ ๋ฆฌ์ฆ
Archives
- Today
- Total
๐ฑ → ๐ณ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ ํ๋ฒํธ ๋ชฉ๋ก - python ๋ณธ๋ฌธ
728x90
์ฝ๋
์๊ฐ ์ด๊ณผ ์ฝ๋
def solution(phone_book):
phone_book.sort()
for i in range(len(phone_book)):
for j in range(i+1, len(phone_book)):
if phone_book[j].startswith(phone_book[i]):
return False
return True
์๊ฐ๋ณต์ก๋(big-O)์ ๋ฐ์ดํฐ ํฌ๊ธฐ(n)์ ๋ฃ์ด์ ๋์จ ๊ฐ์ด 100,000,000(1์ต)์ด ๋์ผ๋ฉด ์๊ฐ ์ ํ ์ด๊ณผํ ๊ฐ๋ฅ์ฑ ์์ !
for๋ฌธ์ 2๋ฒ์ฐ๋ฉด ์ต์ ์ ๊ฒฝ์ฐ O(N^2)
1 ≤ phone_book ≤ 1000000 ์ด๋ฏ๋ก, O(N^2)์ ์๋จ.
๋๋๊ฒ๋ ์ด๋์ ๊ฐ์ ์ฝ๋๋ฅผ ์์ฑํ๋ค ใ ใ
์๊ฐ๋ณต์ก๋ ์๊ฐํ๋ฉด์ ๋ฌธ์ ํ์ผ๋ผ๊ณ ! ! ! ! !
์ ๋ต ์ฝ๋
def solution(phone_book):
phone_book.sort()
for i in range(len(phone_book)-1):
if phone_book[i] == phone_book[i+1][:len(phone_book[i])]:
return False
return True
def solution(phone_book):
phone_book.sort()
for i in range(len(phone_book)-1):
if phone_book[i+1].startswith(phone_book[i]):
return False
return True
l = ['10', '1', '5']
print(sorted(l))
# ['1', '10', '5']
string์ sort ๋ฐฉ๋ฒ์ ๋ณด๋ฉด ์ซ์ ์๋ฆฟ์
728x90
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ด์ค์ฐ์ ์์ํ - python (1) | 2023.11.09 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ - python (1) | 2023.10.30 |
[Baekjoon] 4963. ์ฌ์ ๊ฐ์ - python (0) | 2023.10.27 |
[Baekjoon] 1926. ๊ทธ๋ฆผ - python (0) | 2023.10.03 |
[Baekjoon] 7490. 0 ๋ง๋ค๊ธฐ - python (1) | 2023.10.02 |