본문 바로가기
알고리즘 & 자료구조/프로그래머스

시소 짝꿍

by 신재권 2023. 3. 30.
package programmers;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class 시소_짝꿍 {

   //시소 중심으로 2,3,4 거리의 지점에 좌석이 있다.
   //시소를 두 명이 탈 때, 탑승한 사람의 무게와 시소 측과 좌석 간의 거리의 곱이 양쪽이 같다면 시소 짝꿍
   // 1:1, 2:1, 3:2, 4:3

   public static long solution(int[] weights) {
      long ans = 0;
      Map<Double, Integer> map = new HashMap<>();

      Arrays.sort(weights);

      for (int w : weights) {
         ans += getMate(w, map);
      }

      return ans;
   }

   private static long getMate(int w, Map<Double, Integer> map) {
      long count = 0;
      double d1 = w * 1.0;
      double d2 = (w * 2.0) / 3.0;
      double d3 = (w * 1.0) / 2.0;
      double d4 = (w * 3.0) / 4.0;
      
      if(map.containsKey(d1)) count += map.get(d1);
      if(map.containsKey(d2)) count += map.get(d2);
      if(map.containsKey(d3)) count += map.get(d3);
      if(map.containsKey(d4)) count += map.get(d4);

      map.put(d1, map.getOrDefault(d1, 0) + 1);
      
      return count;
   }

   public static void main(String[] args) {
      System.out.println(solution(new int[] {100, 180, 360, 100, 270}));
      System.out.println(solution(new int[] {100, 200, 300, 400}));
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

미로 탈출  (0) 2023.04.03
조이스틱  (0) 2023.03.31
N Queen  (0) 2023.03.29
테이블 해시 함수  (0) 2023.03.28
혼자 놀기의 달인  (0) 2023.03.27