코딩테스트/programmers (python)

Programmers / 2단계 / 시소 짝꿍 / python

seulll 2024. 5. 10. 23:22

 

https://school.programmers.co.kr/learn/courses/30/lessons/152996

 

코드

from collections import Counter
def solution(weights):
    answer = 0
    times = (3/4, 2/3, 1/2)
    count_dict = Counter(weights)
    for key, value in count_dict.items():
        if value > 1:
            answer += value * (value - 1) / 2
    
    # weights 안에 중복 없애기 
    weights = list(set(weights))
    
    # 같은 무게가 아닐 경우
    for i in weights:    
        for j in times:
            if i * j in weights:
                combination_num = count_dict[i * j] # 조합 갯수
                answer += combination_num * count_dict[i]
            
    return answer

 

 

참조

https://jibinary.tistory.com/48

 

[Python] [Level 2] 시소 짝꿍 (Counter 함수 활용하기)

프로그래머스 문제 시소 짝꿍 https://school.programmers.co.kr/learn/courses/30/lessons/152996 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록

jibinary.tistory.com