λ°±μ€ (BOJ) 11729λ²
https://www.acmicpc.net/problem/2108
μ¬μ©μΈμ΄ : PYTHON
1.λ¬Έμ
μλ₯Ό μ²λ¦¬νλ κ²μ ν΅κ³νμμ μλΉν μ€μν μΌμ΄λ€. ν΅κ³νμμ Nκ°μ μλ₯Ό λννλ κΈ°λ³Έ ν΅κ³κ°μλ λ€μκ³Ό κ°μ κ²λ€μ΄ μλ€. λ¨, Nμ νμλΌκ³ κ°μ νμ.
μ°μ νκ· : Nκ°μ μλ€μ ν©μ NμΌλ‘ λλ κ°
μ€μκ° : Nκ°μ μλ€μ μ¦κ°νλ μμλ‘ λμ΄νμ κ²½μ° κ·Έ μ€μμ μμΉνλ κ°
μ΅λΉκ° : Nκ°μ μλ€ μ€ κ°μ₯ λ§μ΄ λνλλ κ°
λ²μ : Nκ°μ μλ€ μ€ μ΅λκ°κ³Ό μ΅μκ°μ μ°¨μ΄
Nκ°μ μκ° μ£Όμ΄μ‘μ λ, λ€ κ°μ§ κΈ°λ³Έ ν΅κ³κ°μ ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
2.νμ΄
μ΅λΉκ°μ ꡬνλ κ²μ΄ λ¬Έμ μλλ°,
νμ΄μ¬μ λ΄μ₯ ν¨μμΈ Collectionλͺ¨λμ Counterμ μ¬μ©νμ¬ ν΄κ²° ν μ μμλ€.
Counterν¨μ
보ν΅μ dictionaryλ₯Ό μ¬μ©νμ¬ λ¬Έμμ΄μ μλ κ° μνλ²³μ κ°―μλ₯Ό μΉ΄μ΄ν μ νλ€.
def countLetters(word):
counter={}
for letter in word:
if letter not in counter:
counter[letter]=0
counter[letter]+=1
return counter
counterLetter('hello world')
# {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}
μ΄κ²μ Counterν¨μλ₯Ό μ¬μ©νλ©΄ μ½κ² ꡬν ν μ μμλ€.
from collections import Counter
print(Counter('hello world'))
# Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
λμ λ리 ννλ‘ λ°ν λκΈ°λλ¬Έμ, 루ν ννλ‘ λ릴 μ λ μλ€.
value='hello world'
countValue=Counter(value)
for key,value in countValue.items():
print(key,':',value)
h : 1
e : 1
l : 3
o : 2
: 1
w : 1
r : 1
d : 1
μΆκ°λ‘ most_common() λ©μλλ₯Ό μ¬μ©νμ¬ μμμμλ₯Ό λ°ν ν μλ μλ€.
from collections import Counter
print(Counter('hello world').most_common())
# [('l', 3), ('o', 2), ('h', 1), ('e', 1), (' ', 1), ('w', 1), ('r', 1), ('d', 1)]
μ΄λ μΈμλ‘ μ«μ kλ₯Ό λ£μ΄μ£Όλ©΄ μμ kκ°μ λ°μ΄ν°λ₯Ό μ»μ μλ μλ€.
from collections import Counter
print(Counter('hello world').most_common(1))
# [('l', 3)]
3.μ½λ
import sys
from collections import Counter
N=int(sys.stdin.readline())
l=[]
for _ in range(N):
l.append(int(sys.stdin.readline()))
cnt=[]
l.sort()
print(round(sum(l)/N))
print(l[N//2])
counter_l=Counter(l).most_common()
if len(counter_l)>1 and counter_l[0][1]==counter_l[1][1]:
print(counter_l[1][0])
else:
print(counter_l[0][0])
print(max(l)-min(l))
'πμ½λ©ν μ€νΈ:CodingTest' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€ 1406λ²-νμ΄μ¬]μλν° (0) | 2023.06.20 |
---|---|
[λ°±μ€ 18870λ²-νμ΄μ¬]μ’νμμΆ (0) | 2023.06.20 |
[λ°±μ€ 1181λ²-νμ΄μ¬]λ¨μ΄ μ λ ¬ (0) | 2023.06.19 |
[λ°±μ€ 11729λ²-νμ΄μ¬]νλ Έμ΄ ν μ΄λ μμ (0) | 2023.06.19 |
[μκ³ λ¦¬μ¦]μ΅λ 곡μ½μ(GCD) μκ³ λ¦¬μ¦ - μ ν΄λ¦¬λ νΈμ λ² (0) | 2023.06.19 |