Programmers#メニューの更新
10909 ワード
LEVEL :
Level2
質問の概要:
これは2021年に提出されたKakaoTalk符号化試験問題である.
これは,所与の文字列の組合せセットで最も重複する組合せを選択する問題である.
ソリューション:
組合せで組合せを求め、リストに変換し、並べ替えて文字列に変換し、値の繰返し度合いを求める.
ソリューション1-コードは、短くて非効率な構造カウント関数を使用します.
import itertools
def solution(orders, course):
answer = {}
res = []
for idx in course :
answer[idx] = []
for order in orders :
for idx in course :
combinations = itertools.combinations(order,idx)
for combination in combinations :
c = "".join(sorted(list(combination)))
answer[idx].append(c)
for idx in course :
if not answer[idx] :
continue
max_cnt = 0
max_course = []
for menu in set(answer[idx]) :
count = answer[idx].count(menu)
if max_cnt < count and count >= 2:
max_course = [menu]
max_cnt=count
elif max_cnt == count and count >= 2 :
max_course.append(menu)
res += max_course
res = sorted(res)
return res
ソリューション2-ソート後のチェックの繰り返し->より効果的な方法import itertools
def solution(orders, course):
answer = {}
res = []
for idx in course :
answer[idx] = []
for order in orders :
for idx in course :
combinations = itertools.combinations(order,idx)
for combination in combinations :
c = "".join(sorted(list(combination)))
answer[idx].append(c)
for idx in course :
if not answer[idx] :
continue
max_cnt = 0
max_course = []
answer[idx] = sorted(answer[idx])
compare = [idx][0]
count = 0
for menu in answer[idx] :
if compare == menu :
count += 1
if max_cnt < count and count >= 2:
max_course = [menu]
max_cnt=count
elif max_cnt == count and count >= 2 :
max_course.append(menu)
else :
count = 1
compare = menu
res += max_course
res = sorted(res)
return res
ソース
プログラマー:https://programmers.co.kr/learn/courses/30/lessons/72411
Reference
この問題について(Programmers#メニューの更新), 我々は、より多くの情報をここで見つけました https://velog.io/@tsi0521/Programmers메뉴-리뉴얼テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol