Skip to content

Commit a3dfa91

Browse files
committed
[level 2] Title: 시소 짝꿍, Time: 117.16 ms, Memory: 99.1 MB -BaekjoonHub
1 parent ae08298 commit a3dfa91

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [level 2] 시소 짝꿍 - 152996
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/152996)
4+
5+
### 성능 요약
6+
7+
메모리: 99.1 MB, 시간: 117.16 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 연습문제
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 03월 05일 15:07:35
20+
21+
### 문제 설명
22+
23+
<p>어느 공원 놀이터에는 시소가 하나 설치되어 있습니다. 이 시소는 중심으로부터 2(m), 3(m), 4(m) 거리의 지점에 좌석이 하나씩 있습니다.<br>
24+
이 시소를 두 명이 마주 보고 탄다고 할 때, 시소가 평형인 상태에서 각각에 의해 시소에 걸리는 토크의 크기가 서로 상쇄되어 완전한 균형을 이룰 수 있다면 그 두 사람을 시소 짝꿍이라고 합니다. 즉, 탑승한 사람의 무게와 시소 축과 좌석 간의 거리의 곱이 양쪽 다 같다면 시소 짝꿍이라고 할 수 있습니다.<br>
25+
사람들의 몸무게 목록 <code>weights</code>이 주어질 때, 시소 짝꿍이 몇 쌍 존재하는지 구하여 return 하도록 solution 함수를 완성해주세요.</p>
26+
27+
<hr>
28+
29+
<h5>제한 사항</h5>
30+
31+
<ul>
32+
<li>2 ≤ <code>weights</code>의 길이 ≤ 100,000</li>
33+
<li>100 ≤ <code>weights</code>[i] ≤ 1,000
34+
35+
<ul>
36+
<li>몸무게 단위는 N(뉴턴)으로 주어집니다.</li>
37+
<li>몸무게는 모두 정수입니다.</li>
38+
</ul></li>
39+
</ul>
40+
41+
<hr>
42+
43+
<h5>입출력 예</h5>
44+
<table class="table">
45+
<thead><tr>
46+
<th>weights</th>
47+
<th>result</th>
48+
</tr>
49+
</thead>
50+
<tbody><tr>
51+
<td>[100,180,360,100,270]</td>
52+
<td>4</td>
53+
</tr>
54+
</tbody>
55+
</table>
56+
<hr>
57+
58+
<h5>입출력 예 설명</h5>
59+
60+
<p>{100, 100} 은 서로 같은 거리에 마주보고 앉으면 균형을 이룹니다.<br>
61+
{180, 360} 은 각각 4(m), 2(m) 거리에 마주보고 앉으면 균형을 이룹니다.<br>
62+
{180, 270} 은 각각 3(m), 2(m) 거리에 마주보고 앉으면 균형을 이룹니다.<br>
63+
{270, 360} 은 각각 4(m), 3(m) 거리에 마주보고 앉으면 균형을 이룹니다.</p>
64+
65+
66+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public long solution(int[] weights) {
5+
Arrays.sort(weights);
6+
7+
Map<Double, Long> map = new HashMap<>();
8+
long answer = 0;
9+
10+
for(int w : weights){
11+
12+
double[] ratios = {1.0, 2.0/3, 1.0/2, 3.0/4};
13+
14+
for(double r : ratios){
15+
double target = w * r;
16+
17+
if(map.containsKey(target)){
18+
answer += map.get(target);
19+
}
20+
}
21+
22+
map.put((double)w, map.getOrDefault((double)w, 0L) + 1);
23+
}
24+
25+
return answer;
26+
}
27+
}

0 commit comments

Comments
 (0)