๋ฐ์ํ
๋ฌธ์
ํ์ด
itertools ์์ combinations ์ import ํด์ ๋ฌธ์์ด์ ์กฐํฉ์ ์ด์ฉํ๋ค. combinations ๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ฒ๋ผ ๊ฐ ํํ์ ํ๋์ ์กฐํฉ์ด ๊ฐ ์์๋ณ๋ก ์ชผ๊ฐ์ ธ์ ์ถ๋ ฅ๋๋ฏ๋ก, ํ์์ ๋ฐ๋ผ ํ์ฒ๋ฆฌ๊ฐ ํ์ํ๋ค.
a = 'abcd'
b = list(combinations(a,2))
print(b)
for i in range(len(b)):
b[i] = ''.join(b[i])
print(b)
๋ฌธ์ ๋ order ๋ฌธ์์ด์ ์ํํ๋ฉด์ ๊ฐ ์ฃผ๋ฌธ๋ณ๋ก ๋ชจ๋ ์กฐํฉ์ ๊ฐ์๋ฅผ foodMap์ผ๋ก ์นด์ดํธํ๊ณ , ์ธํธ์ ๋ฉ๋ด ๊ฐ์๋ณ max๊ฐ์ ์นด์ดํธํด๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ง์ง๋ง์ course๋ฅผ ์ํํ๋ฉด์ foodMap์์ value๊ฐ์ด maxCnt์ ๋์ผํ key๋ง answer์ appendํ๊ณ ๋ง์ง๋ง์ answer๋ฅผ sort ํ๊ณ ๋ฆฌํดํ๋ค.
* dictionary๋ฅผ for ๋ฌธ์ ๋๋ฉด์ key์ value๋ฅผ ๋ชจ๋ ์ป๊ณ ์ถ์๋๋ .items()๋ฅผ ์ด์ฉํ๋ค.
a={'a':1,'b':2,'c':3}
print(a,end='\n\n')
for key,value in a.items():
print('key =',key,',','values =',value)
from itertools import combinations
def solution(orders, course):
answer = []
foodMap = [{} for _ in range(11)]
maxCnt = [0 for _ in range(11)]
for str in orders:
for num in range(2,len(str)+1):
for i in combinations(sorted(str),num):
key = ''.join(i)
if key in foodMap[num]:
foodMap[num][key] +=1
maxCnt[num] = max(maxCnt[num],foodMap[num][key])
else:
foodMap[num][key] = 1
for num in course:
for key, value in foodMap[num].items():
if value >=2 and maxCnt[num] == value:
answer.append(key)
answer=sorted(answer)
return answer
๋ฐ์ํ
'๐ป Programming > ์๊ณ ๋ฆฌ์ฆ ํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python] ๋ฐฑ์ค3190_๋ฑ (๊ตฌํ) (0) | 2022.02.15 |
---|---|
[python] ํ๋ก๊ทธ๋๋จธ์ค_์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ (๊ตฌํ) (0) | 2022.02.15 |
[python] ํ๋ก๊ทธ๋๋จธ์ค_์คํจ์จ (์ ๋ ฌ) (0) | 2022.02.15 |
[python] ํ๋ก๊ทธ๋๋จธ์ค_ํ๋ฆฐํฐ (์คํํ) (0) | 2022.02.14 |
[python] ํ๋ก๊ทธ๋๋จธ์ค_๋ฌธ์์ด ์์ถ (์์ ํ์) (0) | 2022.02.14 |