โ 2910 ๋น๋ ์ ๋ ฌ โ
import sys
input=sys.stdin.readline
N,C=map(int,input().split())
freq=dict()
message=list(map(int,input().split()))
order=1
for n in message:
if n not in freq:
freq[n] = [1, order]
order += 1
else:
freq[n][0] += 1
freq_sorted = sorted(freq.items(), key=lambda x:(-x[1][0],x[1][1]))
for x in freq_sorted:
for _ in range(x[1][0]):
print(str(x[0]),end=' ')
๐ง๐ป๐จ hash table์ key๊ฐ์ ๊ณ ์ ํ ์ซ์, value๋ ์ซ์์ ๋น๋์ ์ซ์ ์ถํ ์์ ๋ ๊ฐ๋ฅผ list ํํ๋ก ์ ์ฅํ๊ฒ๋ ๊ตฌ์ฑํ ์ ์๋ค
๐ง๐ป๐จ ์์์ผ ํ ๊ฒ โ
์๋ ์์ ํ์ธ: ๋จผ์ ๋น๋์ ๊ธฐ์ค์ ๋ง๊ฒ ์ ๋ ฌ(default ์ค๋ฆ์ฐจ์์ด๋ฏ๋ก ์์ - ๋ถ์ฌ ๋ด๋ฆผ์ฐจ์ / order ์ค๋ฆ์ฐจ์์ด๋ฏ๋ก ๊ทธ๋๋ก)
โป dictionary sorted() lambda function ์ฌ์ฉ code ๊ผญ ์ต์ํด์ง๊ธฐ โป
๐ง๐ป๐จ ์์์ผ ํ ๊ฒ โก
: ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ์ ์ํด ์ ์ด์ dictionary๋ฅผ ๋ชจ๋ ์์ฐ์๋ก ์ด๊ธฐํํ์ง ๋ง๊ณ , ๋ฑ์ฅํ๋ ๋ถ๋ถ๋ง key๋ฅผ ๋ง๋ ๋ค
โ 20920 ์๋จ์ด ์๊ธฐ๋ ๊ดด๋ก์ โ
import sys
input=sys.stdin.readline
from collections import defaultdict
N,M=map(int,input().split())
note=defaultdict(list)
for _ in range(N):
word=input().rstrip()
if len(word)>=M:
if word in note.keys():
note[word][0]+=1
else:
note[word].extend([1,len(word)])
note=sorted(note.keys(),key= lambda x: (-note[x][0],-note[x][1],x))
print(*note,sep='\n')
๐ง๐ป๐จ dictionary sorting
: defauldict๋ก list๊ฐ key์ธ dictionary๋ฅผ ๋ง๋ค์ด [๋น๋์, ๊ธธ์ด] update. key = lambda x: ๋ก list์ ์ฒซ๋ฒ์งธ ์์ ๋จผ์ → list์ ๋๋ฒ์งธ ์์ → key ๊ฐ alphabetical order ์ ๋ ฌ code (lambda x : (-note[x][0], -note[x][1], x)) ๊ฑฐ๊พธ๋ก ์ ๋ ฌํ๊ณ ์ถ๋ค๋ฉด ์ซ์์ ๊ฒฝ์ฐ ์์ ๋ง์ด๋์ค ๋ถํธ(-) ๋ถ์ด๊ธฐ
'BOJ > ๐ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ Combinatorics Upper-Intermediate I - 4 Solvedโ (0) | 2023.08.16 |
---|---|
โ Regular Expression ์ค์๊ธ - 2๋ฌธ์ ()โ (0) | 2023.08.14 |
โ DAC ์ค์๊ธ1 - 3๋ฌธ์ ()โ (0) | 2023.03.20 |
โ Number Theory Upper-Intermediate I - 8 Solvedโ (0) | 2023.02.28 |
โ BFS&DFS Intermediate I - 17 Solvedโ (0) | 2023.02.17 |
๋๊ธ