일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- realclass
- leetcode
- 사이드
- 링피트
- 10분
- 영어원서읽기
- FIT XR
- 리얼 클래스
- 매일
- 30분
- 운동
- 파비최
- 화상영어
- 잡생각
- 읽기
- 괜찮음
- English
- 스탭퍼
- Writing
- 만화도
- 3줄정리
- 뭐든
- 미드시청
- 프로젝트
- 월간
- Problem Solving
- 쓰릴오브파이트
- 영어공부
- Daily Challenge
- 개발자
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/matchsticks-to-square/
Matchsticks to Square - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
DFS, DP로 풀 수 있는 문제.
생각보다 어렵다.. 처음에 어떻게 접근할지 감이 안 옴 =_=
class Solution:
def makesquare(self, matchsticks: List[int]) -> bool:
if not matchsticks:
return False
l = len(matchsticks)
p = sum(matchsticks)
ps = p // 4
if ps * 4 != p:
return False
matchsticks.sort(reverse=True)
s = [0, 0, 0, 0]
def dfs(index):
if index == l:
return s[0] == s[1] == s[2] == s[3] == ps
for i in range(4):
if s[i] + matchsticks[index] <= ps:
s[i] += matchsticks[index]
if dfs(index + 1):
return True
s[i] -= matchsticks[index]
return False
return dfs(0)
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (0) | 2022.07.14 |
---|---|
Today's Challenge (0) | 2022.07.13 |
Today's Challenge (0) | 2022.07.11 |
Today's Challenge (0) | 2022.07.10 |
Today's Challenge (0) | 2022.07.09 |
Comments