-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #414 from kjb512/main
[kayden] Week 04 Solutions
- Loading branch information
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |