Skip to content

Commit 8fc1dd4

Browse files
committed
feat: 등차수열의 원리 활용
1 parent 3d14ae4 commit 8fc1dd4

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

missing-number/gyeo-ri.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
"""
22
[결과 요약]
3-
# 재시도횟수: 1회
3+
# 재시도횟수: 2회
44
1. 반복문으로 구하기: 로직은 쉬우나 n이 최대 10^4이므로 input 길이에 따라 속도가 느려질 수 있음
5+
2. 등차수열의 원리를 활용하여 n!을 구한 다음 nums의 합을 빼주기
56
"""
67

78

89
class Solution:
910
def missingNumber(self, nums: list[int]) -> int:
1011
len_nums = len(nums)
11-
for i in range(len_nums + 1):
12-
if i not in nums:
13-
return i
14-
15-
raise Exception("No Answer")
12+
total_sum = int(len_nums * (len_nums + 1) / 2)
13+
return total_sum - sum(nums)
1614

1715

1816
if __name__ == "__main__":

0 commit comments

Comments
 (0)