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
- ํ ์ดํ๋ก์ ํธ
- KDT
- heapq
- node.js
- ํ๋ก๊ทธ๋๋จธ์ค
- mongodb
- ๋๋ฆผ์ฝ๋ฉ
- ํฌ๋กค๋ง
- javascript
- Python
- fe
- ํ๋ก ํธ์๋
- ์ฝ๋ฉ
- ํ๋ก์ ํธ
- ๋ชจ๊ฐ์ฝ
- JS
- react
- ๋ฐฑ์ค
- ๋ ธ๋ง๋์ฝ๋
- ํ์ด์ฌ
- error
- ์ฝ๋ฉ์ ํ
- ๊ตญ๋น์ง์
- Til
- ๊ทธ๋ฆฌ๋
- ๊ฐ๋ฐ
- HTML
- CSS
- ์ฝ๋ฉํ ์คํธ
- ์๊ณ ๋ฆฌ์ฆ
Archives
- Today
- Total
๐ฑ → ๐ณ
[๋ฐฑ์ค] 18870๋ฒ: ์ขํ ์์ถ python ๋ณธ๋ฌธ
728x90
https://www.acmicpc.net/problem/18870
๐ ๋ฌธ์ ํด์ค
"Xi>Xj๋ฅผ ๋ง์กฑํ๋ ์๋ก ๋ค๋ฅธ ์ขํ์ ๊ฐ์์ ๊ฐ์์ผ๋๋ค" ๋ผ๋ ๋ป์ ์ฆ Xi๊ฐ ๋ฆฌ์คํธ ์์์์ ํฌ๊ธฐ ์์๋ฅผ ์ถ๋ ฅํ๋ฉด ๋๋ค๋ ๋ง
(ํฌ๊ธฐ ์์๋ 0๋ถํฐ ์์.)
์ฆ, ๋ฆฌ์คํธ์์์ ์๊ธฐ๋ณด๋ค ์์ ์ซ์์ ๊ฐ์๋ฅผ ์ธ๋ ๊ฒ์ด๋ฏ๋ก, ์์ ์ด ๋ฆฌ์คํธ ์์์์ ํฌ๊ธฐ ์์๋ฅผ ์ถ๋ ฅํ๋ฉด ๋จ
๐ ์ฝ๋
์คํจ ์ฝ๋ (์๊ฐ ์ด๊ณผ)
n = int(input())
arr = list(map(int, input().split()))
arr2 = sorted(list(set(arr)))
for i in arr:
print(arr2.index(i), end = ' ')
list.index(i) → ์๊ฐ ๋ณต์ก๋ O(n)
์ฑ๊ณต ์ฝ๋
n = int(input())
arr = list(map(int, input().split()))
arr2 = sorted(list(set(arr)))
dic = {arr2[i]: i for i in range(len(arr2))}
for i in arr:
print(dic[i], end=' ')
index[i] → ์๊ฐ ๋ณต์ก๋ O(1)
โ๏ธ ์์ธํ ์์๋ณด์) arr2, dic๋ print ํด์ ํ์ธํด๋ณด๊ธฐ
n = int(input())
arr = list(map(int, input().split()))
arr2 = sorted(list(set(arr)))
print(arr2)
dic = {arr2[i]: i for i in range(len(arr2))}
print(dic)
for i in arr:
print(dic[i], end=' ')
๊ฒฐ๊ณผ
๐ ๊ฒฐ๋ก
list.index(i) ํํ์ ์๊ฐ ๋ณต์ก๋ = O(N)
index[i] ํํ์ ์๊ฐ ๋ณต์ก๋ = O(1)
๐ ํ์ด์ฌ ๋์ ๋๋ฆฌ์ ๊ดํด์..
- ๋์ ๋๋ฆฌ์ด๋ฆ = {"key๊ฐ" : "value๊ฐ"}
- key ์ค๋ณต ํ์ฉ X
- key๊ฐ ์ค๋ณต ๋ ๊ฒฝ์ฐ ๋ง์ง๋ง์ ์ ๋ ฅ๋ key์ value๋ฅผ ์ถ๋ ฅ.
1๋ฒ ์์ )
dic = {"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4",}
print(dic["key1"]
#๊ฒฐ๊ณผ
# value1
2๋ฒ ์์ )
arr = [1, 0, 2, -4, 6, 33, 22]
dic = {arr[i]:i for i in range (len(arr))}
print("dic =",dic)
print("dic[33] = ",dic[33])
# ์ถ๋ ฅ๊ฒฐ๊ณผ
# dic = {1: 0, 0: 1, 2: 2, -4: 3, 6: 4, 33: 5, 22: 6}
# dic[33] = 5
์ฐธ๊ณ ํ ๋ธ๋ก๊ทธ :
https://eunhee-programming.tistory.com/116
[์ฝ๋์ง๋ ๋ฌธ๊ณผ๋ :ํฐ์คํ ๋ฆฌ]
728x90
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Algorithm) Greedy (0) | 2022.10.25 |
---|---|
Algorithm) ์๊ณ ๋ฆฌ์ฆ ์ฑ๋ฅ (0) | 2022.10.25 |
[๋ฐฑ์ค] 2108๋ฒ: ํต๊ณํ python (0) | 2022.10.02 |
[๋ฐฑ์ค] 10989๋ฒ: ์ ์ ๋ ฌํ๊ธฐ 3 python (0) | 2022.10.02 |
[๋ฐฑ์ค] 2750, 2751 - ์ ์ ๋ ฌํ๊ธฐ 1,2 python (0) | 2022.10.02 |