Skip to content

Commit

Permalink
Merge pull request #414 from kjb512/main
Browse files Browse the repository at this point in the history
[kayden] Week 04 Solutions
  • Loading branch information
kjb512 authored Sep 6, 2024
2 parents 8301385 + 570eaeb commit 9ae8457
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
17 changes: 17 additions & 0 deletions longest-common-subsequence/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 시간복잡도: O(N)
# 공간복잡도: O(N)
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums = set(nums)
answer = 0

for num in nums:
if num - 1 not in nums:
length = 1

while num + length in nums:
length += 1

answer = max(answer, length)

return answer
33 changes: 33 additions & 0 deletions maximum-product-subarray/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 시간복잡도: O(N)
# 공간복잡도: O(1)
class Solution:
def maxProduct(self, nums: List[int]) -> int:
n = len(nums)
positive, negative = 0, 0

if nums[0] > 0:
positive = nums[0]
else:
negative = nums[0]

answer = max(nums)

for i in range(1, n):
if nums[i] >= 0:
positive *= nums[i]
negative *= nums[i]

if positive == 0:
positive = nums[i]

else:
temp = positive
positive = negative * nums[i]
negative = temp * nums[i]

if negative == 0:
negative = nums[i]

answer = max(answer, positive)

return answer
7 changes: 7 additions & 0 deletions missing-number/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 시간복잡도: O(N)
# 공간복잡도: O(1)
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
total = n*(n+1)//2
return total - sum(nums)
25 changes: 25 additions & 0 deletions valid-palindrome/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 시간복잡도: O(N)
# 공간복잡도: O(N)
class Solution:

def isPalindrome(self, s: str) -> bool:
string = ""

for letter in s:
if letter.isalnum(): # if ('a' <= char <= 'z') or ('A' <= char <= 'Z') or ('0' <= char <= '9'):
string += letter.lower()


def valid(s):
start, end = 0, len(s)-1

while start < end:
if s[start] != s[end]:
return False

start += 1
end -= 1

return True

return valid(string)
38 changes: 38 additions & 0 deletions word-search/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 시간복잡도: O(N*M*4^limit) limit: word의 길이
# 공간복잡도: O(N*M)
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
m = len(board)
n = len(board[0])
limit = len(word)
visited = [[False for _ in range(n)] for _ in range(m)]
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

target = 0

def dfs(x, y, idx):

if idx == limit - 1:
return True

for i in range(4):
nx, ny = x + dx[i], y + dy[i]

if 0 <= nx < m and 0 <= ny < n and not visited[nx][ny] and board[nx][ny] == word[idx + 1]:
visited[nx][ny] = True
if dfs(nx, ny, idx + 1):
return True
visited[nx][ny] = False

return False

for i in range(m):
for j in range(n):
if board[i][j] == word[target]:
visited[i][j] = True
if dfs(i, j, 0):
return True
visited[i][j] = False

return False

0 comments on commit 9ae8457

Please sign in to comment.