LV 2: 시소 짝궁

ewillwin·2023년 8월 16일
0

문제 링크

LV 2: 시소 짝궁


구현 방식

  • from collections import defaultdict

    • defaultdict(int) -> 초기값을 0으로 설정
    • defaultdict(set) -> 초기값을 빈 set으로 설정
    • defaultdict(list) -> 초기값을 빈 list로 설정
  • weigths를 순회하며 w, w*2, w/2, w*3/2, w*2/3, w*4/3, w*3/4 위치에 있는 시소의 개수를 더해준다

  • 먼저 더해준 후에 해당 위치 시소의 개수를 1 증가시켜준다

  • 예제 출력 결과

defaultdict(<class 'int'>, {100: 1, 200: 0, 50.0: 0, 150.0: 0, 66.66666666666667: 0, 133.33333333333334: 0, 75.0: 0}) 0

defaultdict(<class 'int'>, {100: 1, 200: 0, 50.0: 0, 150.0: 0, 66.66666666666667: 0, 133.33333333333334: 0, 75.0: 0, 180: 1, 360: 0, 90.0: 0, 270.0: 0, 120.0: 0, 240.0: 0, 135.0: 0}) 0

defaultdict(<class 'int'>, {100: 1, 200: 0, 50.0: 0, 150.0: 0, 66.66666666666667: 0, 133.33333333333334: 0, 75.0: 0, 180: 1, 360: 1, 90.0: 0, 270.0: 0, 120.0: 0, 240.0: 0, 135.0: 0, 720: 0, 540.0: 0, 480.0: 0}) 1

defaultdict(<class 'int'>, {100: 2, 200: 0, 50.0: 0, 150.0: 0, 66.66666666666667: 0, 133.33333333333334: 0, 75.0: 0, 180: 1, 360: 1, 90.0: 0, 270.0: 0, 120.0: 0, 240.0: 0, 135.0: 0, 720: 0, 540.0: 0, 480.0: 0}) 2

defaultdict(<class 'int'>, {100: 2, 200: 0, 50.0: 0, 150.0: 0, 66.66666666666667: 0, 133.33333333333334: 0, 75.0: 0, 180: 1, 360: 1, 90.0: 0, 270.0: 1, 120.0: 0, 240.0: 0, 135.0: 0, 720: 0, 540.0: 0, 480.0: 0, 405.0: 0, 202.5: 0}) 4

코드

from collections import defaultdict

def solution(weights):
    N = len(weights)
    
    pair = defaultdict(int) #초기값을 0으로 설정해줌
          
    result = 0
    for w in weights:
        result += pair[w] + pair[w*2] + pair[w/2] + pair[(w*3)/2] + pair[(w*2)/3] + pair[(w*4)/3] + pair[(w*3)/4]
        pair[w] += 1
                
    return result
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글