"""
출처:프로그래머스
https://school.programmers.co.kr/learn/courses/30/lessons/70130
"""
# 풀이 과정
"""
생각방향
# 부분수열의 모든 경우의 수를 비트마스크로 나타낸 후 스타수열 거르기
"""
from itertools import combinations
from collections import Counter
def solution(a):
result = 0
k = Counter(a)
k = dict(k)
# 최대 9개의 수를 데이터를 정렬하기에 많은 시간 소요 안됨
k_new = sorted(k.items(), key=lambda x: (x[1]))
# 최대 숫자 모음 체크
check = []
for i, j in k_new:
check.append(i)
# 들어있는 숫자 횟수
n = 0
while check:
# 스타수열의 교집합이 될 수
s = check.pop()
# 스타수열
star = []
for num in range(len(a)):
i = a[num]
if len(star) % 2 == 0:
star.append(i)
else:
if star[-1] != s and i != s:
continue
elif star[-1] == s and i != s:
star.append(i)
elif star[-1] != s and i == s:
star.append(i)
if len(star) % 2 == 1:
star.pop()
result = max(result, len(star))
if len(check) > 0:
# 이후 만들어질 최대 개수 보다 이미 많을 경우 더이상 무의하기는거 확인
if k[check[-1]] * 2 < result:
return result
return result