Skip to content

Commit 8246eb1

Browse files
authored
Merge pull request #426 from sun912/main
[sun]Week4 문제풀이
2 parents 92359ca + a3b4539 commit 8246eb1

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
TC: O(n)
3+
SC: O(n)
4+
"""
5+
6+
class Solution:
7+
def longestConsecutive(self, nums: List[int]) -> int:
8+
longest = 0
9+
num_set = set(nums)
10+
print(num_set)
11+
12+
for n in num_set:
13+
if (n-1) not in num_set:
14+
length = 1
15+
while (n+length) in num_set:
16+
length += 1
17+
longest = max(longest, length)
18+
19+
return longest

maximum-product-subarray/sun912.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
TC: O(n)
3+
SC: O(1)
4+
"""
5+
class Solution:
6+
def maxProduct(self, nums: List[int]) -> int:
7+
minimum, maximum = 1, 1
8+
result = nums[0]
9+
10+
for num in nums:
11+
minimum, maximum = min(minimum * num, maximum * num, num), max(minimum * num, maximum * num, num)
12+
result = max(maximum, result)
13+
14+
return result
15+

missing-number/sun912.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
TC: O(n)
3+
SC: O(n)
4+
"""
5+
6+
class Solution:
7+
def missingNumber(self, nums: List[int]) -> int:
8+
nums_set = set(nums)
9+
for i in range(len(nums)):
10+
if i not in nums_set:
11+
return i
12+
return len(nums)

valid-palindrome/sun912.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
TC: O(n)
3+
SC: O(n)
4+
"""
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
str = list(s.lower())
8+
chars = []
9+
for c in str:
10+
if (ord(c) >= ord("a") and ord(c) <= ord("z")) or (ord(c) >= ord("0") and ord(c) <= ord("9")):
11+
chars.append(c)
12+
13+
for i in range(len(chars)//2):
14+
15+
if len(c)%2 == 0:
16+
if chars[i] != chars[-(i+1)]:
17+
return False
18+
else:
19+
if chars[i] != chars[-(i+1)]:
20+
return False
21+
22+
return True

word-search/sun912.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
TC: O(ROWS*COLS*4^word length)
3+
SC: O(ROWS*COLS*word length)
4+
"""
5+
6+
class Solution:
7+
def exist(self, board: List[List[str]], word: str) -> bool:
8+
ROWS = len(board)
9+
COLS = len(board[0])
10+
path = set()
11+
12+
def dfs(r,c,i):
13+
if i == len(word):
14+
return True
15+
16+
if (r < 0 or
17+
c < 0 or
18+
r >= ROWS or
19+
c >= COLS or
20+
word[i] != board[r][c] or
21+
(r,c) in path
22+
):
23+
return False
24+
25+
path.add((r,c))
26+
27+
res = (dfs(r+1, c, i+1) or
28+
dfs(r, c+1, i+1) or
29+
dfs(r-1, c, i+1) or
30+
dfs(r, c-1, i+1)
31+
)
32+
path.remove((r,c))
33+
return res
34+
35+
for r in range(ROWS):
36+
for c in range(COLS):
37+
if dfs(r,c, 0): return True
38+
39+
return False
40+
41+

0 commit comments

Comments
 (0)