File tree Expand file tree Collapse file tree 5 files changed +120
-0
lines changed
longest-common-subsequence Expand file tree Collapse file tree 5 files changed +120
-0
lines changed Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(N)
2+ # 공간복잡도: O(N)
3+ class Solution :
4+ def longestConsecutive (self , nums : List [int ]) -> int :
5+ nums = set (nums )
6+ answer = 0
7+
8+ for num in nums :
9+ if num - 1 not in nums :
10+ length = 1
11+
12+ while num + length in nums :
13+ length += 1
14+
15+ answer = max (answer , length )
16+
17+ return answer
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(N)
2+ # 공간복잡도: O(1)
3+ class Solution :
4+ def maxProduct (self , nums : List [int ]) -> int :
5+ n = len (nums )
6+ positive , negative = 0 , 0
7+
8+ if nums [0 ] > 0 :
9+ positive = nums [0 ]
10+ else :
11+ negative = nums [0 ]
12+
13+ answer = max (nums )
14+
15+ for i in range (1 , n ):
16+ if nums [i ] >= 0 :
17+ positive *= nums [i ]
18+ negative *= nums [i ]
19+
20+ if positive == 0 :
21+ positive = nums [i ]
22+
23+ else :
24+ temp = positive
25+ positive = negative * nums [i ]
26+ negative = temp * nums [i ]
27+
28+ if negative == 0 :
29+ negative = nums [i ]
30+
31+ answer = max (answer , positive )
32+
33+ return answer
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(N)
2+ # 공간복잡도: O(1)
3+ class Solution :
4+ def missingNumber (self , nums : List [int ]) -> int :
5+ n = len (nums )
6+ total = n * (n + 1 )// 2
7+ return total - sum (nums )
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(N)
2+ # 공간복잡도: O(N)
3+ class Solution :
4+
5+ def isPalindrome (self , s : str ) -> bool :
6+ string = ""
7+
8+ for letter in s :
9+ if letter .isalnum (): # if ('a' <= char <= 'z') or ('A' <= char <= 'Z') or ('0' <= char <= '9'):
10+ string += letter .lower ()
11+
12+
13+ def valid (s ):
14+ start , end = 0 , len (s )- 1
15+
16+ while start < end :
17+ if s [start ] != s [end ]:
18+ return False
19+
20+ start += 1
21+ end -= 1
22+
23+ return True
24+
25+ return valid (string )
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(N*M*4^limit) limit: word의 길이
2+ # 공간복잡도: O(N*M)
3+ class Solution :
4+ def exist (self , board : List [List [str ]], word : str ) -> bool :
5+ m = len (board )
6+ n = len (board [0 ])
7+ limit = len (word )
8+ visited = [[False for _ in range (n )] for _ in range (m )]
9+ dx = [0 , 0 , - 1 , 1 ]
10+ dy = [- 1 , 1 , 0 , 0 ]
11+
12+ target = 0
13+
14+ def dfs (x , y , idx ):
15+
16+ if idx == limit - 1 :
17+ return True
18+
19+ for i in range (4 ):
20+ nx , ny = x + dx [i ], y + dy [i ]
21+
22+ if 0 <= nx < m and 0 <= ny < n and not visited [nx ][ny ] and board [nx ][ny ] == word [idx + 1 ]:
23+ visited [nx ][ny ] = True
24+ if dfs (nx , ny , idx + 1 ):
25+ return True
26+ visited [nx ][ny ] = False
27+
28+ return False
29+
30+ for i in range (m ):
31+ for j in range (n ):
32+ if board [i ][j ] == word [target ]:
33+ visited [i ][j ] = True
34+ if dfs (i , j , 0 ):
35+ return True
36+ visited [i ][j ] = False
37+
38+ return False
You can’t perform that action at this time.
0 commit comments