I. Description
II. Code
class Solution:
def clumsy(self, n: int) -> int:
result = 0
add_or_sub = 1
for i in range(n, -1, -4):
if i == 3:
return (result + add_or_sub * 6)
if i == 2:
return (result + add_or_sub * 2)
if i == 1:
return (result + add_or_sub * 1)
if i == 0:
return result
result += add_or_sub * (i * (i-1) // (i-2))
result += (i-3)
add_or_sub = -1
Result를 계산하며 i를 n에서부터 감소시킬 때 마지막은 0~3이 된다. 따라서 그 경우를 미리 고려해주었다.
'코테 준비 > Stack' 카테고리의 다른 글
[LeetCode] 1008. Construct Binary Search Tree from Preorder Traversal (0) | 2025.01.26 |
---|---|
[LeetCode] 1003. Check If Word Is Valid After Substitutions (0) | 2025.01.26 |
[LeetCode] 1441. Build an Array with Stack Operations (0) | 2025.01.26 |
[LeetCode] 2487. Remove Nodes From Linked List (0) | 2025.01.24 |