File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ ๏ปฟ# --- ํด์ ---
2
+ #๋งค๊ฐ๋ณ์ nums ๋ฅผ set์ผ๋ก ๋ฐ๊ฟ์ ์ค๋ณต์ ๊ฑฐ
3
+ #set์ list๋ก ์ ํํ์ฌ sortํ์ฌ ์ค๋ฆ์ฐจ์์ผ๋ก ๋ณํ
4
+ #for loop ๋ก nums[1]๋ถํฐ nums[len(nums)-1] ์ ์ ๊ทผ
5
+ #nums[i]-nums[i-1] =1 ์ด๋ฉด ๋ณ์ current๋ฅผ ์ฆ๊ฐ, longest๋ current์ ์ต๋๊ฐ์ ์
๋ฐ์ดํธ
6
+ #๋ง์ฝ ํด๋น ์กฐ๊ฑด์ ๋ง์กฑํ์ง ์๋๋ค๋ฉด current๋ฅผ 1๋ก ์ด๊ธฐํ, ์ดํ longest ๊ฐ return
7
+
8
+ # --- Big O
9
+ #N: ๋งค๊ฐ๋ณ์ nums์ ๊ธธ์ด๊ฐ N์ด๋ค.
10
+
11
+ # Time Complexity: O(N)
12
+ #- set(nums)๋ nums์๋ชจ๋ ์์๋ฅผ ํ๋์ฉ ์ํํ๋ฉฐ ์ค๋ณต ์ ๊ฑฐ : O(N)
13
+ #- list(set(nums)) ๋ data type์ ๋ณํํ๋ฉด์ nums์ ๊ฐฏ์๋งํผ data๋ฅผ ์ถ๊ฐ ์์ฑ: O(N)
14
+ #- for loop ๋ nums[1] ๋ถํฐ nums[len(nums)-1]๋งํผ ์ํ: O(N)
15
+
16
+ # Space Complexity: O(N)
17
+ #-nums = list(set(nums)): list(),set(),dict() ์ด๋ฐ data type์ ๋ณํํ ๋๋ง๋ค N๋งํผ ๊ณต๊ฐ ์ถ๊ฐํ ๋น: O(N)
18
+ #-longest,current ๋ณ์๋ ์์ : O(1)
19
+
20
+ class Solution (object ):
21
+ def longestConsecutive (self , nums ):
22
+ """
23
+ :type nums: List[int]
24
+ :rtype: int
25
+ """
26
+ #If the nums doesn't have elements, return 0
27
+ if len (nums ) == 0 :
28
+ return 0
29
+ #์ค๋ณต์ ๊ฑฐ set
30
+ nums = list (set (nums ))
31
+ #sort
32
+ nums .sort ()
33
+ print (nums )
34
+
35
+ #Variables
36
+ longest = 1
37
+ current = 1
38
+
39
+ #Approah all element of nums for checking the sequnece number or not
40
+ for i in range (1 ,len (nums )):
41
+ if nums [i ] == nums [i - 1 ] + 1 :
42
+ current += 1 #current๋ nums[i]์ nums[i-1]์ ์ฐจ์ด๊ฐ 1์ด๋ฉด +1ํด์ค.
43
+ longest = max (longest , current ) #return๊ฐ์ ์ํด currrent๊ฐ ๊ฐ์ฅ ํฌ๋๋ก ์
๋ฐ์ดํธ
44
+ else :#์ฐ์ ๋์ง ์์ ์ current 1๋ก ์ด๊ธฐํ
45
+ current = 1
46
+ print (longest )
47
+ return longest
48
+
49
+
You canโt perform that action at this time.
0 commit comments