Skip to content

Commit f283f0f

Browse files
committed
593
1 parent d6a1938 commit f283f0f

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
1. 아이디어 :
3+
4개 점 사이의 모든 순서쌍(12개) 거리를 유클리드 거리로 구해 딕셔너리에 카운트한다.
4+
정사각형이면 변 4개(양방향 8번)와 대각선 2개(양방향 4번)만 나오므로,
5+
거리별 개수를 정렬했을 때 [4, 8]이면 정사각형이다.
6+
(int(length) != 0 조건으로 좌표가 겹치는 점(거리 0)은 카운트에서 제외한다.)
7+
2. 시간복잡도 :
8+
o(1) - 점이 항상 4개로 고정
9+
3. 자료구조/알고리즘 :
10+
defaultdict(해시맵), 유클리드 거리
11+
'''
12+
13+
from collections import defaultdict
14+
import math
15+
16+
17+
class Solution:
18+
def validSquare(self, p1: list[int], p2: list[int], p3: list[int], p4: list[int]) -> bool:
19+
lst = [p1, p2, p3, p4]
20+
dic = defaultdict(int)
21+
for i in range(4):
22+
for j in range(4):
23+
if i == j:
24+
continue
25+
x1, y1 = lst[i]
26+
x2, y2 = lst[j]
27+
length = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
28+
if int(length) != 0:
29+
dic[length] += 1
30+
tmp = [i for i in dic.values()]
31+
tmp.sort()
32+
33+
return tmp == [4, 8]

0 commit comments

Comments
 (0)