forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKwonNayeon.py
More file actions
33 lines (27 loc) ยท 966 Bytes
/
KwonNayeon.py
File metadata and controls
33 lines (27 loc) ยท 966 Bytes
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
31
"""
Constraints:
1. n equals the length of array nums
2. n is between 1 and 10^4 inclusive
3. Each element nums[i] is between 0 and n inclusive
4. All numbers in nums are unique (no duplicates)
<Solution 1>
Time Complexity: O(nlogn)
- ์ ๋ ฌ์ nlogn, ์ํ์ n์ด ํ์ํ๋ฏ๋ก ์ ์ฒด์ ์ผ๋ก O(nlogn)
Space Complexity: O(1)
- ์ถ๊ฐ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์๊ณ ์
๋ ฅ ๋ฐฐ์ด๋ง ์ฌ์ฉ
ํ์ด ๋ฐฉ๋ฒ:
1. nums ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
2. 0๋ถํฐ n-1๊น์ง ์์๋๋ก ๋น๊ตํ์ฌ ์ธ๋ฑ์ค์ ๋ค๋ฅธ ๊ฐ์ ์ฐพ์
- ์ธ๋ฑ์ค์ ํด๋น ์์น์ ๊ฐ์ด ์ผ์นํ์ง ์์ ๋, ๊ทธ ์ธ๋ฑ์ค๊ฐ missing number
"""
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
nums.sort()
for i in range(n):
if i == nums[i]:
continue
else:
return i
# missing number๊ฐ ๋ฐฐ์ด์ ๋ง์ง๋ง์ ์ค๋ ๊ฒฝ์ฐ
return n