Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sun]Week4 문제풀이 #426

Merged
merged 7 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions longest-consecutive-sequence/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
TC: O(n)
SC: O(n)
"""

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
longest = 0
num_set = set(nums)
print(num_set)

for n in num_set:
if (n-1) not in num_set:
length = 1
while (n+length) in num_set:
length += 1
longest = max(longest, length)

return longest
15 changes: 15 additions & 0 deletions maximum-product-subarray/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
TC: O(n)
SC: O(1)
"""
class Solution:
def maxProduct(self, nums: List[int]) -> int:
minimum, maximum = 1, 1
result = nums[0]

for num in nums:
minimum, maximum = min(minimum * num, maximum * num, num), max(minimum * num, maximum * num, num)
result = max(maximum, result)

return result

12 changes: 12 additions & 0 deletions missing-number/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
TC: O(n)
SC: O(n)
"""

class Solution:
def missingNumber(self, nums: List[int]) -> int:
nums_set = set(nums)
for i in range(len(nums)):
if i not in nums_set:
return i
return len(nums)
22 changes: 22 additions & 0 deletions valid-palindrome/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
TC: O(n)
SC: O(n)
"""
class Solution:
def isPalindrome(self, s: str) -> bool:
str = list(s.lower())
chars = []
for c in str:
if (ord(c) >= ord("a") and ord(c) <= ord("z")) or (ord(c) >= ord("0") and ord(c) <= ord("9")):
chars.append(c)

for i in range(len(chars)//2):

if len(c)%2 == 0:
if chars[i] != chars[-(i+1)]:
return False
else:
if chars[i] != chars[-(i+1)]:
return False

return True
41 changes: 41 additions & 0 deletions word-search/sun912.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
TC: O(ROWS*COLS*4^word length)
SC: O(ROWS*COLS*word length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공간 복잡도를 다른 분들보다 좀 크게 보시는 것 같네요? 설명 좀 해주실 수 있으실까요?

"""

class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
ROWS = len(board)
COLS = len(board[0])
path = set()

def dfs(r,c,i):
if i == len(word):
return True

if (r < 0 or
c < 0 or
r >= ROWS or
c >= COLS or
word[i] != board[r][c] or
(r,c) in path
):
return False

path.add((r,c))

res = (dfs(r+1, c, i+1) or
dfs(r, c+1, i+1) or
dfs(r-1, c, i+1) or
dfs(r, c-1, i+1)
)
path.remove((r,c))
return res

for r in range(ROWS):
for c in range(COLS):
if dfs(r,c, 0): return True

return False