일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- realclass
- English
- 사이드
- 만화도
- 뭐든
- 괜찮음
- 3줄정리
- 쓰릴오브파이트
- 영어원서읽기
- 영어공부
- 파비최
- leetcode
- 월간
- 링피트
- 스탭퍼
- 리얼 클래스
- 30분
- Problem Solving
- FIT XR
- 개발자
- Daily Challenge
- 매일
- 잡생각
- 읽기
- 운동
- 프로젝트
- Writing
- 10분
- 화상영어
- 미드시청
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.11.29 Today's Challenge 본문
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/
DP로 푸는 문제. 변수선언을 하는 특이한 방법에 대해서 배움 (1_000_000_000)
class Solution:
def numberOfWays(self, corridor: str) -> int:
MOD = 1_000_000_007
cache = [[-1] * 3 for _ in range(len(corridor))]
def count(index, seats):
if index == len(corridor):
return 1 if seats == 2 else 0
if cache[index][seats] != -1:
return cache[index][seats]
if seats == 2:
if corridor[index] == "S":
result = count(index + 1, 1)
else:
result = (count(index+1, 0) + count(index+1, 2)) % MOD
else:
if corridor[index] == "S":
result = count(index+1, seats+1)
else:
result = count(index+1, seats)
cache[index][seats] = result
return cache[index][seats]
return count(0, 0)
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.12.01 Today's Challenge (0) | 2023.12.01 |
---|---|
2023.11.30 Today's Challenge (0) | 2023.11.30 |
2023.11.28 Today's Challenge (0) | 2023.11.28 |
2023.11.27 Today's Challenge (0) | 2023.11.27 |
2023.11.26 Today's Challenge (0) | 2023.11.26 |
Comments